4a432ebec57303d2cbba78cc9853b795c0677d80
[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 utils import formatFullName
44 from permissions import SetMemberProperties, SetMemberPassword
45
46
47 class MemberDataTool (BaseTool):
48 """ This tool wraps user objects, making them act as Member objects.
49 """
50
51 meta_type = 'Plinn Member Data Tool'
52 ## __implements__ = (IMemberDataTool, ActionProviderBase.__implements__)
53
54 security = ClassSecurityInfo()
55
56 def __init__(self):
57 BaseTool.__init__(self)
58 # Create the default properties.
59 self._setProperty('name', '', 'string')
60 self._setProperty('given_name', '', 'string')
61 self._setProperty('wysiwyg_editor', 'FCK', 'string')
62 self._setProperty('photo_width', 800, 'int')
63
64 def wrapUser(self, u) :
65 wu = super(MemberDataTool, self).wrapUser(u)
66 return wu.__of__(self).__of__(u)
67
68
69 def __bobo_traverse__(self, REQUEST, name):
70 if hasattr(self,name):
71 return getattr(self,name)
72 else:
73 if self._members.has_key(name) :
74 return self.wrapUser(self.acl_users.getUser(name))
75
76 InitializeClass(MemberDataTool)
77
78
79 class MemberAdapter(BaseMemberAdapter, SimpleItem, DynamicType, CMFCatalogAware):
80
81 """Member data adapter.
82 """
83
84 adapts(IUser, IMemberDataTool)
85 implements(IMember)
86
87 portal_type = 'Member Data'
88
89 security = ClassSecurityInfo()
90
91 def __init__(self, user, tool):
92 super(MemberAdapter, self).__init__(user, tool)
93 self.id = self.getId()
94
95 security.declarePublic('getMemberFullName')
96 def getMemberFullName(self, nameBefore=1) :
97 """ Return the best full name representation """
98 memberName = self.getProperty('name', default='')
99 memberGivenName = self.getProperty('given_name', default='')
100 memberId = self.getId()
101 return formatFullName(memberName, memberGivenName, memberId, nameBefore=nameBefore)
102
103 def getMemberSortableFormat(self) :
104 """ Return a specific format of full name for alphabetical sorting """
105 return self.getMemberFullName(nameBefore = 1).lower()
106
107 # security overload
108 security.declareProtected(SetMemberProperties, 'setMemberProperties')
109 def setMemberProperties(self, mapping):
110 super(MemberAdapter, self).setMemberProperties(mapping)
111 self.reindexObject()
112
113 security.declareProtected(SetMemberPassword, 'setMemberPassword')
114 def setMemberPassword(self, password, domains=None) :
115 """ set member password """
116
117 registration = getUtility(IRegistrationTool)
118 failMessage = registration.testPasswordValidity(password)
119 if failMessage is not None :
120 raise ValueError(failMessage)
121
122 self.setSecurityProfile(password=password, domains=domains)
123
124 security.declarePrivate('manage_beforeDelete')
125 def manage_beforeDelete(self) :
126 """ uncatalog object """
127 self.unindexObject()
128
129 def _setPortalTypeName(self, pt) :
130 """ Static Dynamic Type ;-) """
131 pass
132
133 # user object interface
134 # overloads to make methods not publishable
135
136 def getUserName(self):
137 return super(MemberAdapter, self).getUserName()
138
139 def getId(self):
140 return super(MemberAdapter, self).getId()
141
142 def getRoles(self):
143 return super(MemberAdapter, self).getRoles()
144
145 def getRolesInContext(self, object):
146 return super(MemberAdapter, self).getRolesInContext(object)
147
148 def getDomains(self):
149 return super(MemberAdapter, self).getDomains()
150
151 def has_role(self, roles, object=None):
152 return super(MemberAdapter, self).has_role(roles, object=None)
153
154 InitializeClass(MemberAdapter)
155
156
157 class MemberData (BaseData, DynamicType, CMFCatalogAware):
158
159 ## __implements__ = IMemberData
160
161 portal_type = 'Member Data'
162
163 security = ClassSecurityInfo()
164 # migré
165 # security.declareProtected(SetMemberPassword, 'setMemberPassword')
166 # def setMemberPassword(self, password, domains=None) :
167 # """ set member password """
168 #
169 # registration = getToolByName(self, 'portal_registration', None)
170 # if registration:
171 # failMessage = registration.testPasswordValidity(password)
172 # if failMessage is not None:
173 # raise 'Bad Request', failMessage
174 #
175 # user_folder = self.acl_users
176 # self.setSecurityProfile(password=password, domains=domains)
177 # if user_folder.meta_type == 'Group User Folder' :
178 # self.changePassword(password)
179
180
181 #XXX restore the previous implementation for GRUF 2 I'll remove that later...
182 security.declarePrivate('setSecurityProfile')
183 def setSecurityProfile(self, password=None, roles=None, domains=None):
184 """Set the user's basic security profile"""
185 u = self.getUser()
186 # This is really hackish. The Zope User API needs methods
187 # for performing these functions.
188 if password is not None:
189 u.__ = password
190 if roles is not None:
191 u.roles = roles
192 if domains is not None:
193 u.domains = domains
194
195 # migré
196 # def getMemberFullName(self, nameBefore=1) :
197 # """ Return the best full name representation """
198 # memberName = self.getProperty('name', default='')
199 # memberGivenName = self.getProperty('given_name', default='')
200 # memberId = self.getProperty('id', default='')
201 # return formatFullName(memberName, memberGivenName, memberId, nameBefore=nameBefore)
202
203 # migré
204 # def getMemberSortableFormat(self) :
205 # """ Return a specific format of full name for alphabetical sorting """
206 # return self.getMemberFullName(nameBefore = 1).lower()
207
208
209 # migré
210 # ## overload default security declaration
211 # security.declareProtected(SetMemberProperties, 'setMemberProperties')
212 # def setMemberProperties(self, mapping):
213 # BaseData.setMemberProperties(self, mapping)
214 # self.reindexObject()
215
216 # migré
217 # security.declarePrivate('manage_beforeDelete')
218 # def manage_beforeDelete(self) :
219 # """ uncatalog object """
220 # self.unindexObject()
221
222 # migré
223 # def _setPortalTypeName(self, pt) :
224 # """ Static Dynamic Type ;-) """
225 # pass
226
227 # migré
228 # # user object interface
229 # # overloads to make methods not publishable
230 #
231 # def getUserName(self):
232 # return BaseData.getUserName(self)
233 #
234 # def getId(self):
235 # return BaseData.getId(self)
236 #
237 # def getRoles(self):
238 # return BaseData.getRoles(self)
239 #
240 # def getRolesInContext(self, object):
241 # return BaseData.getRolesInContext(self, object)
242 #
243 # def getDomains(self):
244 # return BaseData.getDomains(self)
245 #
246 # def has_role(self, roles, object=None):
247 # return BaseData.has_role(self, roles, object=None)
248
249
250
251 InitializeClass(MemberData)