Back to index

plone3  3.1.7
MembershipTool.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved.
00004 #
00005 # This software is subject to the provisions of the Zope Public License,
00006 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
00007 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
00008 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00009 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
00010 # FOR A PARTICULAR PURPOSE.
00011 #
00012 ##############################################################################
00013 """ CMFDefault portal_membership tool.
00014 
00015 $Id: MembershipTool.py 77186 2007-06-28 19:06:19Z yuppie $
00016 """
00017 
00018 from AccessControl import ClassSecurityInfo
00019 from Acquisition import aq_base
00020 from Acquisition import aq_inner
00021 from Acquisition import aq_parent
00022 from Globals import DTMLFile
00023 from Globals import InitializeClass
00024 from zope.interface import implements
00025 
00026 from Products.CMFCore.MembershipTool import MembershipTool as BaseTool
00027 from Products.CMFCore.utils import _checkPermission
00028 from Products.CMFCore.utils import _getAuthenticatedUser
00029 
00030 from Document import addDocument
00031 from interfaces import IMembershipTool
00032 from interfaces.portal_membership \
00033         import portal_membership as z2IMembershipTool
00034 from permissions import ListPortalMembers
00035 from permissions import ManagePortal
00036 from permissions import ManageUsers
00037 from permissions import View
00038 from utils import _dtmldir
00039 
00040 DEFAULT_MEMBER_CONTENT = """\
00041 Default page for %s
00042 
00043   This is the default document created for you when
00044   you joined this community.
00045 
00046   To change the content just select "Edit"
00047   in the Tool Box on the left.
00048 """
00049 
00050 
00051 class MembershipTool(BaseTool):
00052 
00053     """ Implement 'portal_membership' interface using "stock" policies.
00054     """
00055 
00056     implements(IMembershipTool)
00057     __implements__ = (z2IMembershipTool, )
00058 
00059     meta_type = 'Default Membership Tool'
00060     membersfolder_id = 'Members'
00061 
00062     security = ClassSecurityInfo()
00063 
00064     #
00065     #   ZMI methods
00066     #
00067     security.declareProtected( ManagePortal, 'manage_overview' )
00068     manage_overview = DTMLFile( 'explainMembershipTool', _dtmldir )
00069 
00070     security.declareProtected(ManagePortal, 'manage_mapRoles')
00071     manage_mapRoles = DTMLFile('membershipRolemapping', _dtmldir )
00072 
00073     security.declareProtected(ManagePortal, 'manage_setMembersFolderById')
00074     def manage_setMembersFolderById(self, id='', REQUEST=None):
00075         """ ZMI method to set the members folder object by its id.
00076         """
00077         self.setMembersFolderById(id)
00078         if REQUEST is not None:
00079             REQUEST['RESPONSE'].redirect( self.absolute_url()
00080                     + '/manage_mapRoles'
00081                     + '?manage_tabs_message=Members+folder+changed.'
00082                     )
00083 
00084     #
00085     #   'portal_membership' interface methods
00086     #
00087     security.declareProtected( ListPortalMembers, 'getRoster' )
00088     def getRoster(self):
00089         """ Return a list of mappings for 'listed' members.
00090 
00091         If Manager, return a list of all usernames.  The mapping
00092         contains the id and listed variables.
00093         """
00094         isUserManager = _checkPermission(ManageUsers, self)
00095         roster = []
00096         for member in self.listMembers():
00097             if isUserManager or member.listed:
00098                 roster.append({'id':member.getId(),
00099                                'listed':member.listed})
00100         return roster
00101 
00102     security.declareProtected(ManagePortal, 'setMembersFolderById')
00103     def setMembersFolderById(self, id=''):
00104         """ Set the members folder object by its id.
00105         """
00106         self.membersfolder_id = id.strip()
00107 
00108     security.declarePublic('getMembersFolder')
00109     def getMembersFolder(self):
00110         """ Get the members folder object.
00111         """
00112         parent = aq_parent( aq_inner(self) )
00113         members = getattr(parent, self.membersfolder_id, None)
00114         return members
00115 
00116     security.declarePublic('createMemberArea')
00117     def createMemberArea(self, member_id=''):
00118         """ Create a member area for 'member_id' or authenticated user.
00119         """
00120         if not self.getMemberareaCreationFlag():
00121             return None
00122         members = self.getMembersFolder()
00123         if members is None:
00124             return None
00125         if self.isAnonymousUser():
00126             return None
00127         # Note: We can't use getAuthenticatedMember() and getMemberById()
00128         # because they might be wrapped by MemberDataTool.
00129         user = _getAuthenticatedUser(self)
00130         user_id = user.getId()
00131         if member_id in ('', user_id):
00132             member = user
00133             member_id = user_id
00134         else:
00135             if _checkPermission(ManageUsers, self):
00136                 member = self.acl_users.getUserById(member_id, None)
00137                 if member:
00138                     member = member.__of__(self.acl_users)
00139                 else:
00140                     raise ValueError, 'Member %s does not exist' % member_id
00141             else:
00142                 return None
00143         if hasattr( aq_base(members), member_id ):
00144             return None
00145 
00146         # Note: We can't use invokeFactory() to add folder and content because
00147         # the user might not have the necessary permissions.
00148 
00149         # Create Member's home folder.
00150         members.manage_addPortalFolder(id=member_id,
00151                                        title="%s's Home" % member_id)
00152         f = members._getOb(member_id)
00153 
00154         # Grant Ownership and Owner role to Member
00155         f.changeOwnership(member)
00156         f.__ac_local_roles__ = None
00157         f.manage_setLocalRoles(member_id, ['Owner'])
00158 
00159         # Create Member's initial content.
00160         if hasattr(self, 'createMemberContent'):
00161             self.createMemberContent(member=member,
00162                                    member_id=member_id,
00163                                    member_folder=f)
00164         else:
00165             addDocument( f
00166                        , 'index_html'
00167                        , member_id+"'s Home"
00168                        , member_id+"'s front page"
00169                        , "structured-text"
00170                        , (DEFAULT_MEMBER_CONTENT % member_id)
00171                        )
00172 
00173             # Grant Ownership and Owner role to Member
00174             f.index_html.changeOwnership(member)
00175             f.index_html.__ac_local_roles__ = None
00176             f.index_html.manage_setLocalRoles(member_id, ['Owner'])
00177 
00178             f.index_html._setPortalTypeName( 'Document' )
00179             f.index_html.reindexObject()
00180             f.index_html.notifyWorkflowCreated()
00181         return f
00182 
00183     security.declarePublic('createMemberarea')
00184     createMemberarea = createMemberArea
00185 
00186     def getHomeFolder(self, id=None, verifyPermission=0):
00187         """ Return a member's home folder object, or None.
00188         """
00189         if id is None:
00190             member = self.getAuthenticatedMember()
00191             if not hasattr(member, 'getMemberId'):
00192                 return None
00193             id = member.getMemberId()
00194         members = self.getMembersFolder()
00195         if members:
00196             try:
00197                 folder = members._getOb(id)
00198                 if verifyPermission and not _checkPermission(View, folder):
00199                     # Don't return the folder if the user can't get to it.
00200                     return None
00201                 return folder
00202             except (AttributeError, TypeError, KeyError):
00203                 pass
00204         return None
00205 
00206     def getHomeUrl(self, id=None, verifyPermission=0):
00207         """ Return the URL to a member's home folder, or None.
00208         """
00209         home = self.getHomeFolder(id, verifyPermission)
00210         if home is not None:
00211             return home.absolute_url()
00212         else:
00213             return None
00214 
00215 InitializeClass(MembershipTool)