[Zope-Checkins] CVS: Zope3/lib/python/Zope/App/Security - IAuthenticationService.py:1.1.2.3 PrincipalRegistry.py:1.1.2.5

Guido van Rossum guido@python.org
Thu, 13 Dec 2001 12:50:46 -0500


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

Modified Files:
      Tag: Zope-3x-branch
	IAuthenticationService.py PrincipalRegistry.py 
Log Message:
Add defaultPrincipal() method and associated machinery.

=== Zope3/lib/python/Zope/App/Security/IAuthenticationService.py 1.1.2.2 => 1.1.2.3 ===
         """
 
+    def defaultPrincipal():
+        """
+        Return the id of the default principal, if one is defined;
+        return None if no default principal is defined.
+        """
         
     def unauthorized(id, request):
         """


=== Zope3/lib/python/Zope/App/Security/PrincipalRegistry.py 1.1.2.4 => 1.1.2.5 ===
         return None
 
+    __defaultid = None
+    __defaultObject = None
+
+    def defineDefaultPrincipal(self, principal, title, description=''):
+        id = principal
+        if id in self.__principalsById:
+            raise DuplicateId(id)
+        self.__defaultid = id
+        p = Principal(principal, title, description, '', '')
+        self.__defaultObject = p
+
+    def defaultPrincipal(self):
+        return self.__defaultid
+
     def unauthorized(self, id, request):
         pass
 
     def getPrincipal(self, id):
         r = self.__principalsById.get(id)
-        if r is None: raise NotFoundError(id)
+        if r is None:
+            if id == self.__defaultid:
+                return self.__defaultObject
+            raise NotFoundError(id)
         return r
 
     def getPrincipalByLogin(self, login):
@@ -73,7 +90,7 @@
         if login in self.__principalsByLogin:
             raise DuplicateLogin(login)
 
-        if id in self.__principalsById:
+        if id in self.__principalsById or id == self.__defaultid:
             raise DuplicateId(id)
         
         p = Principal(id, title, description, login, password)