Back to index

plone3  3.1.7
security.py
Go to the documentation of this file.
00001 from zope.interface import Interface
00002 from zope.component import adapts
00003 from zope.formlib.form import FormFields
00004 from zope.interface import implements
00005 from zope.schema import Bool
00006 
00007 from Products.CMFCore.utils import getToolByName
00008 from Products.CMFDefault.formlib.schema import SchemaAdapterBase
00009 from Products.CMFPlone import PloneMessageFactory as _
00010 from Products.CMFPlone.interfaces import IPloneSiteRoot
00011 
00012 from form import ControlPanelForm
00013 
00014 class ISecuritySchema(Interface):
00015 
00016     enable_self_reg = Bool(title=_(u'Enable self-registration'),
00017                         description=_(u"Allows users to register themselves "
00018                                       "on the site. If not selected, only site "
00019                                       "managers can add new users."),
00020                         default=False,
00021                         required=False)
00022 
00023     enable_user_pwd_choice = Bool(title=_(u'Let users select their \
00024 own passwords'),
00025                         description=_(u"If not selected, passwords will be "
00026                                        "autogenerated and mailed to users, "
00027                                        "which verifies that they have entered "
00028                                        "a valid email address."),
00029                         default=False,
00030                         required=False)
00031 
00032     enable_user_folders = Bool(title=_(u'Enable User Folders'),
00033                         description=_(u"If selected, home folders "
00034                                        "where users can create content will "
00035                                        "be created when they log in."),
00036                         default=False,
00037                         required=False)
00038 
00039     allow_anon_views_about = Bool(title=_(u"Allow anyone to view 'about' "
00040                                            "information"),
00041                         description=_(u"If not selected only logged-in users "
00042                                        "will be able to view information about "
00043                                        "who created an item and when it was "
00044                                        "modified."),
00045                         default=False,
00046                         required=False)
00047 
00048 
00049 class SecurityControlPanelAdapter(SchemaAdapterBase):
00050 
00051     adapts(IPloneSiteRoot)
00052     implements(ISecuritySchema)
00053 
00054     def __init__(self, context):
00055         super(SecurityControlPanelAdapter, self).__init__(context)
00056         pprop = getToolByName(context, 'portal_properties')
00057         self.pmembership = getToolByName(context, 'portal_membership')
00058         portal_url = getToolByName(context, 'portal_url')
00059         self.portal = portal_url.getPortalObject()
00060         self.context = pprop.site_properties
00061 
00062     def get_enable_self_reg(self):
00063         app_perms = self.portal.rolesOfPermission(permission='Add portal member')
00064         for appperm in app_perms:
00065             if appperm['name'] == 'Anonymous' and \
00066                appperm['selected'] == 'SELECTED':
00067                 return True
00068         return False
00069 
00070     def set_enable_self_reg(self, value):
00071         app_perms = self.portal.rolesOfPermission(permission='Add portal member')
00072         reg_roles = []
00073         for appperm in app_perms:
00074             if appperm['selected'] == 'SELECTED':
00075                 reg_roles.append(appperm['name'])
00076         if value == True and 'Anonymous' not in reg_roles:
00077             reg_roles.append('Anonymous')
00078         if value == False and 'Anonymous' in reg_roles:
00079             reg_roles.remove('Anonymous')
00080 
00081         self.portal.manage_permission('Add portal member', roles=reg_roles,
00082                                       acquire=0)
00083 
00084     enable_self_reg = property(get_enable_self_reg, set_enable_self_reg)
00085 
00086 
00087     def get_enable_user_pwd_choice(self):
00088         if self.portal.validate_email:
00089             return False
00090         else:
00091             return True
00092 
00093     def set_enable_user_pwd_choice(self, value):
00094         if value == True:
00095             self.portal.validate_email = False
00096         else:
00097             self.portal.validate_email = True
00098 
00099     enable_user_pwd_choice = property(get_enable_user_pwd_choice,
00100                                       set_enable_user_pwd_choice)
00101 
00102 
00103     def get_enable_user_folders(self):
00104         return self.pmembership.getMemberareaCreationFlag()
00105 
00106     def set_enable_user_folders(self, value):
00107         self.pmembership.memberareaCreationFlag = value
00108 
00109     enable_user_folders = property(get_enable_user_folders,
00110                                    set_enable_user_folders)
00111 
00112 
00113     def get_allow_anon_views_about(self):
00114         return self.context.site_properties.allowAnonymousViewAbout
00115 
00116     def set_allow_anon_views_about(self, value):
00117         self.context.site_properties.allowAnonymousViewAbout = value
00118 
00119     allow_anon_views_about = property(get_allow_anon_views_about,
00120                                       set_allow_anon_views_about)
00121 
00122 
00123 class SecurityControlPanel(ControlPanelForm):
00124 
00125     form_fields = FormFields(ISecuritySchema)
00126 
00127     label = _("Security settings")
00128     description = _("Security settings for this site.")
00129     form_name = _("Security settings")