[Zope-Checkins] CVS: Zope3/lib/python/Zope/App/Security - RoleRegistry.py:1.1.2.2

Barry Warsaw barry@wooz.org
Thu, 13 Dec 2001 13:03:09 -0500


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

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

Role class now inherits from RegisteredObject

RoleRegistry 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/RoleRegistry.py 1.1.2.1 => 1.1.2.2 ===
 # FOR A PARTICULAR PURPOSE.
 
-""" Global role registry."""
+"""Global role registry."""
 
+PREFIX = 'Global Role'
+SUFFIX = 'Everybody'
+DESCRIP = 'All users have this role'
+
+from Zope.App.Security.RegisteredObject import RegisteredObject
+from Zope.App.Security.Registry import Registry
 from IRole import IRole
 
-class Role:
+class Role(RegisteredObject):
     __implements__ = IRole
 
-    def __init__(self, title, description):
-        self._title = title
-        self._description = description
-
-    def getTitle(self):
-        return self._title
-
-    def getDescription(self):
-        return self._description
-
-
-# Key is string naming a role, value is a Role object which implements a IRole
-# interface.
-_roles={}
-
-
-def defineRole(name, title=None, description=None):
-    """Define a new role object, register, and return it.
-
-    name is the role name, must be globally unique
-
-    title (optional) is the role title, human readable.  If omitted then
-    the name is used as the title
-
-    description (optional) is human readable
-    """
-    _roles[name] = role = Role(title or name, description or '')
-    return role
- 
-def definedRole(name):
-    """Return true if named role is registered, otherwise return false
-    """
-    return _roles.has_key(name)
-
-_missing = []
-def getRole(name, default=_missing):
-    """Return role object registered as name.
-
-    If no named role is registered, return optional default.  If default
-    is not given, then KeyError is raised.
-    """
-    ret = _roles.get(name, default)
-    if ret is _missing:
-        raise KeyError('No such role: %s' % name)
-    return ret
 
-def _clear(): # Reset, e.g., for unit testing antisepsis
-    _roles.clear()
+class RoleRegistry(Registry):
+    def __init__(self, prefix=PREFIX):
+        Registry.__init__(self, Role)
+        self._prefix = prefix
+
+    def _make_global_id(self, suffix):
+        return self._prefix + '.' + suffix
+
+    def defineRole(self, name, title=None, description=None):
+        """Define a new role object, register, and return it.
+
+        name is the role name, must be globally unique
+
+        title (optional) is the role 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 definedRole(self, name):
+        """Return true if named role is registered, otherwise return false
+        """
+        return self.is_registered(self._make_global_id(name))
+
+    def getRole(self, name):
+        """Return role object registered as name.
+
+        If no named role is registered KeyError is raised.
+        """
+        return self.getRegisteredObject(self._make_global_id(name))
+
+
+registry = RoleRegistry()
+
+Everybody = registry.defineRole(SUFFIX, SUFFIX, DESCRIP)