src/mercurialserver/access.py
author Cédric Krier <ced@b2ck.com>
Sun, 14 Dec 2014 20:30:25 +0100
changeset 374 7a1d6b228af6
parent 372 80f78674c56e
permissions -rw-r--r--
Set user as login name using a configuration index of the key path.

"""Mercurial access control hook"""

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

import os
from mercurialserver import ruleset
from mercurialserver import changes

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

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)
    for ctx in changes.changes(repo, node):
        if not allow(ctx):
            raise mercurial.util.Abort(_('%s: access denied for changeset %s') %
                (__name__, mercurial.node.short(ctx.node())))

def phasehook(ui, repo, hooktype, **kwargs):
    if hooktype != 'prepushkey':
        raise mercurial.util.Abort(_('config error - hook type "%s" cannot stop '
                                     'incoming phase changes') % hooktype)
    if kwargs.get('namespace') == 'phases':
        if not ruleset.rules.allow("publish"):
            raise mercurial.util.Abort(_('%s: access denied for making %s public') %
                                       (__name__, kwargs['key'][:12]))