access.py
changeset 23 9fa62cfd2821
parent 19 62ee928ac9b3
parent 22 578555227599
child 24 9f8e11ede780
--- a/access.py	Tue Apr 22 12:55:56 2008 +0100
+++ b/access.py	Tue Apr 22 13:03:17 2008 +0100
@@ -21,21 +21,32 @@
     def __init__(self, ui, repo):
         self.ui = ui
         self.repo = repo
-        self.repo_path = os.environ['HG_REPO_PATH']
-        self.user = os.environ['REMOTE_USER']
         self.rules = ruleset.Ruleset.readfile(os.environ['HG_ACCESS_RULES_FILE'])
+        self.rules.set(user = os.environ['REMOTE_USER'])
+        self.rules.set(repo = os.environ['HG_REPO_PATH'])
+
+    def allow(self, node):
+        '''return if access allowed, raise exception if not.'''
+        ctx = self.repo.changectx(node)
+        branch = ctx.branch()
+        if not self.rules.allow("write", branch=branch, file=None):
+            self.ui.debug(_('%s: user %s not allowed on branch %s\n') %
+                (__name__, self.user, branch))
+            return False
+        for f in ctx.files():
+            if not self.rules.allow("write", branch=branch, file=f):
+                self.ui.debug(_('%s: user %s not allowed on %s\n') %
+                              (__name__, self.user, f))
+                return False
+        self.ui.debug(_('%s: allowing changeset %s\n') % (__name__, short(node)))
+        return True
 
     def check(self, node):
-        '''return if access allowed, raise exception if not.'''
-        files = self.repo.changectx(node).files()
-        for f in files:
-            if not self.rules.allow("write", user=self.user, repo=self.repo_path, file=f):
-                self.ui.debug(_('%s: user %s not allowed on %s\n') %
-                              (__name__, self.getuser(), f))
-                raise util.Abort(_('%s: access denied for changeset %s') %
-                                 (__name__, short(node)))
-        self.ui.debug(_('%s: allowing changeset %s\n') % (__name__, short(node)))
+        if not allow(self, node):
+            raise util.Abort(_('%s: access denied for changeset %s') %
+                (__name__, short(node)))
 
+        
 def hook(ui, repo, hooktype, node=None, source=None, **kwargs):
     if hooktype != 'pretxnchangegroup':
         raise util.Abort(_('config error - hook type "%s" cannot stop '