vendor/CMF/1.6.3/CMFCore

view RegistrationTool.py @ 2:4c712d7bd1d7

Added tag 1.6.3 for changeset 1babb9d61518
author Georges Racinet on purity.racinet.fr <georges@racinet.fr>
date Fri, 09 Sep 2011 12:44:00 +0200
parents
children
line source
1 ##############################################################################
2 #
3 # Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved.
4 #
5 # This software is subject to the provisions of the Zope Public License,
6 # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
10 # FOR A PARTICULAR PURPOSE.
11 #
12 ##############################################################################
13 """ Basic user registration tool.
15 $Id$
16 """
18 import re
20 from Globals import InitializeClass
21 from Globals import DTMLFile
22 from OFS.SimpleItem import SimpleItem
23 from AccessControl import ClassSecurityInfo
24 from random import choice
26 from ActionProviderBase import ActionProviderBase
27 from permissions import AddPortalMember
28 from permissions import MailForgottenPassword
29 from permissions import ManagePortal
30 from utils import UniqueObject
31 from utils import _checkPermission
32 from utils import _limitGrantedRoles
33 from utils import getToolByName
34 from utils import _dtmldir
36 from interfaces.portal_registration \
37 import portal_registration as IRegistrationTool
40 class RegistrationTool(UniqueObject, SimpleItem, ActionProviderBase):
42 """ Create and modify users by making calls to portal_membership.
43 """
45 __implements__ = (IRegistrationTool, ActionProviderBase.__implements__)
47 id = 'portal_registration'
48 meta_type = 'CMF Registration Tool'
49 member_id_pattern = ''
50 default_member_id_pattern = "^[A-Za-z][A-Za-z0-9_]*$"
51 _ALLOWED_MEMBER_ID_PATTERN = re.compile(default_member_id_pattern)
53 security = ClassSecurityInfo()
55 manage_options = (ActionProviderBase.manage_options +
56 ({ 'label' : 'Overview', 'action' : 'manage_overview' }
57 ,{ 'label' : 'Configure', 'action' : 'manage_configuration' }
58 ) + SimpleItem.manage_options)
60 #
61 # ZMI methods
62 #
63 security.declareProtected(ManagePortal, 'manage_overview')
64 manage_overview = DTMLFile( 'explainRegistrationTool', _dtmldir )
66 security.declareProtected(ManagePortal, 'manage_configuration')
67 manage_configuration = DTMLFile('configureRegistrationTool', _dtmldir)
69 security.declareProtected(ManagePortal, 'manage_editIDPattern')
70 def manage_editIDPattern(self, pattern, REQUEST=None):
71 """Edit the allowable member ID pattern TTW"""
72 pattern.strip()
74 if len(pattern) > 0:
75 self.member_id_pattern = pattern
76 self._ALLOWED_MEMBER_ID_PATTERN = re.compile(pattern)
77 else:
78 self.member_id_pattern = ''
79 self._ALLOWED_MEMBER_ID_PATTERN = re.compile(
80 self.default_member_id_pattern)
82 if REQUEST is not None:
83 msg = 'Member ID Pattern changed'
84 return self.manage_configuration(manage_tabs_message=msg)
86 security.declareProtected(ManagePortal, 'getIDPattern')
87 def getIDPattern(self):
88 """ Return the currently-used member ID pattern """
89 return self.member_id_pattern
91 security.declareProtected(ManagePortal, 'getDefaultIDPattern')
92 def getDefaultIDPattern(self):
93 """ Return the currently-used member ID pattern """
94 return self.default_member_id_pattern
97 #
98 # 'portal_registration' interface methods
99 #
100 security.declarePublic('isRegistrationAllowed')
101 def isRegistrationAllowed(self, REQUEST):
102 '''Returns a boolean value indicating whether the user
103 is allowed to add a member to the portal.
104 '''
105 return _checkPermission(AddPortalMember, self.aq_inner.aq_parent)
107 security.declarePublic('testPasswordValidity')
108 def testPasswordValidity(self, password, confirm=None):
109 '''If the password is valid, returns None. If not, returns
110 a string explaining why.
111 '''
112 return None
114 security.declarePublic('testPropertiesValidity')
115 def testPropertiesValidity(self, new_properties, member=None):
116 '''If the properties are valid, returns None. If not, returns
117 a string explaining why.
118 '''
119 return None
121 security.declarePublic('generatePassword')
122 def generatePassword(self):
123 """ Generate a valid password.
124 """
125 # we don't use these to avoid typos: OQ0Il1
126 chars = 'ABCDEFGHJKLMNPRSTUVWXYZabcdefghijkmnopqrstuvwxyz23456789'
127 return ''.join( [ choice(chars) for i in range(6) ] )
129 security.declareProtected(AddPortalMember, 'addMember')
130 def addMember(self, id, password, roles=('Member',), domains='',
131 properties=None):
132 '''Creates a PortalMember and returns it. The properties argument
133 can be a mapping with additional member properties. Raises an
134 exception if the given id already exists, the password does not
135 comply with the policy in effect, or the authenticated user is not
136 allowed to grant one of the roles listed (where Member is a special
137 role that can always be granted); these conditions should be
138 detected before the fact so that a cleaner message can be printed.
139 '''
140 if not self.isMemberIdAllowed(id):
141 raise ValueError('The login name you selected is already '
142 'in use or is not valid. Please choose another.')
144 failMessage = self.testPasswordValidity(password)
145 if failMessage is not None:
146 raise ValueError(failMessage)
148 if properties is not None:
149 failMessage = self.testPropertiesValidity(properties)
150 if failMessage is not None:
151 raise ValueError(failMessage)
153 # Limit the granted roles.
154 # Anyone is always allowed to grant the 'Member' role.
155 _limitGrantedRoles(roles, self, ('Member',))
157 membership = getToolByName(self, 'portal_membership')
158 membership.addMember(id, password, roles, domains, properties)
160 member = membership.getMemberById(id)
161 self.afterAdd(member, id, password, properties)
162 return member
164 security.declareProtected(AddPortalMember, 'isMemberIdAllowed')
165 def isMemberIdAllowed(self, id):
166 '''Returns 1 if the ID is not in use and is not reserved.
167 '''
168 if len(id) < 1 or id == 'Anonymous User':
169 return 0
170 if not self._ALLOWED_MEMBER_ID_PATTERN.match( id ):
171 return 0
172 membership = getToolByName(self, 'portal_membership')
173 if membership.getMemberById(id) is not None:
174 return 0
175 return 1
177 security.declarePublic('afterAdd')
178 def afterAdd(self, member, id, password, properties):
179 '''Called by portal_registration.addMember()
180 after a member has been added successfully.'''
181 pass
183 security.declareProtected(MailForgottenPassword, 'mailPassword')
184 def mailPassword(self, forgotten_userid, REQUEST):
185 '''Email a forgotten password to a member. Raises an exception
186 if user ID is not found.
187 '''
188 raise NotImplementedError
190 InitializeClass(RegistrationTool)