[Zope3-checkins] CVS: Zope3/src/zope/app/services/pluggableauth - __init__.py:1.13 configure.zcml:1.7

Jim Fulton jim at zope.com
Wed Feb 25 07:32:01 EST 2004


Update of /cvs-repository/Zope3/src/zope/app/services/pluggableauth
In directory cvs.zope.org:/tmp/cvs-serv5084/src/zope/app/services/pluggableauth

Modified Files:
	__init__.py configure.zcml 
Log Message:
Fixed breakage in the pluggable auth service.  Presumably, this
breakage was introduced by recent container changes.

Changed to use containment constraints, to use standard container
views, and to get principal container
item names from principal logins. This needs more thought.


=== Zope3/src/zope/app/services/pluggableauth/__init__.py 1.12 => 1.13 ===
--- Zope3/src/zope/app/services/pluggableauth/__init__.py:1.12	Fri Feb 20 17:02:32 2004
+++ Zope3/src/zope/app/services/pluggableauth/__init__.py	Wed Feb 25 07:31:59 2004
@@ -19,6 +19,7 @@
 import sys
 import time
 import random
+import zope.schema
 
 from persistent import Persistent
 from BTrees.IOBTree import IOBTree
@@ -28,8 +29,13 @@
 from zope.app.services.servicenames import Authentication
 from zope.component.interfaces import IViewFactory
 from zope.app.container.ordered import OrderedContainer
+from zope.app.container.constraints import ItemTypePrecondition
+from zope.app.container.constraints import ContainerTypesConstraint
 from zope.app.interfaces.container import IOrderedContainer
 from zope.app.interfaces.container import IAddNotifiable
+from zope.app.interfaces.container import INameChooser
+from zope.app.interfaces.container import IContainerNamesContainer
+from zope.app.interfaces.container import IContained
 from zope.app.interfaces.services.pluggableauth import IUserSchemafied
 from zope.app.interfaces.security import ILoginPassword
 from zope.app.interfaces.services.pluggableauth \
@@ -188,10 +194,31 @@
 
         del self[id]
 
+class IBTreePrincipalSource(
+    ILoginPasswordPrincipalSource,
+    IContainerPrincipalSource,
+    INameChooser,
+    IContainerNamesContainer,
+    ):
+
+    def __setitem__(name, principal):
+        """Add a principal
+
+        The name must be the same as the principal login
+        """
+
+    __setitem__.precondition  = ItemTypePrecondition(IUserSchemafied)
+
+class IBTreePrincipalSourceContained(IContained):
+
+    __parent__ = zope.schema.Field(
+        constraint = ContainerTypesConstraint(IBTreePrincipalSource),
+        )
+
 class BTreePrincipalSource(Persistent, Contained):
     """An efficient, scalable provider of Authentication Principals."""
 
-    implements(ILoginPasswordPrincipalSource, IContainerPrincipalSource)
+    implements(IBTreePrincipalSource)
 
     def __init__(self):
 
@@ -424,21 +451,48 @@
         if user.password == password:
             return user
 
+
+    def checkName(self, name, object):
+        """Check to make sure the name is valid
+
+        Don't allow suplicate names:
+
+        >>> sps = BTreePrincipalSource()
+        >>> prin1 = SimplePrincipal('gandalf', 'shadowfax')
+        >>> sps['gandalf'] = prin1
+        >>> sps.checkName('gandalf', prin1)
+        Traceback (most recent call last):
+        ...
+        LoginNameTaken: gandalf
+
+        """
+        if name in self._numbers_by_login:
+            raise LoginNameTaken(name)
+
     def chooseName(self, name, object):
-        "See zope.app.interfaces.container.INameChooser"
+        """Choose a name for the principal
 
-        store = self._principals_by_number
-        while 1:
-            key = gen_key()
-            if key not in store:
-                return str(key)
-            
+        Always choose the object's existing name:
+
+        >>> sps = BTreePrincipalSource()
+        >>> prin1 = SimplePrincipal('gandalf', 'shadowfax')
+        >>> sps.chooseName(None, prin1)
+        'gandalf'
+
+        """
+        return object.login
+
+class LoginNameTaken(Exception):
+    """A login name is in use
+    """
 
 
 class SimplePrincipal(Persistent, Contained):
     """A no-frills IUserSchemafied implementation."""
 
-    implements(IUserSchemafied)
+    implements(IUserSchemafied, IBTreePrincipalSourceContained)
+
+
 
     def __init__(self, login, password, title='', description=''):
         self.id = ''


=== Zope3/src/zope/app/services/pluggableauth/configure.zcml 1.6 => 1.7 ===
--- Zope3/src/zope/app/services/pluggableauth/configure.zcml:1.6	Thu Dec 18 04:57:14 2003
+++ Zope3/src/zope/app/services/pluggableauth/configure.zcml	Wed Feb 25 07:32:00 2004
@@ -41,7 +41,8 @@
         />
     <require
         permission="zope.ManageServices"
-        interface="zope.app.interfaces.container.IWriteContainer"
+        interface="zope.app.interfaces.container.IWriteContainer
+                   zope.app.interfaces.container.INameChooser"
         />
     <allow
         interface="zope.app.interfaces.services.pluggableauth.IPrincipalSource"




More information about the Zope3-Checkins mailing list