[Zope3-checkins] SVN: Zope3/trunk/src/zope/security/ Added IGroupAwarePrincipal interface.

Amos Brocco amos.brocco at mriyasoftware.com
Thu Oct 14 08:25:32 EDT 2004


Log message for revision 28170:
  Added IGroupAwarePrincipal interface.
  
  Removed unneeded test for Groups.
  
  Removed unneeded subscriber.
  

Changed:
  U   Zope3/trunk/src/zope/security/configure.zcml
  U   Zope3/trunk/src/zope/security/group.py
  U   Zope3/trunk/src/zope/security/interfaces.py
  U   Zope3/trunk/src/zope/security/tests/groups_principals.txt
  D   Zope3/trunk/src/zope/security/tests/grouptest.txt
  U   Zope3/trunk/src/zope/security/tests/test_group.py
  D   Zope3/trunk/src/zope/security/tests/test_groupprincipals.py

-=-
Modified: Zope3/trunk/src/zope/security/configure.zcml
===================================================================
--- Zope3/trunk/src/zope/security/configure.zcml	2004-10-14 12:23:43 UTC (rev 28169)
+++ Zope3/trunk/src/zope/security/configure.zcml	2004-10-14 12:25:29 UTC (rev 28170)
@@ -37,13 +37,9 @@
     />
     
   <subscriber
-    factory="zope.security.group.updateGroupsForPrincipal"
-    for="zope.security.interfaces.IPrincipalCreatedEvent"
+    factory="zope.security.group.setGroupsForPrincipal"
+    for="zope.app.pas.interfaces.IAuthenticatedPrincipalCreated"
     />
   
-    <subscriber
-    factory="zope.security.group.updatePrincipalsFromGroups"
-    for="zope.security.interfaces.IGroupChangedEvent"
-    />
 
 </configure> 

Modified: Zope3/trunk/src/zope/security/group.py
===================================================================
--- Zope3/trunk/src/zope/security/group.py	2004-10-14 12:23:43 UTC (rev 28169)
+++ Zope3/trunk/src/zope/security/group.py	2004-10-14 12:25:29 UTC (rev 28170)
@@ -17,11 +17,14 @@
 
 """
 
-from zope.security.interfaces import IGroup, IGroupChangedEvent, IPrincipalCreatedEvent
+from zope.security.interfaces import IGroup
+from zope.app.pas.interfaces import IAuthenticatedPrincipalCreated
 from persistent import Persistent
-from zope.interface import implements
+from zope.interface import implements, alsoProvides
 from zope.app.groupscontainer.interfaces import IGroupContained, IGroupsFolder
+from zope.security.interfaces import IGroupAwarePrincipal
 from types import StringTypes
+import zope.app.zapi
 from zope.event import notify
 
 
@@ -64,44 +67,20 @@
     def setPrincipals(self, prinlist):
         origprincipals = self.__principals
         self.__principals = prinlist
-        notify(GroupChangedEvent(self, origprincipals))
         
     principals = property(getPrincipals, setPrincipals)
     
-    
-class PrincipalCreatedEvent:
-    """Event on principal creation"""
-    implements(IPrincipalCreatedEvent)
-    
-    def __init__(self, principal):
-        self.principal = principal
-        
-class GroupChangedEvent:
-    """Event on group change"""
-    implements(IGroupChangedEvent)
-    
-    def __init__(self, group, originalids):
-        self.group = group
-        self.originalids = originalids
-        
-        
-def updateGroupsForPrincipal(event):
-    """Updates group informations when a principal is created"""
+
+def setGroupsForPrincipal(event):
+    """Set group information when a principal is created"""
     principal = event.principal
-    groupfolder = zope.app.zapi.getUtility(IGroupsFolder)
-    groups = groupfolder.getGroupsForPrincipal(principal.id)
-    principal.groups = groups
-    
-def updatePrincipalsFromGroups(event):
-    """Updates principals when a group changes"""
-    group = event.group
-    originalids = event.originalids
-    groupfolder = zope.app.zapi.getUtility(IGroupsFolder)
-    for principalid in originalids:
-        authservice = zope.app.zapi.getService(IAuthenticationService)
-        principal = authservice.getPrincipal(principalid)
+    alsoProvides(principal, IGroupAwarePrincipal)
+    principal.groups = []
+    groupfolders = zope.app.zapi.getUtilitiesFor(IGroupsFolder)
+    for name, groupfolder in groupfolders:
         groups = groupfolder.getGroupsForPrincipal(principal.id)
-        principal.groups = groups
+        principal.groups.extend(groups)
+
     
     
     

Modified: Zope3/trunk/src/zope/security/interfaces.py
===================================================================
--- Zope3/trunk/src/zope/security/interfaces.py	2004-10-14 12:23:43 UTC (rev 28169)
+++ Zope3/trunk/src/zope/security/interfaces.py	2004-10-14 12:25:29 UTC (rev 28170)
@@ -267,6 +267,12 @@
         description=_("A detailed description of the principal."),
         required=False)
         
+        
+class IGroupAwarePrincipal(IPrincipal):
+    """Group aware principal interface
+    Extends IPrincipal to contain group information.
+    """
+    
     groups = List(
         title=_("Groups"),
         description=_("List of groups the principal belongs to"),
@@ -317,20 +323,9 @@
         description=_("List of principals which belong to the group"),
         required=False)
 
+
         
-class IPrincipalCreatedEvent(Interface):
-    """ Interface for principal created events"""
-    principal = Attribute(
-                    "The principal that was created")
     
-class IGroupChangedEvent(Interface):
-    """ Interface for group changed event"""
-    group = Attribute(
-                    "The group that was changed")
-    
-    
-        
-    
 
 
 

Modified: Zope3/trunk/src/zope/security/tests/groups_principals.txt
===================================================================
--- Zope3/trunk/src/zope/security/tests/groups_principals.txt	2004-10-14 12:23:43 UTC (rev 28169)
+++ Zope3/trunk/src/zope/security/tests/groups_principals.txt	2004-10-14 12:25:29 UTC (rev 28170)
@@ -1,15 +1,18 @@
 Groups & Principals
-================
+===================
 
 We import the needed modules
     >>> from zope.security.group import Group
+    >>> from zope.security.interfaces import IGroupAwarePrincipal
     >>> from zope.app.groupscontainer.groupsfolder import GroupsFolder
+    >>> from zope.app.groupscontainer.interfaces import IGroupsFolder
     >>> from zope.app.pas.principalplugins import PrincipalFactory
     >>> from zope.publisher.tests.httprequest import TestRequest
+    >>> import zope.app.zapi
 
 We first create a group folder and add some groups:
 
-    >>> groups = GroupsFolder()
+    >>> name, groups = list(zope.app.zapi.getUtilitiesFor(IGroupsFolder))[0]
     >>> geeks = Group(id='geeks', principalsids=['srichter','jim'])
     >>> newbies = Group(id='newbies', principalsids=['amos','claudia'])
     >>> sprinters = Group(id='sprinters', principalsids=['srichter', 'jim', 'amos','claudia'])
@@ -21,6 +24,8 @@
     
 2) Get some principals
     >>> srichter = pf.createAuthenticatedPrincipal('srichter', {} , TestRequest())
+    >>> IGroupAwarePrincipal.providedBy(srichter)
+    True
     >>> groups.getGroupsForPrincipal(srichter.id)
     ['geeks', 'sprinters']
     >>> thomas = pf.createAuthenticatedPrincipal('thomas', {} , TestRequest())
@@ -30,3 +35,12 @@
     >>> groups.getGroupsForPrincipal(amos.id)
     ['sprinters', 'newbies']
     
+3) Now we add a group and check that getGroupsForPrincipal gets updated but 
+   principal group info gets NOT updated:
+    >>> thinkers = Group(id='thinkers', principalsids=['srichter', 'jim', 'thomas','robert'])
+    >>> groups[thinkers.id] = thinkers
+    >>> groups.getGroupsForPrincipal(srichter.id)
+    ['geeks', 'sprinters', 'thinkers']
+    >>> srichter.groups
+    ['geeks', 'sprinters']
+    

Deleted: Zope3/trunk/src/zope/security/tests/grouptest.txt
===================================================================
--- Zope3/trunk/src/zope/security/tests/grouptest.txt	2004-10-14 12:23:43 UTC (rev 28169)
+++ Zope3/trunk/src/zope/security/tests/grouptest.txt	2004-10-14 12:25:29 UTC (rev 28170)
@@ -1,32 +0,0 @@
-Group Implementation
-==================
-
-A group is used to store a list of principals. 
-First we create a new group
-    >>> from zope.security.group import Group
-    >>> foogroup = Group(id='foogroup', title='A typical foo group', description='Foo group', principalsids=['1',  '2', '3', '4', '5'])
-    >>> foogroup.getPrincipals()
-    ['1', '2', '3', '4', '5']
-    
-We can add additional principals with the addPrincipal method:
-    >>> foogroup.addPrincipals('8', '12')
-    >>> foogroup.getPrincipals()
-    ['1', '2', '3', '4', '5', '8', '12']
-    
-It is not possible to have two times the same id:
-    >>> foogroup.addPrincipals('8')
-    >>> foogroup.getPrincipals()
-    ['1', '2', '3', '4', '5', '8', '12']
-    
-Or delete a particular principal:
-    >>> foogroup.removePrincipals('3', '12')
-    >>> foogroup.getPrincipals()
-    ['1', '2', '4', '5', '8']
-    
-To check if a principal belongs to the group we use the containsPrincipal method:
-    >>> foogroup.containsPrincipal('5')
-    True
-    >>> foogroup.containsPrincipal('13')
-    False
-
-    

Modified: Zope3/trunk/src/zope/security/tests/test_group.py
===================================================================
--- Zope3/trunk/src/zope/security/tests/test_group.py	2004-10-14 12:23:43 UTC (rev 28169)
+++ Zope3/trunk/src/zope/security/tests/test_group.py	2004-10-14 12:25:29 UTC (rev 28170)
@@ -19,10 +19,21 @@
 
 import unittest
 from zope.testing.doctest import DocFileSuite
+from zope.app.tests import placelesssetup, ztapi
+import zope.security.group
+import zope.app.groupscontainer.groupsfolder
+import zope.app.groupscontainer.interfaces
+import zope.app.pas.interfaces
 
+def setUp(test):
+    placelesssetup.setUp(test)
+    ztapi.subscribe([zope.app.pas.interfaces.IAuthenticatedPrincipalCreated], None, zope.security.group.setGroupsForPrincipal)
+    groups = zope.app.groupscontainer.groupsfolder.GroupsFolder()
+    ztapi.provideUtility(zope.app.groupscontainer.interfaces.IGroupsFolder, groups)
+
 def test_suite():
     suite = unittest.TestSuite()
-    suite.addTest(DocFileSuite('grouptest.txt'))
+    suite.addTest(DocFileSuite('groups_principals.txt', setUp=setUp, tearDown=placelesssetup.tearDown))
     return suite
         
     

Deleted: Zope3/trunk/src/zope/security/tests/test_groupprincipals.py
===================================================================
--- Zope3/trunk/src/zope/security/tests/test_groupprincipals.py	2004-10-14 12:23:43 UTC (rev 28169)
+++ Zope3/trunk/src/zope/security/tests/test_groupprincipals.py	2004-10-14 12:25:29 UTC (rev 28170)
@@ -1,28 +0,0 @@
-##############################################################################
-#
-# Copyright (c) 2004 Zope Corporation and Contributors.
-# All Rights Reserved.
-#
-# This software is subject to the provisions of the Zope Public License,
-# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
-# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
-# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
-# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
-# FOR A PARTICULAR PURPOSE.
-#
-##############################################################################
-"""Groups folder tests.
-
-$Id: tests_groupprincipals.py 27237 2004-10-12 10:49:00 mriya3 $
-"""
-
-
-import unittest
-from zope.testing.doctest import DocFileSuite
-
-def test_suite():
-    suite = unittest.TestSuite()
-    suite.addTest(DocFileSuite('groups_principals.txt'))
-    return suite
-        
-    



More information about the Zope3-Checkins mailing list