src/access.py
changeset 51 d87eeeae29a5
parent 50 77d97aa18f29
child 52 f9eb98bb0791
equal deleted inserted replaced
50:77d97aa18f29 51:d87eeeae29a5
     7 #
     7 #
     8 # This software may be used and distributed according to the terms
     8 # This software may be used and distributed according to the terms
     9 # of the GNU General Public License, incorporated herein by reference.
     9 # of the GNU General Public License, incorporated herein by reference.
    10 
    10 
    11 from mercurial.i18n import _
    11 from mercurial.i18n import _
    12 from mercurial.node import *
    12 import mercurial.util
    13 from mercurial import util
       
    14 
    13 
    15 import os
    14 import os
    16 import ruleset
    15 import ruleset
    17 
    16 
    18 class Checker(object):
    17 class Checker(object):
    24         
    23         
    25         self.rules = ruleset.rules_from_env()
    24         self.rules = ruleset.rules_from_env()
    26         self.rules.set(user = os.environ['REMOTE_USER'])
    25         self.rules.set(user = os.environ['REMOTE_USER'])
    27         self.rules.set(repo = os.environ['HG_REPO_PATH'])
    26         self.rules.set(repo = os.environ['HG_REPO_PATH'])
    28 
    27 
    29     def allow(self, node):
    28     def allow(self, ctx):
    30         '''return if access allowed, raise exception if not.'''
       
    31         ctx = self.repo.changectx(node)
       
    32         branch = ctx.branch()
    29         branch = ctx.branch()
    33         if not self.rules.allow("write", branch=branch, file=None):
    30         if not self.rules.allow("write", branch=branch, file=None):
    34             return False
    31             return False
    35         for f in ctx.files():
    32         for f in ctx.files():
    36             if not self.rules.allow("write", branch=branch, file=f):
    33             if not self.rules.allow("write", branch=branch, file=f):
    37                 return False
    34                 return False
    38         self.ui.debug(_('%s: allowing changeset %s\n') % (__name__, short(node)))
       
    39         return True
    35         return True
    40 
    36 
    41     def check(self, node):
    37     def check(self, ctx):
    42         if not self.allow(node):
    38         '''return if access allowed, raise exception if not.'''
    43             raise util.Abort(_('%s: access denied for changeset %s') %
    39         if not self.allow(ctx):
    44                 (__name__, short(node)))
    40             raise mercurial.util.Abort(_('%s: access denied for changeset %s') %
       
    41                 (__name__, ctx.short()))
    45 
    42 
    46         
    43         
    47 def hook(ui, repo, hooktype, node=None, source=None, **kwargs):
    44 def hook(ui, repo, hooktype, node=None, source=None, **kwargs):
    48     if hooktype != 'pretxnchangegroup':
    45     if hooktype != 'pretxnchangegroup':
    49         raise util.Abort(_('config error - hook type "%s" cannot stop '
    46         raise mercurial.util.Abort(_('config error - hook type "%s" cannot stop '
    50                            'incoming changesets') % hooktype)
    47                            'incoming changesets') % hooktype)
    51     c = Checker(ui, repo)
    48     c = Checker(ui, repo)
    52     start = repo.changelog.rev(bin(node))
    49     start = repo.changectx(node).rev()
    53     end = len(repo.changelog)
    50     end = repo.changelog.count()
    54     for rev in xrange(start, end):
    51     for rev in xrange(start, end):
    55         c.check(repo.changelog.node(rev))
    52         c.check(repo.changectx(rev))
    56 
    53