Back to index

plone3  3.1.7
role.py
Go to the documentation of this file.
00001 from OFS.SimpleItem import SimpleItem
00002 
00003 from zope.interface import implements, Interface
00004 from zope.component import adapts
00005 from zope.formlib import form
00006 from zope import schema
00007 
00008 from plone.contentrules.rule.interfaces import IExecutable, IRuleElementData
00009 
00010 from plone.app.contentrules.browser.formhelper import AddForm, EditForm 
00011 
00012 from Acquisition import aq_inner
00013 from Products.CMFCore.utils import getToolByName
00014 from Products.CMFPlone import PloneMessageFactory as _
00015 
00016 class IRoleCondition(Interface):
00017     """Interface for the configurable aspects of a role condition.
00018     
00019     This is also used to create add and edit forms, below.
00020     """
00021     
00022     role_names = schema.Set(title=_(u"Roles"),
00023                             description=_(u"The roles to check for."),
00024                             required=True,
00025                             value_type=schema.Choice(vocabulary="plone.app.vocabularies.Roles"))
00026          
00027 class RoleCondition(SimpleItem):
00028     """The actual persistent implementation of the role condition element.
00029     
00030     Note that we must mix in SimpleItem to keep Zope 2 security happy.
00031     """
00032     implements(IRoleCondition, IRuleElementData)
00033     
00034     role_names = []
00035     element = "plone.conditions.Role"
00036     
00037     @property
00038     def summary(self):
00039         return _(u"Roles are: ${names}", mapping=dict(names=", ".join(self.role_names)))
00040 
00041 class RoleConditionExecutor(object):
00042     """The executor for this condition.
00043     
00044     This is registered as an adapter in configure.zcml
00045     """
00046     implements(IExecutable)
00047     adapts(Interface, IRoleCondition, Interface)
00048          
00049     def __init__(self, context, element, event):
00050         self.context = context
00051         self.element = element
00052         self.event = event
00053 
00054     def __call__(self):
00055         portal_membership = getToolByName(self.context, 'portal_membership', None)
00056         if portal_membership is None:
00057             return False
00058         member = portal_membership.getAuthenticatedMember()
00059         roles_in_context = member.getRolesInContext(aq_inner(self.event.object))
00060         for r in self.element.role_names:
00061             if r in roles_in_context:
00062                 return True
00063         return False
00064         
00065 class RoleAddForm(AddForm):
00066     """An add form for role rule conditions.
00067     """
00068     form_fields = form.FormFields(IRoleCondition)
00069     label = _(u"Add Role Condition")
00070     description = _(u"A role condition can prevent rules from executing unless the current user has a particular role.")
00071     form_name = _(u"Configure element")
00072     
00073     def create(self, data):
00074         c = RoleCondition()
00075         form.applyChanges(c, self.form_fields, data)
00076         return c
00077 
00078 class RoleEditForm(EditForm):
00079     """An edit form for role conditions
00080     """
00081     form_fields = form.FormFields(IRoleCondition)
00082     label = _(u"Add Role Condition")
00083     description = _(u"A role condition can prevent rules from executing unless the current user has a particular role.")
00084     form_name = _(u"Configure element")