src/mercurialserver/access.py
author David Douard <david.douard@logilab.fr>
Mon, 03 Nov 2014 11:12:45 +0100
changeset 372 80f78674c56e
parent 311 3cbde66305e4
permissions -rw-r--r--
Add support for phases This adds a "publish" permission level (between "init" and "write") required to be able to change the phase of a changeset from "draft" to "public". Update documentation accordingly. This is meant to be used for using the changeset evolution feature of mercurial, see http://evolution.experimentalworks.net/doc/
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
242
03d8f07230b3 Tidy up file prologues; move credits to CREDITS
Paul Crowley <paul@lshift.net>
parents: 241
diff changeset
     1
"""Mercurial access control hook"""
17
4c98440de851 Started work on acl.py replacement - currently broken.
Paul Crowley <paul@ciphergoth.org>
parents:
diff changeset
     2
4c98440de851 Started work on acl.py replacement - currently broken.
Paul Crowley <paul@ciphergoth.org>
parents:
diff changeset
     3
from mercurial.i18n import _
51
d87eeeae29a5 Use the context API
Paul Crowley <paul@lshift.net>
parents: 50
diff changeset
     4
import mercurial.util
53
853444c5d393 small fix
Paul Crowley <paul@lshift.net>
parents: 52
diff changeset
     5
import mercurial.node
17
4c98440de851 Started work on acl.py replacement - currently broken.
Paul Crowley <paul@ciphergoth.org>
parents:
diff changeset
     6
4c98440de851 Started work on acl.py replacement - currently broken.
Paul Crowley <paul@ciphergoth.org>
parents:
diff changeset
     7
import os
67
fd16d9a1234b Put .py files into a directory of their own
Paul Crowley <paul@lshift.net>
parents: 62
diff changeset
     8
from mercurialserver import ruleset
fd16d9a1234b Put .py files into a directory of their own
Paul Crowley <paul@lshift.net>
parents: 62
diff changeset
     9
from mercurialserver import changes
17
4c98440de851 Started work on acl.py replacement - currently broken.
Paul Crowley <paul@ciphergoth.org>
parents:
diff changeset
    10
241
4af1e1ccf75b "Checker" class isn't doing any work, replace with function
Paul Crowley <paul@lshift.net>
parents: 77
diff changeset
    11
def allow(ctx):
4af1e1ccf75b "Checker" class isn't doing any work, replace with function
Paul Crowley <paul@lshift.net>
parents: 77
diff changeset
    12
    branch = ctx.branch()
4af1e1ccf75b "Checker" class isn't doing any work, replace with function
Paul Crowley <paul@lshift.net>
parents: 77
diff changeset
    13
    if not ruleset.rules.allow("write", branch=branch, file=None):
4af1e1ccf75b "Checker" class isn't doing any work, replace with function
Paul Crowley <paul@lshift.net>
parents: 77
diff changeset
    14
        return False
4af1e1ccf75b "Checker" class isn't doing any work, replace with function
Paul Crowley <paul@lshift.net>
parents: 77
diff changeset
    15
    for f in ctx.files():
4af1e1ccf75b "Checker" class isn't doing any work, replace with function
Paul Crowley <paul@lshift.net>
parents: 77
diff changeset
    16
        if not ruleset.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
    17
            return False
241
4af1e1ccf75b "Checker" class isn't doing any work, replace with function
Paul Crowley <paul@lshift.net>
parents: 77
diff changeset
    18
    return True
20
f4daa224dc7e Add support for locking by branch, and document breaking in.
Paul Crowley <paul@ciphergoth.org>
parents: 18
diff changeset
    19
17
4c98440de851 Started work on acl.py replacement - currently broken.
Paul Crowley <paul@ciphergoth.org>
parents:
diff changeset
    20
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
    21
    if hooktype != 'pretxnchangegroup':
51
d87eeeae29a5 Use the context API
Paul Crowley <paul@lshift.net>
parents: 50
diff changeset
    22
        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
    23
                           'incoming changesets') % hooktype)
52
f9eb98bb0791 Encapsulate change finding with backwards compatibility
Paul Crowley <paul@lshift.net>
parents: 51
diff changeset
    24
    for ctx in changes.changes(repo, node):
241
4af1e1ccf75b "Checker" class isn't doing any work, replace with function
Paul Crowley <paul@lshift.net>
parents: 77
diff changeset
    25
        if not allow(ctx):
4af1e1ccf75b "Checker" class isn't doing any work, replace with function
Paul Crowley <paul@lshift.net>
parents: 77
diff changeset
    26
            raise mercurial.util.Abort(_('%s: access denied for changeset %s') %
4af1e1ccf75b "Checker" class isn't doing any work, replace with function
Paul Crowley <paul@lshift.net>
parents: 77
diff changeset
    27
                (__name__, mercurial.node.short(ctx.node())))
372
80f78674c56e Add support for phases
David Douard <david.douard@logilab.fr>
parents: 311
diff changeset
    28
80f78674c56e Add support for phases
David Douard <david.douard@logilab.fr>
parents: 311
diff changeset
    29
def phasehook(ui, repo, hooktype, **kwargs):
80f78674c56e Add support for phases
David Douard <david.douard@logilab.fr>
parents: 311
diff changeset
    30
    if hooktype != 'prepushkey':
80f78674c56e Add support for phases
David Douard <david.douard@logilab.fr>
parents: 311
diff changeset
    31
        raise mercurial.util.Abort(_('config error - hook type "%s" cannot stop '
80f78674c56e Add support for phases
David Douard <david.douard@logilab.fr>
parents: 311
diff changeset
    32
                                     'incoming phase changes') % hooktype)
80f78674c56e Add support for phases
David Douard <david.douard@logilab.fr>
parents: 311
diff changeset
    33
    if kwargs.get('namespace') == 'phases':
80f78674c56e Add support for phases
David Douard <david.douard@logilab.fr>
parents: 311
diff changeset
    34
        if not ruleset.rules.allow("publish"):
80f78674c56e Add support for phases
David Douard <david.douard@logilab.fr>
parents: 311
diff changeset
    35
            raise mercurial.util.Abort(_('%s: access denied for making %s public') %
80f78674c56e Add support for phases
David Douard <david.douard@logilab.fr>
parents: 311
diff changeset
    36
                                       (__name__, kwargs['key'][:12]))