[Zope3-checkins] SVN: Zope3/trunk/ Added removePrincipalFromAllGroups and principals <-> groups mapping update function

Amos Brocco amos.brocco at mriyasoftware.com
Wed Oct 13 13:24:37 EDT 2004


Log message for revision 28116:
  Added removePrincipalFromAllGroups and principals <-> groups mapping update function
  
  

Changed:
  A   Zope3/trunk/package-includes/groupscontainer-configure.zcml
  A   Zope3/trunk/package-includes/security-configure.zcml
  U   Zope3/trunk/src/zope/app/groupscontainer/groupsfolder.py
  U   Zope3/trunk/src/zope/app/groupscontainer/groupsfolder.txt
  U   Zope3/trunk/src/zope/security/group.py

-=-
Added: Zope3/trunk/package-includes/groupscontainer-configure.zcml
===================================================================
--- Zope3/trunk/package-includes/groupscontainer-configure.zcml	2004-10-13 17:17:20 UTC (rev 28115)
+++ Zope3/trunk/package-includes/groupscontainer-configure.zcml	2004-10-13 17:24:35 UTC (rev 28116)
@@ -0,0 +1 @@
+<include package="zope.app.groupscontainer"/>

Added: Zope3/trunk/package-includes/security-configure.zcml
===================================================================
--- Zope3/trunk/package-includes/security-configure.zcml	2004-10-13 17:17:20 UTC (rev 28115)
+++ Zope3/trunk/package-includes/security-configure.zcml	2004-10-13 17:24:35 UTC (rev 28116)
@@ -0,0 +1 @@
+<include package="zope.security"/>

Modified: Zope3/trunk/src/zope/app/groupscontainer/groupsfolder.py
===================================================================
--- Zope3/trunk/src/zope/app/groupscontainer/groupsfolder.py	2004-10-13 17:17:20 UTC (rev 28115)
+++ Zope3/trunk/src/zope/app/groupscontainer/groupsfolder.py	2004-10-13 17:24:35 UTC (rev 28116)
@@ -42,6 +42,10 @@
             groupListForPrincipal = self.__inverseMapping[principal]
             if name in groupListForPrincipal:
                 groupListForPrincipal.remove(name)
+        for principal in self.__inverseMapping.keys():
+            groupListForPrincipal = self.__inverseMapping[principal]
+            if len(groupListForPrincipal) == 0:
+                del self.__inverseMapping[principal]
         super(BTreeContainer,self).__delitem__(name)
                 
     def __setitem__(self, name, object):
@@ -69,6 +73,33 @@
             return self.__getitem__(groupid).principals
         else:
             return []
+            
+    def removePrincipalFromAllGroups(self, principalId):
+        """Removes a principal from all the groups"""
+        groupsItIsIn = self.getGroupsForPrincipal(principalId)
+        for groupid in groupsItIsIn:
+            self.__getitem__(groupid).removePrincipals(principalId)
+                
+    def getAllMappedPrincipals(self):
+        """Returns all mapped principals"""
+        return list(self.__inverseMapping.keys())
+        
+    def updateMappingForPrincipals(self, *principalsIds):
+        for principalId in principalsIds:
+            self.updateMappingForPrincipal(principalId)
+        
+    def updateMappingForPrincipal(self, principalId):
+        """Updates inverse mapping for principalId -> group"""
+        tmpNewGroupList = []
+        # Search in all groups and add to inverse mapping if the principalId is in 
+        for groupid in self.keys():
+            if self.__getitem__(groupid).containsPrincipal(principalId):
+                tmpNewGroupList.append(groupid)
+        # If the principal disappears from all groups just remove it
+        if len(tmpNewGroupList) == 0:
+            del self.__inverseMapping[principalId]
+        else:
+            self.__inverseMapping[principalId] = tmpNewGroupList
 
     def search(self, query, start=None, batch_size=None):
         """ Search for groups"""

Modified: Zope3/trunk/src/zope/app/groupscontainer/groupsfolder.txt
===================================================================
--- Zope3/trunk/src/zope/app/groupscontainer/groupsfolder.txt	2004-10-13 17:17:20 UTC (rev 28115)
+++ Zope3/trunk/src/zope/app/groupscontainer/groupsfolder.txt	2004-10-13 17:24:35 UTC (rev 28116)
@@ -74,7 +74,6 @@
       >>> notordinarypeople.getGroupsForPrincipal('2')
       []
       
-      
     Now we test the search capabilities, as in IQuerySchemaSearch example:
     
     We're trying to search groups in the notordinarypeople class, so first we get a view:
@@ -121,4 +120,16 @@
       >>> view.results('test')
       [u'robots']
       >>> request.form['test.field.search'] = 'eek'
+      
+      We can also remove a principal from all the groups:
+      >>> notordinarypeople.removePrincipalFromAllGroups('1')
+      >>> notordinarypeople.getGroupsForPrincipal('1')
+      []
+      >>> notordinarypeople.getAllMappedPrincipals()
+      ['0']
+      
+      If we remove the last group there will be no principal mapped anymore:
+      >>> del notordinarypeople['robots']
+      >>> notordinarypeople.getAllMappedPrincipals()
+      []
     

Modified: Zope3/trunk/src/zope/security/group.py
===================================================================
--- Zope3/trunk/src/zope/security/group.py	2004-10-13 17:17:20 UTC (rev 28115)
+++ Zope3/trunk/src/zope/security/group.py	2004-10-13 17:24:35 UTC (rev 28116)
@@ -43,6 +43,7 @@
             if not principalId in self.__principals:
                 tmpNewList.append(principalId)
         self.setPrincipals(tmpNewList)
+        self.__parent__.updateMappingForPrincipals(*principalIds)
 
     def removePrincipals(self, *principalIds):
         tmpNewList = self.__principals
@@ -50,6 +51,7 @@
             if principalId in tmpNewList:
                 tmpNewList.remove(principalId)
         self.setPrincipals(tmpNewList)
+        self.__parent__.updateMappingForPrincipals(*principalIds)
         
     def containsPrincipal(self, principalId):
         return principalId in self.__principals



More information about the Zope3-Checkins mailing list