[Zope-Checkins] CVS: Zope3/lib/python/Zope/App/Security - PermissionRegistry.py:1.1.2.11

Barry Warsaw barry@wooz.org
Thu, 13 Dec 2001 12:56:32 -0500


Update of /cvs-repository/Zope3/lib/python/Zope/App/Security
In directory cvs.zope.org:/tmp/cvs-serv8201/lib/python/Zope/App/Security

Modified Files:
      Tag: Zope-3x-branch
	PermissionRegistry.py 
Log Message:
Make the registry a class instance, inheriting from Registry class

Permission class now inherits from RegisteredObject

PermissionRegistry instances hold their own prefix for the ids.

Get rid of all module-level functions, now use module global instance
and its methods.


=== Zope3/lib/python/Zope/App/Security/PermissionRegistry.py 1.1.2.10 => 1.1.2.11 ===
 """ Global permission registry."""
 
+PREFIX = 'Global Permission'
+SUFFIX = 'Zope.Public'
+DESCRIP = 'Anybody can do this'
+
+from Zope.App.Security.RegisteredObject import RegisteredObject
+from Zope.App.Security.Registry import Registry
 from IPermission import IPermission
 
-class Permission:
+
+class Permission(RegisteredObject):
     __implements__ = IPermission
 
-    def __init__(self, title, description):
-        self._title = title
-        self._description = description
 
-    def getTitle(self):
-        return self._title
+class PermissionRegistry(Registry):
+    def __init__(self, prefix=PREFIX):
+        Registry.__init__(self, Permission)
+        self._prefix = prefix
 
-    def getDescription(self):
-        return self._description
+    def _make_global_id(self, suffix):
+        return self._prefix + '.' + suffix
 
+    def definePermission(self, name, title=None, description=None):
+        """Define a new permission object, register, and return it.
 
-# Key is string naming permission, value is a permission object which
-# implements a IPermission interface.
-_permissions={}
+        name is the permission name, must be globally unique
 
-PublicPermission = Permission('Zope.Public', 'Anybody can do this')
+        title (optional) is the permission title, human readable.  If omitted
+        then the name is used as the title
 
+        description (optional) is human readable
+        """
+        if title is None:
+            title = name
+        if description is None:
+            description = ''
+        return self.register(self._make_global_id(name), title, description)
 
-def definePermission(name, title=None, description=None):
-    """Define a new permission object, register, and return it.
+    def definedPermission(self, name):
+        """Return true if named permission is registered, otherwise return
+        false
+        """
+        return self.is_registered(self._make_global_id(name))
 
-    name is the permission name, must be globally unique
+    def getPermission(self, name):
+        """Return permission object registered as name.
 
-    title (optional) is the permission title, human readable.  If omitted then
-    the name is used as the title
+        If no named permission is registered KeyError is raised.
 
-    description (optional) is human readable
-    """
-    _permissions[name] = perm = Permission(title or name, description or '')
-    return perm
- 
-def definedPermission(name):
-    """Return true if named permission is registered, otherwise return false
-    """
-    return _permissions.has_key(name)
+        """
+        return self.getRegisteredObject(self._make_global_id(name))
 
-_missing = []
-def getPermission(name, default=_missing):
-    """Return permission object registered as name.
 
-    If no named permission is registered, return optional default.  If default
-    is not given, then KeyError is raised.
-    """
-    ret = _permissions.get(name, default)
-    if ret is _missing:
-        raise KeyError('No such permission: %s' % name)
-    return ret
+registry = PermissionRegistry()
 
-def _clear(): # Reset, e.g., for unit testing antisepsis
-    _permissions.clear()
+PublicPermission = registry.definePermission(SUFFIX, SUFFIX, DESCRIP)