Back to index

plone3  3.1.7
security.py
Go to the documentation of this file.
00001 from zope.app.schema.vocabulary import IVocabularyFactory
00002 from zope.interface import implements
00003 from zope.schema.vocabulary import SimpleTerm
00004 from zope.schema.vocabulary import SimpleVocabulary
00005 
00006 from Products.CMFCore.utils import getToolByName
00007 
00008 class RolesVocabulary(object):
00009     """Vocabulary factory for roles in the portal
00010 
00011       >>> from zope.component import queryUtility
00012       >>> from plone.app.vocabularies.tests.base import DummyContext
00013       >>> from plone.app.vocabularies.tests.base import DummyTool
00014 
00015       >>> name = 'plone.app.vocabularies.Roles'
00016       >>> util = queryUtility(IVocabularyFactory, name)
00017       >>> context1 = DummyContext()
00018       >>> context2 = DummyContext()
00019       >>> context1.context = context2
00020 
00021       >>> util(context1) is None
00022       True
00023 
00024       >>> tool = DummyTool('portal_membership')
00025       >>> def getPortalRoles():
00026       ...     return ('Anonymous', 'Manager', 'Ploonies')
00027       >>> tool.getPortalRoles = getPortalRoles
00028       >>> context2.portal_membership = tool
00029 
00030       >>> roles = util(context1)
00031       >>> roles
00032       <zope.schema.vocabulary.SimpleVocabulary object at ...>
00033 
00034       >>> len(roles.by_token)
00035       3
00036 
00037       >>> manager = roles.by_token['Manager']
00038       >>> manager.title, manager.token, manager.value
00039       ('Manager', 'Manager', 'Manager')
00040     """
00041     implements(IVocabularyFactory)
00042 
00043     def __call__(self, context):
00044         context = getattr(context, 'context', context)
00045         mtool = getToolByName(context, 'portal_membership', None)
00046         if mtool is None:
00047             return None
00048         items = list(mtool.getPortalRoles())
00049         items.sort()
00050         items = [SimpleTerm(i, i, i) for i in items]
00051         return SimpleVocabulary(items)
00052 
00053 RolesVocabularyFactory = RolesVocabulary()
00054 
00055 class GroupsVocabulary(object):
00056     """Vocabulary factory for groups in the portal
00057 
00058       >>> from zope.component import queryUtility
00059       >>> from plone.app.vocabularies.tests.base import DummyContext
00060       >>> from plone.app.vocabularies.tests.base import DummyTool
00061 
00062       >>> name = 'plone.app.vocabularies.Groups'
00063       >>> util = queryUtility(IVocabularyFactory, name)
00064       >>> context1 = DummyContext()
00065       >>> context2 = DummyContext()
00066       >>> context1.context = context2
00067 
00068       >>> util(context1) is None
00069       True
00070 
00071       >>> class DummyGroup(object):
00072       ...     def __init__(self, id, name):
00073       ...         self.id = id
00074       ...         self.name = name
00075       ...
00076       ...     def getGroupId(self):
00077       ...         return self.id
00078       ...
00079       ...     def getGroupName(self):
00080       ...         return self.name
00081 
00082       >>> tool = DummyTool('portal_groups')
00083       >>> def listGroups():
00084       ...     return (DummyGroup('editors', 'Editors'),
00085       ...             DummyGroup('viewers', 'Viewers'))
00086       >>> tool.listGroups = listGroups
00087       >>> context2.portal_groups = tool
00088 
00089       >>> groups = util(context1)
00090       >>> groups
00091       <zope.schema.vocabulary.SimpleVocabulary object at ...>
00092 
00093       >>> len(groups.by_token)
00094       2
00095 
00096       >>> editors = groups.by_token['editors']
00097       >>> editors.title, editors.token, editors.value
00098       ('Editors', 'editors', 'editors')
00099     """
00100     implements(IVocabularyFactory)
00101 
00102     def __call__(self, context):
00103         context = getattr(context, 'context', context)
00104         gtool = getToolByName(context, 'portal_groups', None)
00105         if gtool is None:
00106             return None
00107         items = [ (g.getGroupId(), g.getGroupName()) for g in gtool.listGroups() ]
00108         items.sort()
00109         items = [SimpleTerm(i[0], i[0], i[1]) for i in items]
00110         return SimpleVocabulary(items)
00111         
00112 GroupsVocabularyFactory = GroupsVocabulary()