[Checkins] SVN: z3c.authenticator/trunk/ - Feature: added getUserByLogin to IUserContainer

Adam Groszer agroszer at gmail.com
Tue Jun 9 05:33:45 EDT 2009


Log message for revision 100754:
  - Feature: added getUserByLogin to IUserContainer
  - Added a test for user migration (that they can keep their ID)

Changed:
  U   z3c.authenticator/trunk/CHANGES.txt
  U   z3c.authenticator/trunk/src/z3c/authenticator/README.txt
  U   z3c.authenticator/trunk/src/z3c/authenticator/interfaces.py
  U   z3c.authenticator/trunk/src/z3c/authenticator/user.py

-=-
Modified: z3c.authenticator/trunk/CHANGES.txt
===================================================================
--- z3c.authenticator/trunk/CHANGES.txt	2009-06-09 07:13:00 UTC (rev 100753)
+++ z3c.authenticator/trunk/CHANGES.txt	2009-06-09 09:33:45 UTC (rev 100754)
@@ -5,6 +5,10 @@
 Version 0.7.1 (unreleased)
 --------------------------
 
+- Feature: added getUserByLogin to IUserContainer
+
+- Added a test for user migration (that they can keep their ID)
+
 - ...
 
 

Modified: z3c.authenticator/trunk/src/z3c/authenticator/README.txt
===================================================================
--- z3c.authenticator/trunk/src/z3c/authenticator/README.txt	2009-06-09 07:13:00 UTC (rev 100753)
+++ z3c.authenticator/trunk/src/z3c/authenticator/README.txt	2009-06-09 09:33:45 UTC (rev 100754)
@@ -444,7 +444,7 @@
   ...     provides=interfaces.IFoundPrincipal)
 
 Now we can use them without any other event subscriber or other registration
-in our principal container. Let's add a principal tho this container:
+in our principal container. Let's add a principal to this container:
 
   >>> p = MyUser(u'max', u'password', u'Max', u'', u'max at foobar.com')
   >>> token, max = authPlugin.add(p)
@@ -481,3 +481,43 @@
 
   >>> authenticated.email
   u'max at foobar.com'
+
+Check getUserByLogin:
+
+  >>> authPlugin.getUserByLogin('max')
+  <MyUser object at ...>
+
+  >>> authPlugin.getUserByLogin('max').login
+  u'max'
+
+  >>> authPlugin.getUserByLogin('max').__name__ == token
+  True
+
+
+A handy feature for migration is that you can set your own ``token``.
+Usually in z.a.authentication the ``token`` == login and we want to keep it
+that way, unless you want to iterate through all permissions and whatever.
+Note, the __name__ and the id in the container must be the *SAME* object.
+
+  >>> login = u'migrateduser'
+  >>> p = User(login, u'password', u'John')
+
+Preset the ``token``
+
+  >>> p.__name__ = login
+
+Watch out, we use __setitem__ instead of add(), because add() would kill off
+the preset ``token`` in __name__.
+
+  >>> authPlugin[login] = p
+
+Here we are, the user is set with the non-generated token.
+
+  >>> u'migrateduser' in authPlugin.keys()
+  True
+
+  >>> authPlugin['migrateduser']
+  <z3c.authenticator.user.User object at ...>
+
+  >>> authPlugin.getUserByLogin('migrateduser')
+  <z3c.authenticator.user.User object at ...>

Modified: z3c.authenticator/trunk/src/z3c/authenticator/interfaces.py
===================================================================
--- z3c.authenticator/trunk/src/z3c/authenticator/interfaces.py	2009-06-09 07:13:00 UTC (rev 100753)
+++ z3c.authenticator/trunk/src/z3c/authenticator/interfaces.py	2009-06-09 09:33:45 UTC (rev 100754)
@@ -59,14 +59,14 @@
 
 class IPrincipalRegistryAuthenticatorPlugin(IAuthenticatorPlugin):
     """Principal registry authenticator plugin.
-    
+
     This plugin is a little bit special since principals get returned from
     a IAuthentication source next to the root then any other IAuthenticator.
-    
+
     By defaut this utility returns global principals and the IAuthenticator
     forces to wrap then within a IFoundPrincipal. This allows us to apply
-    local groups to gloal defined principals. 
-    
+    local groups to gloal defined principals.
+
     You can trun of this feature by set allowQueryPrincipal to False.
     Anyway, this is just an optional plugin, you don't have to use it.
     """
@@ -138,12 +138,12 @@
 
 class IAuthenticator(ILogout, IContainer):
     """Authentication utility.
-    
-    The Authenticator supports NOT IAuthenticatorPlugin plugins defined 
-    in zope.app.authentication.interfaces. Because they use and return a 
+
+    The Authenticator supports NOT IAuthenticatorPlugin plugins defined
+    in zope.app.authentication.interfaces. Because they use and return a
     IPrincipalInfo object in the authenticateCredentials method.
-    
-    Note: you have to write your own authenticator plugins because we do not 
+
+    Note: you have to write your own authenticator plugins because we do not
     use the IPrincipalInfo implementation in this authentication module.
     """
 
@@ -160,7 +160,7 @@
         description=_("""Used for extracting credentials.
         Names may be of ids of non-utility ICredentialsPlugins contained in
         the IAuthenticator, or names of registered
-        ICredentialsPlugins utilities.  Contained non-utility ids mask 
+        ICredentialsPlugins utilities.  Contained non-utility ids mask
         utility names."""),
         value_type=zope.schema.Choice(vocabulary='Z3CCredentialsPlugins'),
         default=[],
@@ -171,7 +171,7 @@
         description=_("""Used for converting credentials to principals.
         Names may be of ids of non-utility IAuthenticatorPlugins contained in
         the IAuthenticator, or names of registered
-        IAuthenticatorPlugins utilities.  Contained non-utility ids mask 
+        IAuthenticatorPlugins utilities.  Contained non-utility ids mask
         utility names."""),
         value_type=zope.schema.Choice(vocabulary='Z3CAuthenticatorPlugins'),
         default=[],
@@ -253,18 +253,21 @@
     def add(user):
         """Add a user and returns a the assigned token (principal id)."""
 
+    def getUserByLogin(login):
+        """Return the User object by looking it up by it's login"""
 
+
 # principal interfaces
 class IFoundPrincipal(zope.security.interfaces.IGroupClosureAwarePrincipal):
     """Provides found principal returned by IAuthenticator.getPrincipal.
-    
+
     The only goal of this adapter is to provide a none persistent object which
     we can apply our group of group ids at runtime.
-    
+
     This is needed because there is no way to keep the group of group info
-    in sync if we whould store them in a IGroup implementation as persistent 
+    in sync if we whould store them in a IGroup implementation as persistent
     information.
-    
+
     A found principal gets also created by the IAuthenticators search method
     for users matching the search critaria.
     """
@@ -292,11 +295,11 @@
 
 class IAuthenticatedPrincipal(
     zope.security.interfaces.IGroupClosureAwarePrincipal):
-    """A factory adapting IInternalPrincipal and offering read access to the 
+    """A factory adapting IInternalPrincipal and offering read access to the
     principal.
-    
-    A authenticated principal gets created by the IAuthenticators 
-    authenticateCredentials method for principals matching the credential 
+
+    A authenticated principal gets created by the IAuthenticators
+    authenticateCredentials method for principals matching the credential
     criteria.
     """
 
@@ -324,13 +327,13 @@
 # group interfaces
 class IGroup(zope.security.interfaces.IGroup):
     """IGroup provides the zope.security.interfaces.IGroup.
-    
-    This let us use such IGroups as local registered IEveryoneGroup or 
+
+    This let us use such IGroups as local registered IEveryoneGroup or
     IAuthenticatedGroup utilities.
-    
+
     Note zope.security.interfaces.IGroup implementations are used for store
     IPrincipal ids of other IPrincipal or IGroup objects.
-    
+
     zope.security.interfaces.IGroup implemenations are not used for store
     group of group informations. Group of gorup information are collected
     at runtime by the IAuthentication.getPrincipal method via an adapter
@@ -386,12 +389,12 @@
 
 class IFoundGroup(IFoundPrincipal, zope.security.interfaces.IGroup):
     """IFoundGroup acts as a IFoundPrincipal representing a group.
-    
+
     This group principal is used as IFoundPrincipal adapter which we adhoc
     apply our inherited groups incouding groups of groups. See IFoundPrincipal
     for more information.
-    
-    This means both interface z3c.authenticator.interfaces.IGroupPrincipal and 
+
+    This means both interface z3c.authenticator.interfaces.IGroupPrincipal and
     z3c.authenticator.interfaces.IGroup provide zope.security.interfaces.IGroup.
     """
 
@@ -539,4 +542,3 @@
 # queriable search interfaces
 class IQueriableAuthenticator(ISearchable):
     """Indicates the authenticator provides a search UI for principals."""
-

Modified: z3c.authenticator/trunk/src/z3c/authenticator/user.py
===================================================================
--- z3c.authenticator/trunk/src/z3c/authenticator/user.py	2009-06-09 07:13:00 UTC (rev 100753)
+++ z3c.authenticator/trunk/src/z3c/authenticator/user.py	2009-06-09 09:33:45 UTC (rev 100754)
@@ -31,7 +31,7 @@
 from z3c.authenticator import interfaces
 
 # get the IP addresss only once
-try: 
+try:
   ip = socket.getaddrinfo(socket.gethostname(), 0)[-1][-1][0]
 except:
   ip = '127.0.0.1'
@@ -63,7 +63,7 @@
     passwordManagerName = property(getPasswordManagerName)
 
     def _getPasswordManager(self):
-        return zope.component.getUtility(IPasswordManager, 
+        return zope.component.getUtility(IPasswordManager,
             self.passwordManagerName)
 
     def getPassword(self):
@@ -145,8 +145,8 @@
         ...     print e
         There is no user id token given!
 
-        Probabl we do hav a __name__ during copy/paste, so we have to check 
-        if we get a __parent__ as well 
+        Probably we do have a __name__ during copy/paste, so we have to check
+        if we get a __parent__ as well
 
         >>> user = User()
         >>> user.__name__ = u'usertoken'
@@ -229,6 +229,10 @@
             return None
         return user
 
+    def getUserByLogin(self, login):
+        #don't bother catching KeyError, it's the task of the caller
+        return self[self.__id_by_login[login]]
+
     def queryPrincipal(self, id, default=None):
         user = self.get(id)
         if user is not None:



More information about the Checkins mailing list