Support specifying access directly for users.
[gitosis/httpauth.git] / gitosis / access.py
blobef9ee74ec94eeb44ed9e54a90475d8c71d30e965
1 import os, logging
2 from ConfigParser import NoSectionError, NoOptionError
4 from gitosis import group
6 def haveAccess(config, user, mode, path):
7 """
8 Map request for write access to allowed path.
10 Note for read-only access, the caller should check for write
11 access too.
13 Returns ``None`` for no access, or a tuple of toplevel directory
14 containing repositories and a relative path to the physical repository.
15 """
16 log = logging.getLogger('gitosis.access.haveAccess')
18 log.debug(
19 'Access check for %(user)r as %(mode)r on %(path)r...'
20 % dict(
21 user=user,
22 mode=mode,
23 path=path,
26 basename, ext = os.path.splitext(path)
27 if ext == '.git':
28 log.debug(
29 'Stripping .git suffix from %(path)r, new value %(basename)r'
30 % dict(
31 path=path,
32 basename=basename,
34 path = basename
36 sections = ['group %s' % item for item in
37 group.getMembership(config=config, user=user)]
38 sections.insert(0, 'user %s' % user)
40 for sectname in sections:
41 try:
42 repos = config.get(sectname, mode)
43 except (NoSectionError, NoOptionError):
44 repos = []
45 else:
46 repos = repos.split()
48 mapping = None
50 if path in repos:
51 log.debug(
52 'Access ok for %(user)r as %(mode)r on %(path)r'
53 % dict(
54 user=user,
55 mode=mode,
56 path=path,
58 mapping = path
59 else:
60 try:
61 mapping = config.get(sectname,
62 'map %s %s' % (mode, path))
63 except (NoSectionError, NoOptionError):
64 pass
65 else:
66 log.debug(
67 'Access ok for %(user)r as %(mode)r on %(path)r=%(mapping)r'
68 % dict(
69 user=user,
70 mode=mode,
71 path=path,
72 mapping=mapping,
75 if mapping is not None:
76 prefix = None
77 try:
78 prefix = config.get(sectname, 'repositories')
79 except (NoSectionError, NoOptionError):
80 try:
81 prefix = config.get('gitosis', 'repositories')
82 except (NoSectionError, NoOptionError):
83 prefix = 'repositories'
85 log.debug(
86 'Using prefix %(prefix)r for %(path)r'
87 % dict(
88 prefix=prefix,
89 path=mapping,
91 return (prefix, mapping)