Handle recursivity in groups principals
authorThierry Florac <thierry.florac@onf.fr>
Tue, 30 Jun 2015 15:02:36 +0200
changeset 45 4d5db00d12b9
parent 44 b999bd4dd461
child 46 58599ce9e36e
Handle recursivity in groups principals
src/pyams_security/plugin/group.py
--- a/src/pyams_security/plugin/group.py	Tue Jun 30 15:02:07 2015 +0200
+++ b/src/pyams_security/plugin/group.py	Tue Jun 30 15:02:36 2015 +0200
@@ -21,17 +21,20 @@
 from pyams_security.interfaces import IGroupsFolderPlugin, ILocalGroup, IPrincipalsAddedToGroupEvent, \
     IPrincipalsRemovedFromGroupEvent, PrincipalsAddedToGroupEvent, PrincipalsRemovedFromGroupEvent, ISecurityManager
 from zope.lifecycleevent.interfaces import IObjectAddedEvent
+from zope.schema.interfaces import IVocabularyFactory
 
 # import packages
 from BTrees import OOBTree
 from persistent import Persistent
 from pyams_security.principal import PrincipalInfo
+from pyams_utils.registry import query_utility
 from pyams_utils.request import check_request
 from pyramid.events import subscriber
 from zope.container.contained import Contained
 from zope.container.folder import Folder
-from zope.interface import implementer
+from zope.interface import implementer, provider
 from zope.schema.fieldproperty import FieldProperty
+from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm, getVocabularyRegistry
 
 
 @implementer(ILocalGroup)
@@ -65,6 +68,25 @@
                 registry.notify(PrincipalsRemovedFromGroupEvent(self, removed))
 
 
+@provider(IVocabularyFactory)
+class LocalGroupsVocabulary(SimpleVocabulary):
+    """'PyAMS local groups vocabulary"""
+
+    def __init__(self, context=None):
+        terms = []
+        manager = query_utility(ISecurityManager)
+        if manager is not None:
+            for plugin in manager.values():
+                if IGroupsFolderPlugin.providedBy(plugin):
+                    for group in plugin.values():
+                        terms.append(SimpleTerm('{prefix}:{group_id}'.format(prefix=plugin.prefix,
+                                                                             group_id=group.group_id),
+                                                title=group.title))
+        super(LocalGroupsVocabulary, self).__init__(terms)
+
+getVocabularyRegistry().register('PyAMS local groups', LocalGroupsVocabulary)
+
+
 @implementer(IGroupsFolderPlugin)
 class GroupsFolder(Folder):
     """Principals groups folder"""
@@ -107,9 +129,9 @@
             if seen is None:
                 seen = set()
             for principal in (p for p in principals.copy() if p not in seen):
+                seen.add(principal)
                 if principal.startswith(self.prefix + ':'):
                     principals.update(self.get_all_principals(principal, seen))
-                seen.add(principal)
         return principals
 
     def find_principals(self, query):