8e82159dae9fcd36cce80e0898c64c0b1333c6f6
[Plinn.git] / Products / Plinn / skins / custom_generic / join_form.py
1 ##parameters=member_id='', given_name='', name='', member_email='', password='', confirm='', add='', ajax=''
2 ##
3 from Products.CMFCore.utils import getToolByName
4 from Products.CMFDefault.permissions import ManageUsers
5 from Products.Plinn.RegistrationTool import MODE_ANONYMOUS, MODE_REVIEWED
6 from Products.Plinn.utils import translate
7 def _(message) : return translate(message, context).encode('utf-8')
8
9 mtool = getToolByName(script, 'portal_membership')
10 ptool = getToolByName(script, 'portal_properties')
11 rtool = getToolByName(script, 'portal_registration')
12 atool = getToolByName(script, 'portal_actions')
13 utool = getToolByName(script, 'portal_url')
14 portal_url = utool()
15 validate_email = ptool.getProperty('validate_email')
16 is_anon = mtool.isAnonymousUser()
17 is_newmember = False
18 is_usermanager = mtool.checkPermission(ManageUsers, mtool)
19
20 form = context.REQUEST.form
21 email_as_login = bool(form.get('email_as_login', True if context.REQUEST.method == 'GET' else False))
22
23 if add :
24 if validate_email :
25 password = confirm = rtool.generatePassword()
26 ok = True
27 else :
28 ok = context.validatePassword(**form)
29 if ok :
30 try :
31 if email_as_login :
32 member_id = member_email
33
34 rtool.addMember(id=member_id, password=password,
35 properties={'username': member_id,
36 'given_name' : given_name,
37 'name' : name,
38 'email': member_email})
39 if validate_email :
40 rtool.requestPasswordReset(member_id, initial=True)
41 context.setStatus(True, _('Success!'))
42 is_newmember = True
43 is_anon = False
44 except ValueError, errmsg:
45 context.setStatus(False, errmsg)
46
47 options = {}
48 options['member_id'] = member_id
49 options['given_name'] = given_name
50 options['name'] = name
51 options['member_email'] = member_email
52 options['email_as_login'] = email_as_login
53 options['password'] = is_newmember and context.REQUEST.get('password', '') or ''
54 options['portal_url'] = portal_url
55 options['isAnon'] = is_anon
56 options['isNewMember'] = is_newmember
57 options['isOrdinaryMember'] = not (mtool.isAnonymousUser() or is_newmember)
58 options['validate_email'] = validate_email
59 options['isAnonRegistration'] = rtool.getMode() == MODE_ANONYMOUS
60 options['isReviewedRegistration'] = rtool.getMode() == MODE_REVIEWED
61
62 buttons = []
63 if is_newmember:
64 target = atool.getActionInfo('user/logged_in')['url']
65 buttons.append( {'name': 'login', 'value': 'Log in'} )
66 else:
67 target = atool.getActionInfo('user/join')['url']
68 buttons.append( {'name': 'add', 'value': _('Join')} )
69 options['form'] = { 'action': target,
70 'listButtonInfos': tuple(buttons) }
71 options['ajax']=ajax
72 return context.join_template(**options)