[Zope-Checkins] CVS: Zope/lib/python/AccessControl - PermissionRole.py:1.20

Jim Fulton cvs-admin at zope.org
Fri Nov 28 11:43:54 EST 2003


Update of /cvs-repository/Zope/lib/python/AccessControl
In directory cvs.zope.org:/tmp/cvs-serv3393/lib/python/AccessControl

Modified Files:
	PermissionRole.py 
Log Message:
Permission roles now have a rolesForPermissionOn method for computing
roles when role-computation isn't triggered through attribute accesses.

Temporarily disabled C permision-role implementation.


=== Zope/lib/python/AccessControl/PermissionRole.py 1.19 => 1.20 ===
--- Zope/lib/python/AccessControl/PermissionRole.py:1.19	Thu Oct 23 21:21:48 2003
+++ Zope/lib/python/AccessControl/PermissionRole.py	Fri Nov 28 11:43:53 2003
@@ -30,7 +30,7 @@
         _use_python_impl = 1
 
 
-if _use_python_impl:
+if 1 or _use_python_impl:
 
     import sys
 
@@ -38,18 +38,59 @@
 
     import string
 
-    name_trans=filter(lambda c, an=string.letters+string.digits+'_': c not in an,
+    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 rolesForPermissionOn(perm, object, default=('Manager',)):
+    def rolesForPermissionOn(perm, obj, default=('Manager',), n=None):
         """Return the roles that have the given permission on the given object
         """
-        im=imPermissionRole()
-        im._p='_'+string.translate(perm, name_trans)+"_Permission"
-        im._d=default
-        return im.__of__(object)
 
+        n = n or '_'+string.translate(perm, name_trans)+"_Permission"
+        r = None
+        
+        while 1:
+            if hasattr(obj, n):
+                roles = getattr(obj, n)
+                if roles is None:
+                    return 'Anonymous',
+
+                t = type(roles)
+                if t is tuple:
+                    # If we get a tuple, then we don't acquire
+                    if r is None:
+                        return roles
+                    return r+list(roles)
+
+                if t is str:
+                    # We found roles set to a name.  Start over
+                    # with the new permission name.  If the permission
+                    # name is '', then treat as private!
+                    if roles:
+                        if roles != n:
+                            n = roles
+                        # If we find a name that is the same as the
+                        # current name, we just ignore it.
+                        roles = None
+                    else:
+                        return _what_not_even_god_should_do
+
+                elif roles:
+                    if r is None:
+                        r = list(roles)
+                    else: r = r + list(roles)
+
+            obj = getattr(obj, 'aq_inner', None)
+            if obj is None:
+                break
+            obj = obj.aq_parent
+
+        if r is None:
+            return default
+
+        return r
 
     class PermissionRole(Base):
         """Implement permission-based roles.
@@ -77,6 +118,8 @@
             else:
                 return r
 
+        def rolesForPermissionOn(self, value):
+            return rolesForPermissionOn(None, value, self._d, self._p)
 
     # This is used when a permission maps explicitly to no permission.
     _what_not_even_god_should_do=[]
@@ -85,51 +128,13 @@
         """Implement permission-based roles
         """
 
-        def __of__(self, parent,tt=type(()),st=type(''),ut=type(u''),
-                   getattr=getattr):
-            obj=parent
-            n=self._p
-            r=None
-            while 1:
-                if hasattr(obj,n):
-                    roles=getattr(obj, n)
-
-                    if roles is None: return 'Anonymous',
-
-                    t=type(roles)
-
-                    if t is tt:
-                        # If we get a tuple, then we don't acquire
-                        if r is None: return roles
-                        return r+list(roles)
-
-                    if t in (st, ut):
-                        # We found roles set to a name.  Start over
-                        # with the new permission name.  If the permission
-                        # name is '', then treat as private!
-                        if roles:
-                            if roles != n:
-                                n=roles
-                            # If we find a name that is the same as the
-                            # current name, we just ignore it.
-                            roles=None
-                        else:
-                            return _what_not_even_god_should_do
-
-                    elif roles:
-                        if r is None: r=list(roles)
-                        else: r=r+list(roles)
-
-                obj=getattr(obj, 'aq_inner', None)
-                if obj is None: break
-                obj=obj.aq_parent
-
-            if r is None: r=self._d
-
-            return r
-
-        # The following methods are needed in the unlikely case that an unwrapped
-        # object is accessed:
+        def __of__(self, value):
+            return rolesForPermissionOn(None, value, self._d, self._p)
+        rolesForPermissionOn = __of__
+
+        # The following methods are needed in the unlikely case that
+        # an unwrapped object is accessed:
+        
         def __getitem__(self, i):
             try:
                 v=self._v




More information about the Zope-Checkins mailing list