simplify by allowing some params to be preset in rules
authorPaul Crowley <paul@ciphergoth.org>
Tue, 22 Apr 2008 10:20:47 +0100
changeset 21 59540181a4bb
parent 20 f4daa224dc7e
child 22 578555227599
simplify by allowing some params to be preset in rules
access.py
hg-ssh
rules.py
--- a/access.py	Tue Apr 22 10:13:39 2008 +0100
+++ b/access.py	Tue Apr 22 10:20:47 2008 +0100
@@ -21,20 +21,20 @@
     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 = rules.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", user=self.user, repo=self.repo_path, branch=self.branch, file=None):
+        if not self.rules.allow("write", branch=self.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", user=self.user, repo=self.repo_path, branch=self.branch, file=f):
+            if not self.rules.allow("write", branch=self.branch, file=f):
                 self.ui.debug(_('%s: user %s not allowed on %s\n') %
                               (__name__, self.user, f))
                 return False
--- a/hg-ssh	Tue Apr 22 10:13:39 2008 +0100
+++ b/hg-ssh	Tue Apr 22 10:20:47 2008 +0100
@@ -48,15 +48,15 @@
         fail("Disallowing path: %s" % path)
     return path
 
-def get_cmd(rules, remoteuser, cmd):
+def get_cmd(rules, cmd):
     if cmd.startswith('hg -R ') and cmd.endswith(' serve --stdio'):
         repo = getpath(cmd[6:-14])
-        if rules.allow("read", user=remoteuser, repo=repo, branch=None, file=None):
+        if rules.allow("read", repo=repo):
             os.environ["HG_REPO_PATH"] = repo
             return ['-R', repo, 'serve', '--stdio']
     elif cmd.startswith('hg init '):
         repo = getpath(cmd[8:])
-        if rules.allow("init", user=remoteuser, repo=repo, branch=None, file=None):
+        if rules.allow("init", repo=repo):
             os.environ["HG_REPO_PATH"] = repo
             return ['init', repo]
     fail("Illegal command %r" % cmd)
@@ -69,8 +69,9 @@
         % sys.argv)
 
 rules = rules.Ruleset.readfile(os.environ['HG_ACCESS_RULES_FILE'])
-remoteuser = getpath(os.environ['REMOTE_USER'])
-todispatch = get_cmd(rules, remoteuser, 
+rules.set(remoteuser = getpath(os.environ['REMOTE_USER']))
+rules.set(branch = None, file = None)
+todispatch = get_cmd(rules, 
     os.environ.get('SSH_ORIGINAL_COMMAND', '?'))
 dispatch.dispatch(todispatch)
 
--- a/rules.py	Tue Apr 22 10:13:39 2008 +0100
+++ b/rules.py	Tue Apr 22 10:20:47 2008 +0100
@@ -50,13 +50,20 @@
 
     def __init__(self):
         self.rules = []
+        self.preset = {}
 
     def add(self, action, conditions):
         self.rules.append((action, conditions))
 
+
+    def set(self, **kw):
+        self.preset.update(kw)
+        
     def matchrule(self, **kw):
+        d = self.preset.copy()
+        d.update(**kw)
         for a, c in self.rules:
-            if c(**kw):
+            if c(**d):
                 return a
         return None
 
@@ -77,5 +84,3 @@
         finally:
             f.close()
         return res
-
-