Ré-implémentation de setSecurityProfile en utilisant l'API de l'acl_users de zope.
[Plinn.git] / MemberDataTool.py
1 # -*- coding: utf-8 -*-
2 #######################################################################################
3 # Plinn - http://plinn.org #
4 # Copyright (C) 2005-2007 Benoît PIN <benoit.pin@ensmp.fr> #
5 # #
6 # This program is free software; you can redistribute it and/or #
7 # modify it under the terms of the GNU General Public License #
8 # as published by the Free Software Foundation; either version 2 #
9 # of the License, or (at your option) any later version. #
10 # #
11 # This program is distributed in the hope that it will be useful, #
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
14 # GNU General Public License for more details. #
15 # #
16 # You should have received a copy of the GNU General Public License #
17 # along with this program; if not, write to the Free Software #
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #
19 #######################################################################################
20 """ Workflow aware MemberData to provide reviewed member registration.
21
22
23
24 """
25 from AccessControl.interfaces import IUser
26 from Products.CMFCore.interfaces import IMemberDataTool
27 from Globals import InitializeClass
28 from Acquisition import aq_inner, aq_parent, aq_base
29 from AccessControl import ClassSecurityInfo
30 from OFS.SimpleItem import SimpleItem
31 from Products.CMFCore.MemberDataTool import MemberDataTool as BaseTool
32 from Products.CMFCore.MemberDataTool import MemberData as BaseData
33 from Products.CMFCore.MemberDataTool import MemberAdapter as BaseMemberAdapter
34 from zope.component import adapts
35 from zope.component import getUtility
36 from zope.interface import implements
37 from Products.CMFCore.interfaces import IMember
38 from Products.CMFCore.interfaces import IRegistrationTool
39 # from Products.CMFCore.MemberDataTool import CleanupTemp
40 #from Products.CMFCore.utils import getToolByName
41 from Products.CMFCore.CMFCatalogAware import CMFCatalogAware
42 from Products.CMFCore.DynamicType import DynamicType
43 from Products.CMFCore.exceptions import BadRequest
44 from utils import formatFullName
45 from permissions import SetMemberProperties, SetMemberPassword
46
47
48 class MemberDataTool (BaseTool):
49 """ This tool wraps user objects, making them act as Member objects.
50 """
51
52 meta_type = 'Plinn Member Data Tool'
53 ## __implements__ = (IMemberDataTool, ActionProviderBase.__implements__)
54
55 security = ClassSecurityInfo()
56
57 def __init__(self):
58 BaseTool.__init__(self)
59 # Create the default properties.
60 self._setProperty('name', '', 'string')
61 self._setProperty('given_name', '', 'string')
62 self._setProperty('wysiwyg_editor', 'FCK', 'string')
63 self._setProperty('photo_width', 800, 'int')
64
65 def wrapUser(self, u) :
66 wu = super(MemberDataTool, self).wrapUser(u)
67 return wu.__of__(self).__of__(u)
68
69
70 def __bobo_traverse__(self, REQUEST, name):
71 if hasattr(self,name):
72 return getattr(self,name)
73 else:
74 if self._members.has_key(name) :
75 return self.wrapUser(self.acl_users.getUser(name))
76
77 InitializeClass(MemberDataTool)
78
79
80 class MemberAdapter(BaseMemberAdapter, SimpleItem, DynamicType, CMFCatalogAware):
81
82 """Member data adapter.
83 """
84
85 adapts(IUser, IMemberDataTool)
86 implements(IMember)
87
88 portal_type = 'Member Data'
89
90 security = ClassSecurityInfo()
91
92 def __init__(self, user, tool):
93 super(MemberAdapter, self).__init__(user, tool)
94 self.id = self.getId()
95
96 security.declarePublic('getMemberFullName')
97 def getMemberFullName(self, nameBefore=1) :
98 """ Return the best full name representation """
99 memberName = self.getProperty('name', default='')
100 memberGivenName = self.getProperty('given_name', default='')
101 memberId = self.getId()
102 return formatFullName(memberName, memberGivenName, memberId, nameBefore=nameBefore)
103
104 def getMemberSortableFormat(self) :
105 """ Return a specific format of full name for alphabetical sorting """
106 return self.getMemberFullName(nameBefore = 1).lower()
107
108 # security overload
109 security.declareProtected(SetMemberProperties, 'setMemberProperties')
110 def setMemberProperties(self, mapping):
111 super(MemberAdapter, self).setMemberProperties(mapping)
112 self.reindexObject()
113
114 security.declareProtected(SetMemberPassword, 'setMemberPassword')
115 def setMemberPassword(self, password, domains=None) :
116 """ set member password """
117
118 registration = getUtility(IRegistrationTool)
119 failMessage = registration.testPasswordValidity(password)
120 if failMessage is not None :
121 raise BadRequest(failMessage)
122
123 self.setSecurityProfile(password=password, domains=domains)
124
125 security.declarePrivate('setSecurityProfile')
126 def setSecurityProfile(self, password=None, roles=None, domains=None):
127 """Set the user's basic security profile"""
128 u = self.getUser()
129 # This is really hackish. The Zope User API needs methods
130 # for performing these functions.
131 if password is not None:
132 u.setPassword(password)
133 if roles is not None:
134 u.setRoles(roles)
135 if domains is not None:
136 u.setDomains(domains)
137
138 security.declarePrivate('manage_beforeDelete')
139 def manage_beforeDelete(self) :
140 """ uncatalog object """
141 self.unindexObject()
142
143 def _setPortalTypeName(self, pt) :
144 """ Static Dynamic Type ;-) """
145 pass
146
147 # user object interface
148 # overloads to make methods not publishable
149
150 def getUserName(self):
151 return super(MemberAdapter, self).getUserName()
152
153 def getId(self):
154 return super(MemberAdapter, self).getId()
155
156 def getRoles(self):
157 return super(MemberAdapter, self).getRoles()
158
159 def getRolesInContext(self, object):
160 return super(MemberAdapter, self).getRolesInContext(object)
161
162 def getDomains(self):
163 return super(MemberAdapter, self).getDomains()
164
165 def has_role(self, roles, object=None):
166 return super(MemberAdapter, self).has_role(roles, object=None)
167
168 InitializeClass(MemberAdapter)
169
170
171 class MemberData (BaseData, DynamicType, CMFCatalogAware):
172
173 ## __implements__ = IMemberData
174
175 portal_type = 'Member Data'
176
177 security = ClassSecurityInfo()
178 # migré
179 # security.declareProtected(SetMemberPassword, 'setMemberPassword')
180 # def setMemberPassword(self, password, domains=None) :
181 # """ set member password """
182 #
183 # registration = getToolByName(self, 'portal_registration', None)
184 # if registration:
185 # failMessage = registration.testPasswordValidity(password)
186 # if failMessage is not None:
187 # raise 'Bad Request', failMessage
188 #
189 # user_folder = self.acl_users
190 # self.setSecurityProfile(password=password, domains=domains)
191 # if user_folder.meta_type == 'Group User Folder' :
192 # self.changePassword(password)
193
194
195 #migré
196 #XXX restore the previous implementation for GRUF 2 I'll remove that later...
197 # security.declarePrivate('setSecurityProfile')
198 # def setSecurityProfile(self, password=None, roles=None, domains=None):
199 # """Set the user's basic security profile"""
200 # u = self.getUser()
201 # # This is really hackish. The Zope User API needs methods
202 # # for performing these functions.
203 # if password is not None:
204 # u.__ = password
205 # if roles is not None:
206 # u.roles = roles
207 # if domains is not None:
208 # u.domains = domains
209
210 # migré
211 # def getMemberFullName(self, nameBefore=1) :
212 # """ Return the best full name representation """
213 # memberName = self.getProperty('name', default='')
214 # memberGivenName = self.getProperty('given_name', default='')
215 # memberId = self.getProperty('id', default='')
216 # return formatFullName(memberName, memberGivenName, memberId, nameBefore=nameBefore)
217
218 # migré
219 # def getMemberSortableFormat(self) :
220 # """ Return a specific format of full name for alphabetical sorting """
221 # return self.getMemberFullName(nameBefore = 1).lower()
222
223
224 # migré
225 # ## overload default security declaration
226 # security.declareProtected(SetMemberProperties, 'setMemberProperties')
227 # def setMemberProperties(self, mapping):
228 # BaseData.setMemberProperties(self, mapping)
229 # self.reindexObject()
230
231 # migré
232 # security.declarePrivate('manage_beforeDelete')
233 # def manage_beforeDelete(self) :
234 # """ uncatalog object """
235 # self.unindexObject()
236
237 # migré
238 # def _setPortalTypeName(self, pt) :
239 # """ Static Dynamic Type ;-) """
240 # pass
241
242 # migré
243 # # user object interface
244 # # overloads to make methods not publishable
245 #
246 # def getUserName(self):
247 # return BaseData.getUserName(self)
248 #
249 # def getId(self):
250 # return BaseData.getId(self)
251 #
252 # def getRoles(self):
253 # return BaseData.getRoles(self)
254 #
255 # def getRolesInContext(self, object):
256 # return BaseData.getRolesInContext(self, object)
257 #
258 # def getDomains(self):
259 # return BaseData.getDomains(self)
260 #
261 # def has_role(self, roles, object=None):
262 # return BaseData.has_role(self, roles, object=None)
263
264
265
266 InitializeClass(MemberData)