src/pyams_security/plugin/group.py
changeset 45 4d5db00d12b9
parent 42 07229ac2497b
child 72 6dd8bc7bb7b5
equal deleted inserted replaced
44:b999bd4dd461 45:4d5db00d12b9
    19 
    19 
    20 # import interfaces
    20 # import interfaces
    21 from pyams_security.interfaces import IGroupsFolderPlugin, ILocalGroup, IPrincipalsAddedToGroupEvent, \
    21 from pyams_security.interfaces import IGroupsFolderPlugin, ILocalGroup, IPrincipalsAddedToGroupEvent, \
    22     IPrincipalsRemovedFromGroupEvent, PrincipalsAddedToGroupEvent, PrincipalsRemovedFromGroupEvent, ISecurityManager
    22     IPrincipalsRemovedFromGroupEvent, PrincipalsAddedToGroupEvent, PrincipalsRemovedFromGroupEvent, ISecurityManager
    23 from zope.lifecycleevent.interfaces import IObjectAddedEvent
    23 from zope.lifecycleevent.interfaces import IObjectAddedEvent
       
    24 from zope.schema.interfaces import IVocabularyFactory
    24 
    25 
    25 # import packages
    26 # import packages
    26 from BTrees import OOBTree
    27 from BTrees import OOBTree
    27 from persistent import Persistent
    28 from persistent import Persistent
    28 from pyams_security.principal import PrincipalInfo
    29 from pyams_security.principal import PrincipalInfo
       
    30 from pyams_utils.registry import query_utility
    29 from pyams_utils.request import check_request
    31 from pyams_utils.request import check_request
    30 from pyramid.events import subscriber
    32 from pyramid.events import subscriber
    31 from zope.container.contained import Contained
    33 from zope.container.contained import Contained
    32 from zope.container.folder import Folder
    34 from zope.container.folder import Folder
    33 from zope.interface import implementer
    35 from zope.interface import implementer, provider
    34 from zope.schema.fieldproperty import FieldProperty
    36 from zope.schema.fieldproperty import FieldProperty
       
    37 from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm, getVocabularyRegistry
    35 
    38 
    36 
    39 
    37 @implementer(ILocalGroup)
    40 @implementer(ILocalGroup)
    38 class Group(Persistent, Contained):
    41 class Group(Persistent, Contained):
    39     """Local group persistent class"""
    42     """Local group persistent class"""
    61                 registry.notify(PrincipalsAddedToGroupEvent(self, added))
    64                 registry.notify(PrincipalsAddedToGroupEvent(self, added))
    62             if removed:
    65             if removed:
    63                 logger.debug("Removed principals {0} from group {1} ({2})".format(str(removed), self.group_id,
    66                 logger.debug("Removed principals {0} from group {1} ({2})".format(str(removed), self.group_id,
    64                                                                                   self.title))
    67                                                                                   self.title))
    65                 registry.notify(PrincipalsRemovedFromGroupEvent(self, removed))
    68                 registry.notify(PrincipalsRemovedFromGroupEvent(self, removed))
       
    69 
       
    70 
       
    71 @provider(IVocabularyFactory)
       
    72 class LocalGroupsVocabulary(SimpleVocabulary):
       
    73     """'PyAMS local groups vocabulary"""
       
    74 
       
    75     def __init__(self, context=None):
       
    76         terms = []
       
    77         manager = query_utility(ISecurityManager)
       
    78         if manager is not None:
       
    79             for plugin in manager.values():
       
    80                 if IGroupsFolderPlugin.providedBy(plugin):
       
    81                     for group in plugin.values():
       
    82                         terms.append(SimpleTerm('{prefix}:{group_id}'.format(prefix=plugin.prefix,
       
    83                                                                              group_id=group.group_id),
       
    84                                                 title=group.title))
       
    85         super(LocalGroupsVocabulary, self).__init__(terms)
       
    86 
       
    87 getVocabularyRegistry().register('PyAMS local groups', LocalGroupsVocabulary)
    66 
    88 
    67 
    89 
    68 @implementer(IGroupsFolderPlugin)
    90 @implementer(IGroupsFolderPlugin)
    69 class GroupsFolder(Folder):
    91 class GroupsFolder(Folder):
    70     """Principals groups folder"""
    92     """Principals groups folder"""
   105         principals = principals.copy()
   127         principals = principals.copy()
   106         if principals:
   128         if principals:
   107             if seen is None:
   129             if seen is None:
   108                 seen = set()
   130                 seen = set()
   109             for principal in (p for p in principals.copy() if p not in seen):
   131             for principal in (p for p in principals.copy() if p not in seen):
       
   132                 seen.add(principal)
   110                 if principal.startswith(self.prefix + ':'):
   133                 if principal.startswith(self.prefix + ':'):
   111                     principals.update(self.get_all_principals(principal, seen))
   134                     principals.update(self.get_all_principals(principal, seen))
   112                 seen.add(principal)
       
   113         return principals
   135         return principals
   114 
   136 
   115     def find_principals(self, query):
   137     def find_principals(self, query):
   116         if not self.enabled:
   138         if not self.enabled:
   117             raise StopIteration
   139             raise StopIteration