src/access.py
author Paul Crowley <paul@lshift.net>
Fri, 20 Feb 2009 14:29:48 +0000
changeset 51 d87eeeae29a5
parent 50 77d97aa18f29
child 52 f9eb98bb0791
permissions -rw-r--r--
Use the context API
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50
77d97aa18f29 update dates and copyright notices
Paul Crowley <paul@lshift.net>
parents: 41
diff changeset
     1
# Copyright 2008-2009 LShift Ltd
17
4c98440de851 Started work on acl.py replacement - currently broken.
Paul Crowley <paul@ciphergoth.org>
parents:
diff changeset
     2
# Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com>
4c98440de851 Started work on acl.py replacement - currently broken.
Paul Crowley <paul@ciphergoth.org>
parents:
diff changeset
     3
#
18
538d6b198f4a Big change to support file conditions; format of hg-ssh-access.conf
Paul Crowley <paul@lshift.net>
parents: 17
diff changeset
     4
# Authors:
538d6b198f4a Big change to support file conditions; format of hg-ssh-access.conf
Paul Crowley <paul@lshift.net>
parents: 17
diff changeset
     5
# Paul Crowley <paul@lshift.net>
538d6b198f4a Big change to support file conditions; format of hg-ssh-access.conf
Paul Crowley <paul@lshift.net>
parents: 17
diff changeset
     6
# Vadim Gelfer <vadim.gelfer@gmail.com>
538d6b198f4a Big change to support file conditions; format of hg-ssh-access.conf
Paul Crowley <paul@lshift.net>
parents: 17
diff changeset
     7
#
17
4c98440de851 Started work on acl.py replacement - currently broken.
Paul Crowley <paul@ciphergoth.org>
parents:
diff changeset
     8
# This software may be used and distributed according to the terms
4c98440de851 Started work on acl.py replacement - currently broken.
Paul Crowley <paul@ciphergoth.org>
parents:
diff changeset
     9
# of the GNU General Public License, incorporated herein by reference.
4c98440de851 Started work on acl.py replacement - currently broken.
Paul Crowley <paul@ciphergoth.org>
parents:
diff changeset
    10
4c98440de851 Started work on acl.py replacement - currently broken.
Paul Crowley <paul@ciphergoth.org>
parents:
diff changeset
    11
from mercurial.i18n import _
51
d87eeeae29a5 Use the context API
Paul Crowley <paul@lshift.net>
parents: 50
diff changeset
    12
import mercurial.util
17
4c98440de851 Started work on acl.py replacement - currently broken.
Paul Crowley <paul@ciphergoth.org>
parents:
diff changeset
    13
4c98440de851 Started work on acl.py replacement - currently broken.
Paul Crowley <paul@ciphergoth.org>
parents:
diff changeset
    14
import os
19
62ee928ac9b3 fixes following actual testing
Paul Crowley <paul@lshift.net>
parents: 18
diff changeset
    15
import ruleset
17
4c98440de851 Started work on acl.py replacement - currently broken.
Paul Crowley <paul@ciphergoth.org>
parents:
diff changeset
    16
4c98440de851 Started work on acl.py replacement - currently broken.
Paul Crowley <paul@ciphergoth.org>
parents:
diff changeset
    17
class Checker(object):
4c98440de851 Started work on acl.py replacement - currently broken.
Paul Crowley <paul@ciphergoth.org>
parents:
diff changeset
    18
    '''acl checker.'''
4c98440de851 Started work on acl.py replacement - currently broken.
Paul Crowley <paul@ciphergoth.org>
parents:
diff changeset
    19
4c98440de851 Started work on acl.py replacement - currently broken.
Paul Crowley <paul@ciphergoth.org>
parents:
diff changeset
    20
    def __init__(self, ui, repo):
4c98440de851 Started work on acl.py replacement - currently broken.
Paul Crowley <paul@ciphergoth.org>
parents:
diff changeset
    21
        self.ui = ui
4c98440de851 Started work on acl.py replacement - currently broken.
Paul Crowley <paul@ciphergoth.org>
parents:
diff changeset
    22
        self.repo = repo
39
f5055ce263c7 New system. No breaking in, just putting files in /etc/mercurial-server
Paul Crowley <paul@lshift.net>
parents: 33
diff changeset
    23
        
f5055ce263c7 New system. No breaking in, just putting files in /etc/mercurial-server
Paul Crowley <paul@lshift.net>
parents: 33
diff changeset
    24
        self.rules = ruleset.rules_from_env()
21
59540181a4bb simplify by allowing some params to be preset in rules
Paul Crowley <paul@ciphergoth.org>
parents: 20
diff changeset
    25
        self.rules.set(user = os.environ['REMOTE_USER'])
59540181a4bb simplify by allowing some params to be preset in rules
Paul Crowley <paul@ciphergoth.org>
parents: 20
diff changeset
    26
        self.rules.set(repo = os.environ['HG_REPO_PATH'])
17
4c98440de851 Started work on acl.py replacement - currently broken.
Paul Crowley <paul@ciphergoth.org>
parents:
diff changeset
    27
51
d87eeeae29a5 Use the context API
Paul Crowley <paul@lshift.net>
parents: 50
diff changeset
    28
    def allow(self, ctx):
20
f4daa224dc7e Add support for locking by branch, and document breaking in.
Paul Crowley <paul@ciphergoth.org>
parents: 18
diff changeset
    29
        branch = ctx.branch()
22
578555227599 branch is local not member
Paul Crowley <paul@ciphergoth.org>
parents: 21
diff changeset
    30
        if not self.rules.allow("write", branch=branch, file=None):
20
f4daa224dc7e Add support for locking by branch, and document breaking in.
Paul Crowley <paul@ciphergoth.org>
parents: 18
diff changeset
    31
            return False
f4daa224dc7e Add support for locking by branch, and document breaking in.
Paul Crowley <paul@ciphergoth.org>
parents: 18
diff changeset
    32
        for f in ctx.files():
22
578555227599 branch is local not member
Paul Crowley <paul@ciphergoth.org>
parents: 21
diff changeset
    33
            if not self.rules.allow("write", branch=branch, file=f):
20
f4daa224dc7e Add support for locking by branch, and document breaking in.
Paul Crowley <paul@ciphergoth.org>
parents: 18
diff changeset
    34
                return False
f4daa224dc7e Add support for locking by branch, and document breaking in.
Paul Crowley <paul@ciphergoth.org>
parents: 18
diff changeset
    35
        return True
17
4c98440de851 Started work on acl.py replacement - currently broken.
Paul Crowley <paul@ciphergoth.org>
parents:
diff changeset
    36
51
d87eeeae29a5 Use the context API
Paul Crowley <paul@lshift.net>
parents: 50
diff changeset
    37
    def check(self, ctx):
d87eeeae29a5 Use the context API
Paul Crowley <paul@lshift.net>
parents: 50
diff changeset
    38
        '''return if access allowed, raise exception if not.'''
d87eeeae29a5 Use the context API
Paul Crowley <paul@lshift.net>
parents: 50
diff changeset
    39
        if not self.allow(ctx):
d87eeeae29a5 Use the context API
Paul Crowley <paul@lshift.net>
parents: 50
diff changeset
    40
            raise mercurial.util.Abort(_('%s: access denied for changeset %s') %
d87eeeae29a5 Use the context API
Paul Crowley <paul@lshift.net>
parents: 50
diff changeset
    41
                (__name__, ctx.short()))
20
f4daa224dc7e Add support for locking by branch, and document breaking in.
Paul Crowley <paul@ciphergoth.org>
parents: 18
diff changeset
    42
f4daa224dc7e Add support for locking by branch, and document breaking in.
Paul Crowley <paul@ciphergoth.org>
parents: 18
diff changeset
    43
        
17
4c98440de851 Started work on acl.py replacement - currently broken.
Paul Crowley <paul@ciphergoth.org>
parents:
diff changeset
    44
def hook(ui, repo, hooktype, node=None, source=None, **kwargs):
4c98440de851 Started work on acl.py replacement - currently broken.
Paul Crowley <paul@ciphergoth.org>
parents:
diff changeset
    45
    if hooktype != 'pretxnchangegroup':
51
d87eeeae29a5 Use the context API
Paul Crowley <paul@lshift.net>
parents: 50
diff changeset
    46
        raise mercurial.util.Abort(_('config error - hook type "%s" cannot stop '
17
4c98440de851 Started work on acl.py replacement - currently broken.
Paul Crowley <paul@ciphergoth.org>
parents:
diff changeset
    47
                           'incoming changesets') % hooktype)
18
538d6b198f4a Big change to support file conditions; format of hg-ssh-access.conf
Paul Crowley <paul@lshift.net>
parents: 17
diff changeset
    48
    c = Checker(ui, repo)
51
d87eeeae29a5 Use the context API
Paul Crowley <paul@lshift.net>
parents: 50
diff changeset
    49
    start = repo.changectx(node).rev()
d87eeeae29a5 Use the context API
Paul Crowley <paul@lshift.net>
parents: 50
diff changeset
    50
    end = repo.changelog.count()
17
4c98440de851 Started work on acl.py replacement - currently broken.
Paul Crowley <paul@ciphergoth.org>
parents:
diff changeset
    51
    for rev in xrange(start, end):
51
d87eeeae29a5 Use the context API
Paul Crowley <paul@lshift.net>
parents: 50
diff changeset
    52
        c.check(repo.changectx(rev))
17
4c98440de851 Started work on acl.py replacement - currently broken.
Paul Crowley <paul@ciphergoth.org>
parents:
diff changeset
    53