summaryrefslogtreecommitdiff
path: root/src/zope/security/permission.py
blob: 910347702f186f794b07c3fbb63ab91e443558cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
##############################################################################
#
# Copyright (c) 2002 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Permissions
"""
__docformat__ = "reStructuredText"

import operator

from zope.component import getUtilitiesFor
from zope.component import queryUtility
from zope.interface import directlyProvides
from zope.interface import implementer
from zope.schema.interfaces import IVocabularyFactory
from zope.schema.vocabulary import SimpleTerm
from zope.schema.vocabulary import SimpleVocabulary

from zope.security.checker import CheckerPublic
from zope.security.interfaces import PUBLIC_PERMISSION_NAME as zope_Public
from zope.security.interfaces import IPermission


@implementer(IPermission)
class Permission:
    """
    Default implementation of :class:`zope.security.interfaces.IPermission`.
    """

    def __init__(self, id, title="", description=""):
        self.id = id
        self.title = title
        self.description = description


def checkPermission(context, permission_id):
    """
    Check whether a given permission object exists in the provided
    context as a utility.
    """
    if permission_id is CheckerPublic:
        return
    if not queryUtility(IPermission, permission_id, context=context):
        raise ValueError("Undefined permission ID", permission_id)


def allPermissions(context=None):
    """
    Get the IDs of all defined permission object utilities.
    """
    for name, _permission in getUtilitiesFor(IPermission, context):
        if name != zope_Public:
            yield name


def PermissionsVocabulary(context=None):
    """
    A vocabulary of permission IDs.

    Term values are permissions, while term tokens are permission IDs.
    """
    terms = []
    for name, permission in getUtilitiesFor(IPermission, context):
        terms.append(SimpleTerm(permission, name))
    return SimpleVocabulary(terms)


directlyProvides(PermissionsVocabulary, IVocabularyFactory)


def PermissionIdsVocabulary(context=None):
    """
    A vocabulary of permission IDs.

    Term values are the permission ID strings except for
    :data:`zope.Public
    <zope.security.interfaces.PUBLIC_PERMISSION_NAME>`, which is the
    global permission :data:`zope.security.checker.CheckerPublic`.

    Term titles are the permission ID strings except for
    :data:`zope.Public
    <zope.security.interfaces.PUBLIC_PERMISSION_NAME>`, which is
    shortened to 'Public'.

    Terms are sorted by title except for 'Public', which always appears as
    the first term.
    """
    terms = []
    has_public = False
    for name, _permission in getUtilitiesFor(IPermission, context):
        if name == zope_Public:
            has_public = True
        else:
            terms.append(SimpleTerm(name, name, name))
    terms = sorted(terms, key=operator.attrgetter('title'))
    if has_public:
        terms.insert(0, SimpleTerm(CheckerPublic, zope_Public, 'Public'))
    return SimpleVocabulary(terms)


directlyProvides(PermissionIdsVocabulary, IVocabularyFactory)