1 # -*- coding: iso-8859-1 -*-
3 MoinMoin - create account action
6 @license: GNU GPL, see COPYING for details.
9 from MoinMoin import user, wikiutil, util
10 from MoinMoin.Page import Page
11 from MoinMoin.widget import html
12 from MoinMoin.security.textcha import TextCha
13 from MoinMoin.auth import MoinAuth
18 def _create_user(request):
22 if request.request_method != 'POST':
25 if not TextCha(request).check_answer_from_form():
26 return _('TextCha: Wrong answer! Go back and try again...')
29 theuser = user.User(request, auth_method="new-user")
31 # Require non-empty name
33 theuser.name = form['name'][0]
35 return _("Empty user name. Please enter a user name.")
37 # Don't allow creating users with invalid names
38 if not user.isValidName(request, theuser.name):
39 return _("""Invalid user name {{{'%s'}}}.
40 Name may contain any Unicode alpha numeric character, with optional one
41 space between words. Group page name is not allowed.""", wiki=True) % wikiutil.escape(theuser.name)
43 # Name required to be unique. Check if name belong to another user.
44 if user.getUserId(request, theuser.name):
45 return _("This user name already belongs to somebody else.")
47 # try to get the password and pw repeat
48 password = form.get('password1', [''])[0]
49 password2 = form.get('password2', [''])[0]
51 # Check if password is given and matches with password repeat
52 if password != password2:
53 return _("Passwords don't match!")
55 return _("Please specify a password!")
57 pw_checker = request.cfg.password_checker
59 pw_error = pw_checker(request, theuser.name, password)
61 return _("Password not acceptable: %s") % pw_error
64 if password and not password.startswith('{SHA}'):
66 theuser.enc_password = user.encodePassword(password)
67 except UnicodeError, err:
69 return "Can't encode password: %s" % str(err)
71 # try to get the email, for new users it is required
72 email = wikiutil.clean_input(form.get('email', [''])[0])
73 theuser.email = email.strip()
74 if not theuser.email and 'email' not in request.cfg.user_form_remove:
75 return _("Please provide your email address. If you lose your"
76 " login information, you can get it by email.")
78 # Email should be unique - see also MoinMoin/script/accounts/moin_usercheck.py
79 if theuser.email and request.cfg.user_email_unique:
80 if user.get_by_email_address(request, theuser.email):
81 return _("This email already belongs to somebody else.")
86 result = _("User account created! You can use this account to login now...")
88 result = result + util.dumpFormData(form)
92 def _create_form(request):
94 url = request.page.url(request)
95 ret = html.FORM(action=url)
96 ret.append(html.INPUT(type='hidden', name='action', value='newaccount'))
97 lang_attr = request.theme.ui_lang_attr()
98 ret.append(html.Raw('<div class="userpref"%s>' % lang_attr))
99 tbl = html.TABLE(border="0")
101 ret.append(html.Raw('</div>'))
105 row.append(html.TD().append(html.STRONG().append(
106 html.Text(_("Name")))))
109 cell.append(html.INPUT(type="text", size="36", name="name"))
110 cell.append(html.Text(' ' + _("(Use FirstnameLastname)")))
114 row.append(html.TD().append(html.STRONG().append(
115 html.Text(_("Password")))))
116 row.append(html.TD().append(html.INPUT(type="password", size="36",
121 row.append(html.TD().append(html.STRONG().append(
122 html.Text(_("Password repeat")))))
123 row.append(html.TD().append(html.INPUT(type="password", size="36",
128 row.append(html.TD().append(html.STRONG().append(html.Text(_("Email")))))
129 row.append(html.TD().append(html.INPUT(type="text", size="36",
132 textcha = TextCha(request)
133 if textcha.is_enabled():
136 row.append(html.TD().append(html.STRONG().append(
137 html.Text(_('TextCha (required)')))))
140 td.append(textcha.render())
145 row.append(html.TD())
148 td.append(html.INPUT(type="submit", name="create",
149 value=_('Create Profile')))
153 def execute(pagename, request):
155 for auth in request.cfg.auth:
156 if isinstance(auth, MoinAuth):
161 # we will not have linked, so forbid access
162 request.makeForbidden403()
165 page = Page(request, pagename)
169 submitted = form.has_key('create')
171 if submitted: # user pressed create button
172 request.theme.add_msg(_create_user(request), "dialog")
173 return page.send_page()
174 else: # show create form
175 request.emit_http_headers()
176 request.theme.send_title(_("Create Account"), pagename=pagename)
178 request.write(request.formatter.startContent("content"))
180 # THIS IS A BIG HACK. IT NEEDS TO BE CLEANED UP
181 request.write(_create_form(request))
183 request.write(request.formatter.endContent())
185 request.theme.send_footer(pagename)
186 request.theme.send_closing_html()