access.py
author Paul Crowley <paul@lshift.net>
Tue, 22 Apr 2008 09:46:29 +0100
changeset 18 538d6b198f4a
parent 17 4c98440de851
child 19 62ee928ac9b3
child 20 f4daa224dc7e
permissions -rw-r--r--
Big change to support file conditions; format of hg-ssh-access.conf changed.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
18
538d6b198f4a Big change to support file conditions; format of hg-ssh-access.conf
Paul Crowley <paul@lshift.net>
parents: 17
diff changeset
     1
# Copyright 2008 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 _
4c98440de851 Started work on acl.py replacement - currently broken.
Paul Crowley <paul@ciphergoth.org>
parents:
diff changeset
    12
from mercurial.node import *
4c98440de851 Started work on acl.py replacement - currently broken.
Paul Crowley <paul@ciphergoth.org>
parents:
diff changeset
    13
from mercurial import util
4c98440de851 Started work on acl.py replacement - currently broken.
Paul Crowley <paul@ciphergoth.org>
parents:
diff changeset
    14
4c98440de851 Started work on acl.py replacement - currently broken.
Paul Crowley <paul@ciphergoth.org>
parents:
diff changeset
    15
import os
18
538d6b198f4a Big change to support file conditions; format of hg-ssh-access.conf
Paul Crowley <paul@lshift.net>
parents: 17
diff changeset
    16
import rules
17
4c98440de851 Started work on acl.py replacement - currently broken.
Paul Crowley <paul@ciphergoth.org>
parents:
diff changeset
    17
4c98440de851 Started work on acl.py replacement - currently broken.
Paul Crowley <paul@ciphergoth.org>
parents:
diff changeset
    18
class Checker(object):
4c98440de851 Started work on acl.py replacement - currently broken.
Paul Crowley <paul@ciphergoth.org>
parents:
diff changeset
    19
    '''acl checker.'''
4c98440de851 Started work on acl.py replacement - currently broken.
Paul Crowley <paul@ciphergoth.org>
parents:
diff changeset
    20
4c98440de851 Started work on acl.py replacement - currently broken.
Paul Crowley <paul@ciphergoth.org>
parents:
diff changeset
    21
    def __init__(self, ui, repo):
4c98440de851 Started work on acl.py replacement - currently broken.
Paul Crowley <paul@ciphergoth.org>
parents:
diff changeset
    22
        self.ui = ui
4c98440de851 Started work on acl.py replacement - currently broken.
Paul Crowley <paul@ciphergoth.org>
parents:
diff changeset
    23
        self.repo = repo
18
538d6b198f4a Big change to support file conditions; format of hg-ssh-access.conf
Paul Crowley <paul@lshift.net>
parents: 17
diff changeset
    24
        self.repo_path = os.environ['HG_REPO_PATH']
538d6b198f4a Big change to support file conditions; format of hg-ssh-access.conf
Paul Crowley <paul@lshift.net>
parents: 17
diff changeset
    25
        self.user = os.environ['REMOTE_USER']
538d6b198f4a Big change to support file conditions; format of hg-ssh-access.conf
Paul Crowley <paul@lshift.net>
parents: 17
diff changeset
    26
        self.rules = rules.Ruleset.readfile(os.environ['HG_ACCESS_RULES_FILE'])
17
4c98440de851 Started work on acl.py replacement - currently broken.
Paul Crowley <paul@ciphergoth.org>
parents:
diff changeset
    27
4c98440de851 Started work on acl.py replacement - currently broken.
Paul Crowley <paul@ciphergoth.org>
parents:
diff changeset
    28
    def check(self, node):
4c98440de851 Started work on acl.py replacement - currently broken.
Paul Crowley <paul@ciphergoth.org>
parents:
diff changeset
    29
        '''return if access allowed, raise exception if not.'''
4c98440de851 Started work on acl.py replacement - currently broken.
Paul Crowley <paul@ciphergoth.org>
parents:
diff changeset
    30
        files = self.repo.changectx(node).files()
4c98440de851 Started work on acl.py replacement - currently broken.
Paul Crowley <paul@ciphergoth.org>
parents:
diff changeset
    31
        for f in files:
18
538d6b198f4a Big change to support file conditions; format of hg-ssh-access.conf
Paul Crowley <paul@lshift.net>
parents: 17
diff changeset
    32
            if not self.rules.allow("write", user=self.user, repo=self.repo_path, file=f):
17
4c98440de851 Started work on acl.py replacement - currently broken.
Paul Crowley <paul@ciphergoth.org>
parents:
diff changeset
    33
                self.ui.debug(_('%s: user %s not allowed on %s\n') %
4c98440de851 Started work on acl.py replacement - currently broken.
Paul Crowley <paul@ciphergoth.org>
parents:
diff changeset
    34
                              (__name__, self.getuser(), f))
4c98440de851 Started work on acl.py replacement - currently broken.
Paul Crowley <paul@ciphergoth.org>
parents:
diff changeset
    35
                raise util.Abort(_('%s: access denied for changeset %s') %
4c98440de851 Started work on acl.py replacement - currently broken.
Paul Crowley <paul@ciphergoth.org>
parents:
diff changeset
    36
                                 (__name__, short(node)))
4c98440de851 Started work on acl.py replacement - currently broken.
Paul Crowley <paul@ciphergoth.org>
parents:
diff changeset
    37
        self.ui.debug(_('%s: allowing changeset %s\n') % (__name__, short(node)))
4c98440de851 Started work on acl.py replacement - currently broken.
Paul Crowley <paul@ciphergoth.org>
parents:
diff changeset
    38
4c98440de851 Started work on acl.py replacement - currently broken.
Paul Crowley <paul@ciphergoth.org>
parents:
diff changeset
    39
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
    40
    if hooktype != 'pretxnchangegroup':
4c98440de851 Started work on acl.py replacement - currently broken.
Paul Crowley <paul@ciphergoth.org>
parents:
diff changeset
    41
        raise util.Abort(_('config error - hook type "%s" cannot stop '
4c98440de851 Started work on acl.py replacement - currently broken.
Paul Crowley <paul@ciphergoth.org>
parents:
diff changeset
    42
                           '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
    43
    c = Checker(ui, repo)
17
4c98440de851 Started work on acl.py replacement - currently broken.
Paul Crowley <paul@ciphergoth.org>
parents:
diff changeset
    44
    start = repo.changelog.rev(bin(node))
4c98440de851 Started work on acl.py replacement - currently broken.
Paul Crowley <paul@ciphergoth.org>
parents:
diff changeset
    45
    end = repo.changelog.count()
4c98440de851 Started work on acl.py replacement - currently broken.
Paul Crowley <paul@ciphergoth.org>
parents:
diff changeset
    46
    for rev in xrange(start, end):
4c98440de851 Started work on acl.py replacement - currently broken.
Paul Crowley <paul@ciphergoth.org>
parents:
diff changeset
    47
        c.check(repo.changelog.node(rev))
4c98440de851 Started work on acl.py replacement - currently broken.
Paul Crowley <paul@ciphergoth.org>
parents:
diff changeset
    48