[Checkins] SVN: PluggableAuthService/trunk/ Merge z3-events branch

Wichert Akkerman wichert at wiggy.net
Mon Jun 11 10:27:05 EDT 2007


Log message for revision 76609:
  Merge z3-events branch

Changed:
  _U  PluggableAuthService/trunk/
  _U  PluggableAuthService/trunk/Extensions/
  U   PluggableAuthService/trunk/PluggableAuthService.py
  U   PluggableAuthService/trunk/configure.zcml
  U   PluggableAuthService/trunk/doc/CHANGES.txt
  A   PluggableAuthService/trunk/events.py
  A   PluggableAuthService/trunk/events.zcml
  _U  PluggableAuthService/trunk/interfaces/
  _U  PluggableAuthService/trunk/plugins/
  _U  PluggableAuthService/trunk/tests/
  U   PluggableAuthService/trunk/tests/test_UserFolder.py

-=-

Property changes on: PluggableAuthService/trunk
___________________________________________________________________
Name: svn:ignore
   + *.pyc
*.pyo



Property changes on: PluggableAuthService/trunk/Extensions
___________________________________________________________________
Name: svn:ignore
   + *.pyc
*.pyo


Modified: PluggableAuthService/trunk/PluggableAuthService.py
===================================================================
--- PluggableAuthService/trunk/PluggableAuthService.py	2007-06-11 07:46:40 UTC (rev 76608)
+++ PluggableAuthService/trunk/PluggableAuthService.py	2007-06-11 14:27:03 UTC (rev 76609)
@@ -45,18 +45,15 @@
 from ZTUtils import Batch
 from App.class_init import default__class_init__ as InitializeClass
 
-try:
-    from OFS.interfaces import IObjectManager
-    from OFS.interfaces import ISimpleItem
-    from OFS.interfaces import IPropertyManager
-except ImportError: # BBB
-    from Products.Five.interfaces import IObjectManager
-    from Products.Five.interfaces import ISimpleItem
-    from Products.Five.interfaces import IPropertyManager
+from OFS.interfaces import IObjectManager
+from OFS.interfaces import ISimpleItem
+from OFS.interfaces import IPropertyManager
 
 from Products.PluginRegistry.PluginRegistry import PluginRegistry
 import Products
 
+from zope import event
+
 from interfaces.authservice import IPluggableAuthService
 from interfaces.authservice import _noroles
 from interfaces.plugins import IExtractionPlugin
@@ -80,6 +77,8 @@
 from interfaces.plugins import IChallengeProtocolChooser
 from interfaces.plugins import IRequestTypeSniffer
 
+from events import PrincipalCreated
+
 from permissions import SearchPrincipals
 
 from PropertiedUser import PropertiedUser
@@ -945,6 +944,8 @@
                 user = self.getUser( login )
                 break
 
+        # XXX What should we do if no useradder was succesfull?
+
         for roleassigner_id, roleassigner in roleassigners:
             for role in roles:
                 try:
@@ -955,6 +956,10 @@
                                 )
                     pass
 
+        if user is not None:
+            event.notify(PrincipalCreated(user))
+
+
     security.declarePublic('all_meta_types')
     def all_meta_types(self):
         """ What objects can be put in here?
@@ -1093,6 +1098,7 @@
         for updater_id, updater in cred_updaters:
             updater.updateCredentials(request, response, login, new_password)
 
+
     security.declarePublic('logout')
     def logout(self, REQUEST):
         """Publicly accessible method to log out a user

Modified: PluggableAuthService/trunk/configure.zcml
===================================================================
--- PluggableAuthService/trunk/configure.zcml	2007-06-11 07:46:40 UTC (rev 76608)
+++ PluggableAuthService/trunk/configure.zcml	2007-06-11 14:27:03 UTC (rev 76609)
@@ -4,4 +4,6 @@
 
   <include file="exportimport.zcml" />
 
+  <include file="events.zcml" />
+
 </configure>

Modified: PluggableAuthService/trunk/doc/CHANGES.txt
===================================================================
--- PluggableAuthService/trunk/doc/CHANGES.txt	2007-06-11 07:46:40 UTC (rev 76608)
+++ PluggableAuthService/trunk/doc/CHANGES.txt	2007-06-11 14:27:03 UTC (rev 76609)
@@ -4,6 +4,9 @@
 
     Features Added
 
+      - Added events infrastructure. Enabled new IPrincipalCreatedEvent and
+        ICredentialsUpdatedEvent events.
+
       - Added support for registering plugin types via ZCML.
 
       - Implemented authentication caching in _extractUserIds.

Copied: PluggableAuthService/trunk/events.py (from rev 76608, PluggableAuthService/branches/z3-events/events.py)
===================================================================
--- PluggableAuthService/trunk/events.py	                        (rev 0)
+++ PluggableAuthService/trunk/events.py	2007-06-11 14:27:03 UTC (rev 76609)
@@ -0,0 +1,55 @@
+from Acquisition import aq_parent
+from zope.component import adapter
+from zope.component import subscribers
+from zope.interface import implements
+from Products.PluggableAuthService.interfaces.events import *
+
+class PASEvent(object):
+    implements(IPASEvent)
+
+    def __init__(self, principal):
+        self.principal=principal
+        self.object=principal
+
+
+class PrincipalCreated(PASEvent):
+    implements(IPrincipalCreatedEvent)
+
+
+class PrincipalDeleted(PASEvent):
+    implements(IPrincipalDeletedEvent)
+
+
+class CredentialsUpdated(PASEvent):
+    implements(ICredentialsUpdatedEvent)
+
+    def __init__(self, principal, password):
+        super(CredentialsUpdated, self).__init__(principal)
+        self.password=password
+
+
+class PropertiesUpdated(PASEvent):
+    implements(IPropertiesUpdatedEvent)
+
+    def __init__(self, principal, properties):
+        super(CredentialsUpdated, self).__init__(principal)
+        self.properties=properties
+
+
+def userCredentialsUpdatedHandler(principal, event):
+    pas = aq_parent(principal)
+    pas.updateCredentials(
+            pas,
+            pas.REQUEST,
+            pas.REQUEST.RESPONSE,
+            principal.getId(),
+            event.password)
+
+
+ at adapter(IPASEvent)
+def PASEventNotify(event):
+    """Event subscriber to dispatch PASEvent to interested adapters."""
+    adapters = subscribers((event.principal, event), None)
+    for adapter in adapters:
+        pass # getting them does the work
+

Copied: PluggableAuthService/trunk/events.zcml (from rev 76608, PluggableAuthService/branches/z3-events/events.zcml)
===================================================================
--- PluggableAuthService/trunk/events.zcml	                        (rev 0)
+++ PluggableAuthService/trunk/events.zcml	2007-06-11 14:27:03 UTC (rev 76609)
@@ -0,0 +1,13 @@
+<configure
+    xmlns="http://namespaces.zope.org/zope"
+    >
+ 
+   <subscriber handler=".events.PASEventNotify" />
+
+   <subscriber
+       for=".interfaces.authservice.IBasicUser
+            .interfaces.events.ICredentialsUpdatedEvent"
+       handler=".events.userCredentialsUpdatedHandler"
+       />
+
+</configure>


Property changes on: PluggableAuthService/trunk/interfaces
___________________________________________________________________
Name: svn:ignore
   + *.pyc
*.pyo



Property changes on: PluggableAuthService/trunk/plugins
___________________________________________________________________
Name: svn:ignore
   + *.pyc
*.pyo



Property changes on: PluggableAuthService/trunk/tests
___________________________________________________________________
Name: svn:ignore
   + *.pyc
*.pyo


Modified: PluggableAuthService/trunk/tests/test_UserFolder.py
===================================================================
--- PluggableAuthService/trunk/tests/test_UserFolder.py	2007-06-11 07:46:40 UTC (rev 76608)
+++ PluggableAuthService/trunk/tests/test_UserFolder.py	2007-06-11 14:27:03 UTC (rev 76609)
@@ -23,7 +23,21 @@
 
 from Products.PluggableAuthService.PluggableAuthService import PluggableAuthService
 
+from zope import event
+from zope.component import adapter
+from zope.component import provideHandler
+from Products.PluggableAuthService.interfaces.events import IPrincipalCreatedEvent
+from Products.PluggableAuthService.events import CredentialsUpdated
 
+ at adapter(IPrincipalCreatedEvent)
+def userCreatedHandler(event):
+    pas = event.principal.aq_parent
+    if not hasattr(pas, 'events'):
+        pas.events = []
+
+    pas.events.append(event)
+
+
 class UserFolderTests(pastc.PASTestCase):
 
     def afterSetUp(self):
@@ -275,10 +289,48 @@
         self.assertEqual(f.getDomains(), ())
 
 
+class UserEvents(pastc.PASTestCase):
+
+    def afterSetUp(self):
+        # Set up roles and a user
+        self.uf = self.folder.acl_users
+        self.folder._addRole('role1')
+        self.folder.manage_role('role1', [View])
+        self.uf.roles.addRole('role1')
+        self.folder._addRole('role2')
+        self.uf._doAddUser('user1', 'secret', ['role1'], [])
+
+    def testUserCreationEvent(self):
+        provideHandler(userCreatedHandler)
+        self.uf.events = []
+
+        self.uf._doAddUser('event1', 'secret', ['role1'], [])
+
+        self.assertEqual(len(self.uf.events), 1)
+        event = self.uf.events[0]
+        self.failUnless(IPrincipalCreatedEvent.providedBy(event))
+        self.assertEqual(event.principal.getUserName(), 'event1')
+        self.assertEqual(event.principal.getId(), 'event1')
+
+    def testCredentialsEvent(self):
+        def wrap(self, *args):
+            self._data.append(args)
+            return self._original(*args)
+        self.uf._data=[]
+        self.uf._original=self.uf.updateCredentials
+        self.uf.updateCredentials=wrap
+        event.notify(CredentialsUpdated(self.uf.getUserById("user1"), "testpassword"))
+        self.assertEqual(len(self.uf._data), 1)
+        self.assertEqual(self.uf._data[0][2], "user1")
+        self.assertEqual(self.uf._data[0][3], "testpassword")
+
+
+
 def test_suite():
     suite = unittest.TestSuite()
     suite.addTest(unittest.makeSuite(UserFolderTests))
     suite.addTest(unittest.makeSuite(UserTests))
+    suite.addTest(unittest.makeSuite(UserEvents))
     return suite
 
 if __name__ == '__main__':



More information about the Checkins mailing list