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