src/access.py
author Paul Crowley <paul@lshift.net>
Tue, 24 Feb 2009 09:49:56 +0000
changeset 63 b75177d307e5
parent 62 f1e319d3672a
permissions -rw-r--r--
merged

# Copyright 2008-2009 LShift Ltd
# Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com>
#
# Authors:
# Paul Crowley <paul@lshift.net>
# Vadim Gelfer <vadim.gelfer@gmail.com>
#
# This software may be used and distributed according to the terms
# of the GNU General Public License, incorporated herein by reference.

from mercurial.i18n import _
import mercurial.util
import mercurial.node

import os
import ruleset
import changes

class Checker(object):
    '''acl checker.'''

    def __init__(self, ui, repo):
        self.ui = ui
        self.repo = repo
        
        self.rules = ruleset.rules_from_env()
        self.rules.set(user = os.environ['REMOTE_USER'])
        self.rules.set(repo = os.environ['HG_REPO_PATH'])

    def allow(self, ctx):
        branch = ctx.branch()
        if not self.rules.allow("write", branch=branch, file=None):
            return False
        for f in ctx.files():
            if not self.rules.allow("write", branch=branch, file=f):
                return False
        return True

    def check(self, ctx):
        '''return if access allowed, raise exception if not.'''
        if not self.allow(ctx):
            raise mercurial.util.Abort(_('%s: access denied for changeset %s') %
                (__name__, mercurial.node.short(ctx.node())))

def hook(ui, repo, hooktype, node=None, source=None, **kwargs):
    if hooktype != 'pretxnchangegroup':
        raise mercurial.util.Abort(_('config error - hook type "%s" cannot stop '
                           'incoming changesets') % hooktype)
    c = Checker(ui, repo)
    for ctx in changes.changes(repo, node):
        c.check(ctx)