Everyone always belongs to group "all".

This commit is contained in:
Tommi Virtanen 2007-06-04 14:22:48 +03:00
parent 085e5fffa7
commit 65dc130228
2 changed files with 37 additions and 15 deletions

View file

@ -1,25 +1,15 @@
import logging
from ConfigParser import NoSectionError, NoOptionError
def getMembership(config, user, _seen=None):
"""
Generate groups ``user`` is member of, according to ``config``
:type config: RawConfigParser
:type user: str
:param _seen: internal use only
"""
def _getMembership(config, user, seen):
log = logging.getLogger('gitosis.group.getMembership')
if _seen is None:
_seen = set()
for section in config.sections():
GROUP_PREFIX = 'group '
if not section.startswith(GROUP_PREFIX):
continue
group = section[len(GROUP_PREFIX):]
if group in _seen:
if group in seen:
continue
try:
@ -34,9 +24,28 @@ def getMembership(config, user, _seen=None):
user=user,
group=group,
))
_seen.add(group)
seen.add(group)
yield group
for member_of in getMembership(config, '@%s' % group,
_seen=_seen):
for member_of in _getMembership(
config, '@%s' % group, seen,
):
yield member_of
def getMembership(config, user):
"""
Generate groups ``user`` is member of, according to ``config``
:type config: RawConfigParser
:type user: str
:param _seen: internal use only
"""
seen = set()
for member_of in _getMembership(config, user, seen):
yield member_of
# everyone is always a member of group "all"
yield 'all'

View file

@ -7,12 +7,14 @@ from gitosis import group
def test_no_emptyConfig():
cfg = RawConfigParser()
gen = group.getMembership(config=cfg, user='jdoe')
eq(gen.next(), 'all')
assert_raises(StopIteration, gen.next)
def test_no_emptyGroup():
cfg = RawConfigParser()
cfg.add_section('group hackers')
gen = group.getMembership(config=cfg, user='jdoe')
eq(gen.next(), 'all')
assert_raises(StopIteration, gen.next)
def test_no_notListed():
@ -20,6 +22,7 @@ def test_no_notListed():
cfg.add_section('group hackers')
cfg.set('group hackers', 'members', 'wsmith')
gen = group.getMembership(config=cfg, user='jdoe')
eq(gen.next(), 'all')
assert_raises(StopIteration, gen.next)
def test_yes_simple():
@ -28,6 +31,7 @@ def test_yes_simple():
cfg.set('group hackers', 'members', 'jdoe')
gen = group.getMembership(config=cfg, user='jdoe')
eq(gen.next(), 'hackers')
eq(gen.next(), 'all')
assert_raises(StopIteration, gen.next)
def test_yes_leading():
@ -36,6 +40,7 @@ def test_yes_leading():
cfg.set('group hackers', 'members', 'jdoe wsmith')
gen = group.getMembership(config=cfg, user='jdoe')
eq(gen.next(), 'hackers')
eq(gen.next(), 'all')
assert_raises(StopIteration, gen.next)
def test_yes_trailing():
@ -44,6 +49,7 @@ def test_yes_trailing():
cfg.set('group hackers', 'members', 'wsmith jdoe')
gen = group.getMembership(config=cfg, user='jdoe')
eq(gen.next(), 'hackers')
eq(gen.next(), 'all')
assert_raises(StopIteration, gen.next)
def test_yes_middle():
@ -52,6 +58,7 @@ def test_yes_middle():
cfg.set('group hackers', 'members', 'wsmith jdoe danny')
gen = group.getMembership(config=cfg, user='jdoe')
eq(gen.next(), 'hackers')
eq(gen.next(), 'all')
assert_raises(StopIteration, gen.next)
def test_yes_recurse_one():
@ -63,6 +70,7 @@ def test_yes_recurse_one():
gen = group.getMembership(config=cfg, user='jdoe')
eq(gen.next(), 'smackers')
eq(gen.next(), 'hackers')
eq(gen.next(), 'all')
assert_raises(StopIteration, gen.next)
def test_yes_recurse_one_ordering():
@ -74,6 +82,7 @@ def test_yes_recurse_one_ordering():
gen = group.getMembership(config=cfg, user='jdoe')
eq(gen.next(), 'smackers')
eq(gen.next(), 'hackers')
eq(gen.next(), 'all')
assert_raises(StopIteration, gen.next)
def test_yes_recurse_three():
@ -91,6 +100,7 @@ def test_yes_recurse_three():
eq(gen.next(), 'snackers')
eq(gen.next(), 'smackers')
eq(gen.next(), 'hackers')
eq(gen.next(), 'all')
assert_raises(StopIteration, gen.next)
def test_yes_recurse_junk():
@ -102,6 +112,7 @@ def test_yes_recurse_junk():
gen = group.getMembership(config=cfg, user='jdoe')
eq(gen.next(), 'smackers')
eq(gen.next(), 'hackers')
eq(gen.next(), 'all')
assert_raises(StopIteration, gen.next)
def test_yes_recurse_loop():
@ -113,6 +124,7 @@ def test_yes_recurse_loop():
gen = group.getMembership(config=cfg, user='jdoe')
eq(gen.next(), 'smackers')
eq(gen.next(), 'hackers')
eq(gen.next(), 'all')
assert_raises(StopIteration, gen.next)
def test_no_recurse_loop():
@ -122,4 +134,5 @@ def test_no_recurse_loop():
cfg.add_section('group smackers')
cfg.set('group smackers', 'members', '@hackers')
gen = group.getMembership(config=cfg, user='jdoe')
eq(gen.next(), 'all')
assert_raises(StopIteration, gen.next)