[Checkins] SVN: Products.PluggableAuthService/branches/maurits-login-transform/Products/PluggableAuthService/ Add login_transform property to PAS.

Maurits van Rees cvs-admin at zope.org
Thu Jan 3 09:51:26 UTC 2013


Log message for revision 128986:
  Add login_transform property to PAS.
  
  Use this in the relevant methods of PAS.

Changed:
  U   Products.PluggableAuthService/branches/maurits-login-transform/Products/PluggableAuthService/PluggableAuthService.py
  U   Products.PluggableAuthService/branches/maurits-login-transform/Products/PluggableAuthService/tests/test_exportimport.py

-=-
Modified: Products.PluggableAuthService/branches/maurits-login-transform/Products/PluggableAuthService/PluggableAuthService.py
===================================================================
--- Products.PluggableAuthService/branches/maurits-login-transform/Products/PluggableAuthService/PluggableAuthService.py	2013-01-01 21:40:00 UTC (rev 128985)
+++ Products.PluggableAuthService/branches/maurits-login-transform/Products/PluggableAuthService/PluggableAuthService.py	2013-01-03 09:51:26 UTC (rev 128986)
@@ -181,6 +181,18 @@
 
     maxlistusers = -1   # Don't allow local role form to try to list us!
 
+    # Method for transforming a login name.  This needs to be the name
+    # of a method on this plugin.  See the applyTransform method.
+    login_transform = ''
+
+    _properties = (
+        dict(id='title', type='string', mode='w',
+             label='Title'),
+        dict(id='login_transform', type='string', mode='w',
+             label='Transform to apply to login name'),
+             )
+
+
     def getId( self ):
 
         return self._id
@@ -195,6 +207,7 @@
         """
         plugins = self._getOb( 'plugins' )
 
+        name = self.applyTransform( name )
         user_info = self._verifyUser( plugins, login=name )
 
         if not user_info:
@@ -294,7 +307,8 @@
         if search_name:
             if kw.get('id') is not None:
                 del kw['id'] # don't even bother searching by id
-            kw['login'] = kw['name']
+            # XXX name can be a list, I think.
+            kw['login'] = self.applyTransform( kw['name'] )
 
         plugins = self._getOb( 'plugins' )
         enumerators = plugins.listPlugins( IUserEnumerationPlugin )
@@ -400,9 +414,14 @@
             if not kw.has_key('title'):
                 kw['title'] = search_name
             kw['login'] = search_name
-
+            
+        # For groups we search the original name
+        # (e.g. Administrators), for users we apply the transform,
+        # which could lowercase the name.
+        groups = [ d.copy() for d in self.searchGroups( **kw ) ]
+        if search_name:
+            kw['login'] = self.applyTransform( search_name )
         users = [ d.copy() for d in self.searchUsers( **kw ) ]
-        groups = [ d.copy() for d in self.searchGroups( **kw ) ]
 
         if groups_first:
             result = groups + users
@@ -613,6 +632,7 @@
                     return [ ( user_id, name ) ]
 
                 # Now see if the user ids can be retrieved from the cache
+                credentials['login'] = self.applyTransform( credentials.get('login') )
                 view_name = createViewName('_extractUserIds',
                                            credentials.get('login'))
                 keywords = createKeywords(**credentials)
@@ -723,6 +743,7 @@
 
         """ Allow IUserFactoryPlugins to create, or fall back to default.
         """
+        name = self.applyTransform( name )
         factories = plugins.listPlugins( IUserFactoryPlugin )
 
         for factory_id, factory in factories:
@@ -744,6 +765,7 @@
 
         # See if the user can be retrieved from the cache
         view_name = createViewName('_findUser', user_id)
+        name = self.applyTransform( name )
         keywords = createKeywords(user_id=user_id, name=name)
         user = self.ZCacheable_get( view_name=view_name
                                   , keywords=keywords
@@ -805,7 +827,7 @@
             criteria[ 'id' ] = user_id
 
         if login is not None:
-            criteria[ 'login' ] = login
+            criteria[ 'login' ] = self.applyTransform( login )
 
         view_name = createViewName('_verifyUser', user_id or login)
         keywords = createKeywords(**criteria)
@@ -969,6 +991,7 @@
         roleassigners = plugins.listPlugins( IRoleAssignerPlugin )
 
         user = None
+        login = self.applyTransform( login )
 
         if not (useradders and roleassigners):
             raise NotImplementedError( "There are no plugins"
@@ -1041,6 +1064,49 @@
         resp._unauthorized = self._unauthorized
         resp._has_challenged = False
 
+    security.declarePublic( 'applyTransform' )
+    def applyTransform( self, value ):
+        """ Transform for login name.
+
+        Possibly transform the login, for example by making it lower
+        case.
+
+        value must be a string (or unicode).
+
+        TODO: maybe allow lists/tuples/sets and iterate over them.
+        """
+        login_transform = getattr(self, 'login_transform', None)
+        if not login_transform:
+            return value
+        if not value:
+            return value
+        transform = getattr(self, login_transform.strip(), None)
+        if transform is None:
+            LOG.debug("Transform method %r not found in plugin %r.",
+                      self.login_transform, self)
+            return value
+        return transform(value)
+
+    security.declarePublic( 'lower' )
+    def lower( self, value ):
+        """ Transform for login name.
+
+        Strip the value and lowercase it.
+
+        To use this, set login_tranform to 'lower'.
+        """
+        return value.strip().lower()
+
+    security.declarePublic( 'upper' )
+    def upper( self, value ):
+        """ Transform for login name.
+
+        Strip the value and uppercase it.
+
+        To use this, set login_tranform to 'upper'.
+        """
+        return value.strip().upper()
+
     #
     # Response override
     #
@@ -1133,6 +1199,7 @@
         but the credentials are not stored in the CookieAuthHelper cookie
         but somewhere else, like in a Session.
         """
+        login = self.applyTransform(login)
         plugins = self._getOb('plugins')
         cred_updaters = plugins.listPlugins(ICredentialsUpdatePlugin)
 

Modified: Products.PluggableAuthService/branches/maurits-login-transform/Products/PluggableAuthService/tests/test_exportimport.py
===================================================================
--- Products.PluggableAuthService/branches/maurits-login-transform/Products/PluggableAuthService/tests/test_exportimport.py	2013-01-01 21:40:00 UTC (rev 128985)
+++ Products.PluggableAuthService/branches/maurits-login-transform/Products/PluggableAuthService/tests/test_exportimport.py	2013-01-03 09:51:26 UTC (rev 128986)
@@ -128,9 +128,10 @@
             self.assertEqual(filename, 'PAS/.properties')
             self.assertEqual(content_type, 'text/plain')
             lines = filter(None, [x.strip() for x in text.splitlines()])
-            self.assertEqual(len(lines), 2)
+            self.assertEqual(len(lines), 3)
             self.assertEqual(lines[0], '[DEFAULT]')
             self.assertEqual(lines[1], 'title =')
+            self.assertEqual(lines[2], 'login_transform =')
 
             filename, text, content_type = context._wrote[2]
             self.assertEqual(filename, 'PAS/pluginregistry.xml')
@@ -173,9 +174,10 @@
             self.assertEqual(filename, 'PAS/.properties')
             self.assertEqual(content_type, 'text/plain')
             lines = filter(None, [x.strip() for x in text.splitlines()])
-            self.assertEqual(len(lines), 2)
+            self.assertEqual(len(lines), 3)
             self.assertEqual(lines[0], '[DEFAULT]')
             self.assertEqual(lines[1], 'title =')
+            self.assertEqual(lines[2], 'login_transform =')
 
             filename, text, content_type = context._wrote[2]
             self.assertEqual(filename, 'PAS/pluginregistry.xml')



More information about the checkins mailing list