comparison MoinMoin/action/newaccount.py @ 2294:22749e92a461

new userprefs handling including plugins; todo: refactor prefs.py
author Johannes Berg <johannes AT sipsolutions DOT net>
date Tue, 10 Jul 2007 17:04:55 +0200
parents
children b9feee61d28e
comparison
equal deleted inserted replaced
2293:bb76818db210 2294:22749e92a461
1 # -*- coding: iso-8859-1 -*-
2 """
3 MoinMoin - create account action
4
5 @copyright: 2007 MoinMoin:JohannesBerg
6 @license: GNU GPL, see COPYING for details.
7 """
8
9 from MoinMoin import user, wikiutil, util
10 from MoinMoin.Page import Page
11 from MoinMoin.widget import html
12 import MoinMoin.events as events
13 from MoinMoin.userprefs.prefs import Settings
14
15
16 _debug = False
17
18 def _create_user(request):
19 _ = request.getText
20 form = request.form
21
22 if request.request_method != 'POST':
23 return _("Use UserPreferences to change your settings or create an account.")
24 # Create user profile
25 theuser = user.User(request, auth_method="new-user")
26
27 # Require non-empty name
28 try:
29 theuser.name = form['name'][0]
30 except KeyError:
31 return _("Empty user name. Please enter a user name.")
32
33 # Don't allow creating users with invalid names
34 if not user.isValidName(request, theuser.name):
35 return _("""Invalid user name {{{'%s'}}}.
36 Name may contain any Unicode alpha numeric character, with optional one
37 space between words. Group page name is not allowed.""") % wikiutil.escape(theuser.name)
38
39 # Name required to be unique. Check if name belong to another user.
40 if user.getUserId(request, theuser.name):
41 return _("This user name already belongs to somebody else.")
42
43 # try to get the password and pw repeat
44 password = form.get('password', [''])[0]
45 password2 = form.get('password2', [''])[0]
46
47 # Check if password is given and matches with password repeat
48 if password != password2:
49 return _("Passwords don't match!")
50 if not password:
51 return _("Please specify a password!")
52
53 # Encode password
54 if password and not password.startswith('{SHA}'):
55 try:
56 theuser.enc_password = user.encodePassword(password)
57 except UnicodeError, err:
58 # Should never happen
59 return "Can't encode password: %s" % str(err)
60
61 # try to get the email, for new users it is required
62 email = wikiutil.clean_input(form.get('email', [''])[0])
63 theuser.email = email.strip()
64 if not theuser.email:
65 return _("Please provide your email address. If you lose your"
66 " login information, you can get it by email.")
67
68 # Email should be unique - see also MoinMoin/script/accounts/moin_usercheck.py
69 if theuser.email and request.cfg.user_email_unique:
70 users = user.getUserList(request)
71 for uid in users:
72 if uid == theuser.id:
73 continue
74 thisuser = user.User(request, uid)
75 if thisuser.email == theuser.email and not thisuser.disabled:
76 return _("This email already belongs to somebody else.")
77
78 # save data
79 theuser.save()
80
81 user_created = events.UserCreatedEvent(request, theuser)
82 events.send_event(user_created)
83
84 if form.has_key('create_and_mail'):
85 theuser.mailAccountData()
86
87 result = _("User account created! You can use this account to login now...")
88 if _debug:
89 result = result + util.dumpFormData(form)
90 return result
91
92
93 def execute(pagename, request):
94 pagename = pagename
95 page = Page(request, pagename)
96 _ = request.getText
97 form = request.form
98
99 submitted = form.has_key('create_only') or form.has_key('create_and_mail')
100
101 if submitted: # user pressed create button
102 error = _create_user(request)
103 return page.send_page(msg=error)
104 else: # show create form
105 request.emit_http_headers()
106 request.theme.send_title(_("Create Account"), pagename=pagename)
107
108 request.write(request.formatter.startContent("content"))
109
110 # THIS IS A BIG HACK. IT NEEDS TO BE CLEANED UP
111 request.write(Settings(request).create_form(create_only=True))
112
113 request.write(request.formatter.endContent())
114
115 request.theme.send_footer(pagename)
116 request.theme.send_closing_html()