• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

zopefoundation / AccessControl / 4274890848

pending completion
4274890848

push

github

Jens Vagelpohl
- vb [ci skip]

934 of 1458 branches covered (64.06%)

Branch coverage included in aggregate %.

4982 of 5839 relevant lines covered (85.32%)

4.26 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

29.59
/src/AccessControl/Permission.py
1
##############################################################################
2
#
3
# Copyright (c) 2002 Zope Foundation and Contributors.
4
#
5
# This software is subject to the provisions of the Zope Public License,
6
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
7
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
8
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
9
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
10
# FOR A PARTICULAR PURPOSE.
11
#
12
##############################################################################
13
"""Permissions
5✔
14
"""
15

16
import re
5✔
17

18
from Acquisition import aq_base
5✔
19

20

21
_NOT_ALLOWED_CHARS = re.compile(r'[^a-zA-Z0-9_]')
5✔
22

23

24
def getPermissionIdentifier(name):
5✔
25
    return '_' + re.sub(_NOT_ALLOWED_CHARS, '_', name) + "_Permission"
5✔
26

27

28
# BBB
29
pname = getPermissionIdentifier
5✔
30

31
_marker = []
5✔
32

33

34
class Permission:
5✔
35
    # A Permission maps a named logical permission to a set
36
    # of attribute names. Attribute names which appear in a
37
    # permission may not appear in any other permission defined
38
    # by the object.
39

40
    def __init__(self, name, data, obj, default=None):
5✔
41
        self.name = name
×
42
        self._p = getPermissionIdentifier(name)
×
43
        self.data = data
×
44
        self.obj = aq_base(obj)
×
45
        self.default = default
×
46

47
    def getRoles(self, default=_marker):
5✔
48
        # Return the list of role names which have been given
49
        # this permission for the object in question. To do
50
        # this, we try to get __roles__ from all of the object
51
        # attributes that this permission represents.
52
        obj = self.obj
×
53
        name = self._p
×
54
        if hasattr(obj, name):
×
55
            return getattr(obj, name)
×
56
        roles = default
×
57
        for name in self.data:
×
58
            if name:
×
59
                if hasattr(obj, name):
×
60
                    attr = getattr(obj, name)
×
61
                    if getattr(attr, '__self__', None) is not None:
×
62
                        attr = attr.__self__
×
63
                        if hasattr(attr, '__dict__'):
×
64
                            attr = attr.__dict__
×
65
                            name = name + '__roles__'
×
66
                            if name in attr:
×
67
                                roles = attr[name]
×
68
                                break
×
69
            elif hasattr(obj, '__dict__'):
×
70
                attr = obj.__dict__
×
71
                if '__roles__' in attr:
×
72
                    roles = attr['__roles__']
×
73
                    break
×
74

75
        if roles:
×
76
            try:
×
77
                if 'Shared' not in roles:
×
78
                    return tuple(roles)
×
79
                roles = list(roles)
×
80
                roles.remove('Shared')
×
81
                return roles
×
82
            except BaseException:
×
83
                return []
×
84

85
        if roles is None:
×
86
            return ['Manager', 'Anonymous']
×
87
        if roles is _marker:
×
88
            return ['Manager']
×
89

90
        return roles
×
91

92
    def setRoles(self, roles):
5✔
93
        obj = self.obj
×
94

95
        if isinstance(roles, list) and not roles:
×
96
            if hasattr(obj, self._p):
×
97
                delattr(obj, self._p)
×
98
        else:
99
            setattr(obj, self._p, roles)
×
100

101
        for name in self.data:
×
102
            if name == '':
×
103
                attr = obj
×
104
            else:
105
                attr = getattr(obj, name)
×
106
            try:
×
107
                del attr.__roles__
×
108
            except BaseException:
×
109
                pass
×
110
            try:
×
111
                delattr(obj, name + '__roles__')
×
112
            except BaseException:
×
113
                pass
×
114

115
    def setRole(self, role, present):
5✔
116
        roles = self.getRoles()
×
117
        if role in roles:
×
118
            if present:
×
119
                return
×
120
            if isinstance(roles, list):
×
121
                roles.remove(role)
×
122
            else:
123
                roles = list(roles)
×
124
                roles.remove(role)
×
125
                roles = tuple(roles)
×
126
        elif not present:
×
127
            return
×
128
        else:
129
            if isinstance(roles, list):
×
130
                roles.append(role)
×
131
            else:
132
                roles = roles + (role, )
×
133
        self.setRoles(roles)
×
134

135
    def __len__(self):
5✔
136
        return 1
×
137

138
    def __str__(self):
5✔
139
        return self.name
×
140

141

142
_registeredPermissions = {}
5✔
143
_ac_permissions = ()
5✔
144

145

146
def getPermissions():
5✔
147
    return _ac_permissions
5✔
148

149

150
def addPermission(perm, default_roles=('Manager', )):
5✔
151
    if perm in _registeredPermissions:
5✔
152
        return
5✔
153

154
    entry = ((perm, (), default_roles), )
5✔
155
    global _ac_permissions
156
    _ac_permissions = _ac_permissions + entry
5✔
157
    _registeredPermissions[perm] = 1
5✔
158
    mangled = getPermissionIdentifier(perm)
5✔
159
    if not hasattr(ApplicationDefaultPermissions, mangled):
5✔
160
        setattr(ApplicationDefaultPermissions, mangled, default_roles)
5✔
161

162

163
def registerPermissions(permissions, defaultDefault=('Manager', )):
5✔
164
    """Register an __ac_permissions__ sequence.
165
    """
166
    for setting in permissions:
5✔
167
        if setting[0] in _registeredPermissions:
5✔
168
            continue
5✔
169
        if len(setting) == 2:
5✔
170
            perm, methods = setting
5✔
171
            default = defaultDefault
5✔
172
        else:
173
            perm, methods, default = setting
5✔
174
        addPermission(perm, default)
5✔
175

176

177
class ApplicationDefaultPermissions:
5✔
178
    _View_Permission = ('Manager', 'Anonymous')
5✔
179
    _Access_contents_information_Permission = ('Manager', 'Anonymous')
5✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc