Skip to content

Commit

Permalink
Merge pull request #19 from zopefoundation/ease-pname-computation
Browse files Browse the repository at this point in the history
Ease the computation of the permission identifier
  • Loading branch information
tseaver authored Sep 29, 2016
2 parents 62bb888 + fbccfa4 commit 501afff
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 21 deletions.
10 changes: 3 additions & 7 deletions src/AccessControl/ImplPython.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,13 @@
from AccessControl.SimpleObjectPolicies import Containers
from AccessControl.SimpleObjectPolicies import _noroles
from AccessControl.ZopeGuards import guarded_getitem
from AccessControl.Permission import getPermissionIdentifier

LOG = getLogger('ImplPython')

# AccessControl.PermissionRole
# ----------------------------

_ident_chars = string.ascii_letters + string.digits + "_"
name_trans = filter(lambda c, an=_ident_chars: c not in an,
map(chr, range(256)))
name_trans = string.maketrans(''.join(name_trans), '_' * len(name_trans))

_default_roles = ('Manager',)

# If _embed_permission_in_roles is enabled, computed __roles__
Expand All @@ -63,7 +59,7 @@
def rolesForPermissionOn(perm, object, default=_default_roles, n=None):
"""Return the roles that have the given permission on the given object
"""
n = n or '_' + string.translate(perm, name_trans) + "_Permission"
n = n or getPermissionIdentifier(perm)
r = None

while 1:
Expand Down Expand Up @@ -134,7 +130,7 @@ class PermissionRole(Base):

def __init__(self, name, default=('Manager',)):
self.__name__ = name
self._p = '_' + string.translate(name, name_trans) + "_Permission"
self._p = getPermissionIdentifier(name)
self._d = self.__roles__ = default

def __of__(self, parent):
Expand Down
18 changes: 9 additions & 9 deletions src/AccessControl/Permission.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@
"""Permissions
"""

import string

from Acquisition import aq_base
import re

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

name_trans=filter(lambda c, an=string.letters+string.digits+'_': c not in an,
map(chr, range(256)))
name_trans=string.maketrans(''.join(name_trans), '_'*len(name_trans))

def getPermissionIdentifier(name):
return '_' + re.sub(_NOT_ALLOWED_CHARS, '_', name) + "_Permission"

def pname(name, translate=string.translate, name_trans=name_trans):
return '_'+translate(name, name_trans) + "_Permission"
# BBB
pname = getPermissionIdentifier

_marker=[]

Expand All @@ -36,7 +36,7 @@ class Permission:

def __init__(self, name, data, obj, default=None):
self.name = name
self._p = '_' + string.translate(name, name_trans) + "_Permission"
self._p = getPermissionIdentifier(name)
self.data = data
self.obj = aq_base(obj)
self.default = default
Expand Down Expand Up @@ -152,7 +152,7 @@ def addPermission(perm, default_roles=('Manager', )):
global _ac_permissions
_ac_permissions = _ac_permissions + entry
_registeredPermissions[perm] = 1
mangled = pname(perm) # get mangled permission name
mangled = getPermissionIdentifier(perm)
if not hasattr(ApplicationDefaultPermissions, mangled):
setattr(ApplicationDefaultPermissions, mangled, default_roles)

Expand Down
10 changes: 5 additions & 5 deletions src/AccessControl/PermissionMapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from AccessControl.class_init import InitializeClass
from AccessControl.interfaces import IPermissionMappingSupport
from AccessControl.owner import UnownableOwner
from AccessControl.Permission import pname
from AccessControl.Permission import getPermissionIdentifier
from AccessControl.requestmethod import requestmethod

@implementer(IPermissionMappingSupport)
Expand All @@ -49,7 +49,7 @@ def manage_getPermissionMapping(self):

perms={}
for p in self.possible_permissions():
perms[pname(p)]=p
perms[getPermissionIdentifier(p)]=p

r=[]
a=r.append
Expand Down Expand Up @@ -106,14 +106,14 @@ def _isBeingUsedAsAMethod(self, REQUEST =None, wannaBe=0):

def getPermissionMapping(name, obj, st=type('')):
obj=getattr(obj, 'aq_base', obj)
name=pname(name)
name=getPermissionIdentifier(name)
r=getattr(obj, name, '')
if type(r) is not st: r=''
return r

def setPermissionMapping(name, obj, v):
name=pname(name)
if v: setattr(obj, name, pname(v))
name=getPermissionIdentifier(name)
if v: setattr(obj, name, getPermissionIdentifier(v))
elif obj.__dict__.has_key(name): delattr(obj, name)

class PM(Base):
Expand Down

0 comments on commit 501afff

Please sign in to comment.