Back to index

plone3  3.1.7
group.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 IGroupCondition(Interface):
00017     """Interface for the configurable aspects of a group condition.
00018     
00019     This is also used to create add and edit forms, below.
00020     """
00021     
00022     group_names = schema.Set(title=_(u"Group name"),
00023                              description=_(u"The name of the group."),
00024                              required=True,
00025                              value_type=schema.Choice(vocabulary="plone.app.vocabularies.Groups"))
00026          
00027 class GroupCondition(SimpleItem):
00028     """The actual persistent implementation of the group condition element.
00029     
00030     Note that we must mix in SimpleItem to keep Zope 2 security happy.
00031     """
00032     implements(IGroupCondition, IRuleElementData)
00033     
00034     group_names = []
00035     element = "plone.conditions.Group"
00036     
00037     @property
00038     def summary(self):
00039         return _(u"Groups are: ${names}", mapping=dict(names=", ".join(self.group_names)))
00040         
00041 class GroupConditionExecutor(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, IGroupCondition, 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         portal_groups = getToolByName(self.context, 'portal_groups', None)
00057         if portal_groups is None or portal_groups is None:
00058             return False
00059         member = portal_membership.getAuthenticatedMember()
00060         groupIds = [g.getId() for g in portal_groups.getGroupsByUserId(member.getId())]
00061         for g in self.element.group_names:
00062             if g in groupIds:
00063                 return True
00064         return False
00065         
00066 class GroupAddForm(AddForm):
00067     """An add form for group rule conditions.
00068     """
00069     form_fields = form.FormFields(IGroupCondition)
00070     label = _(u"Add Group Condition")
00071     description = _(u"A group condition can prevent a rule from executing unless the current user is a member of a particular group.")
00072     form_name = _(u"Configure element")
00073     
00074     def create(self, data):
00075         c = GroupCondition()
00076         form.applyChanges(c, self.form_fields, data)
00077         return c
00078 
00079 class GroupEditForm(EditForm):
00080     """An edit form for group conditions
00081     """
00082     form_fields = form.FormFields(IGroupCondition)
00083     label = _(u"Edit Group Condition")
00084     description = _(u"A group condition can prevent a rule from executing unless the current user is a member of a particular group.")
00085     form_name = _(u"Configure element")