Attachment 'newaccount.py'

Download

   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
  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
  14 
  15 
  16 def _create_user(request):
  17     _ = request.getText
  18     form = request.form
  19 
  20     if request.method != 'POST':
  21         return
  22 
  23     if not TextCha(request).check_answer_from_form():
  24         return _('TextCha: Wrong answer! Go back and try again...')
  25 
  26     # Create user profile
  27     theuser = user.User(request, auth_method="new-user")
  28 
  29     # Require non-empty name
  30     try:
  31         theuser.name = form['name']
  32     except KeyError:
  33         return _("Empty user name. Please enter a user name.")
  34 
  35     # Don't allow creating users with invalid names
  36     if not user.isValidName(request, theuser.name):
  37         return _("""Invalid user name {{{'%s'}}}.
  38 Name may contain any Unicode alpha numeric character, with optional one
  39 space between words. Group page name is not allowed.""", wiki=True) % wikiutil.escape(theuser.name)
  40 
  41     # Name required to be unique. Check if name belong to another user.
  42     if user.getUserId(request, theuser.name):
  43         return _("This user name already belongs to somebody else.")
  44 
  45     # try to get the password and pw repeat
  46     password = form.get('password1', '')
  47     password2 = form.get('password2', '')
  48 
  49     # Check if password is given and matches with password repeat
  50     if password != password2:
  51         return _("Passwords don't match!")
  52     if not password:
  53         return _("Please specify a password!")
  54 
  55     pw_checker = request.cfg.password_checker
  56     if pw_checker:
  57         pw_error = pw_checker(request, theuser.name, password)
  58         if pw_error:
  59             return _("Password not acceptable: %s") % pw_error
  60 
  61     # Encode password
  62     if password and not password.startswith('{SHA}'):
  63         try:
  64             theuser.enc_password = user.encodePassword(password)
  65         except UnicodeError, err:
  66             # Should never happen
  67             return "Can't encode password: %s" % str(err)
  68 
  69     # try to get the email, for new users it is required
  70     email = wikiutil.clean_input(form.get('email', ''))
  71     theuser.email = email.strip()
  72     if not theuser.email and 'email' not in request.cfg.user_form_remove:
  73         return _("Please provide your email address. If you lose your"
  74                  " login information, you can get it by email.")
  75 
  76     # Email should be unique - see also MoinMoin/script/accounts/moin_usercheck.py
  77     if theuser.email and request.cfg.user_email_unique:
  78         if user.get_by_email_address(request, theuser.email):
  79             return _("This email already belongs to somebody else.")
  80 
  81     # save data
  82     theuser.save()
  83 
  84     result = _("User account created! You can use this account to login now...")
  85     return result
  86 
  87 
  88 def _create_form(request):
  89     _ = request.getText
  90     url = request.page.url(request)
  91     ret = html.FORM(action=url)
  92     ret.append(html.INPUT(type='hidden', name='action', value='newaccount'))
  93     lang_attr = request.theme.ui_lang_attr()
  94     ret.append(html.Raw('<div class="userpref"%s>' % lang_attr))
  95     tbl = html.TABLE(border="0")
  96     ret.append(tbl)
  97     ret.append(html.Raw('</div>'))
  98 
  99     row = html.TR()
 100     tbl.append(row)
 101     row.append(html.TD().append(html.STRONG().append(
 102                                   html.Text(_("Name")))))
 103     cell = html.TD()
 104     row.append(cell)
 105     cell.append(html.INPUT(type="text", size="36", name="name"))
 106     cell.append(html.Text(' ' + _("(Use FirstnameLastname)")))
 107 
 108     row = html.TR()
 109     tbl.append(row)
 110     row.append(html.TD().append(html.STRONG().append(
 111                                   html.Text(_("Password")))))
 112     row.append(html.TD().append(html.INPUT(type="password", size="36",
 113                                            name="password1")))
 114 
 115     row = html.TR()
 116     tbl.append(row)
 117     row.append(html.TD().append(html.STRONG().append(
 118                                   html.Text(_("Password repeat")))))
 119     row.append(html.TD().append(html.INPUT(type="password", size="36",
 120                                            name="password2")))
 121 
 122     row = html.TR()
 123     tbl.append(row)
 124     row.append(html.TD().append(html.STRONG().append(html.Text(_("Email")))))
 125     row.append(html.TD().append(html.INPUT(type="text", size="36",
 126                                            name="email")))
 127 
 128     textcha = TextCha(request)
 129     if textcha.is_enabled():
 130         row = html.TR()
 131         tbl.append(row)
 132         row.append(html.TD().append(html.STRONG().append(
 133                                       html.Text(_('TextCha (required)')))))
 134         td = html.TD()
 135         if textcha:
 136             td.append(textcha.render())
 137         row.append(td)
 138 
 139     row = html.TR()
 140     tbl.append(row)
 141     row.append(html.TD())
 142     td = html.TD()
 143     row.append(td)
 144     td.append(html.INPUT(type="submit", name="create",
 145                          value=_('Create Profile')))
 146 
 147     return unicode(ret)
 148 
 149 def execute(pagename, request):
 150     found = False
 151     for auth in request.cfg.auth:
 152         if isinstance(auth, MoinAuth):
 153             found = True
 154             break
 155 
 156     if not found:
 157         # we will not have linked, so forbid access
 158         request.makeForbidden(403, 'No MoinAuth in auth list')
 159         return
 160 
 161     page = Page(request, pagename)
 162     _ = request.getText
 163     form = request.form
 164     
 165     #Begin change from GunnarScherf
 166     if not request.user.isSuperUser():
 167         request.theme.add_msg(_('You are not allowed to use this action.'), "error")
 168         return page.send_page()	    
 169     #End Change from Gunnar Scherf
 170 
 171     submitted = form.has_key('create')
 172 
 173     if submitted: # user pressed create button
 174         request.theme.add_msg(_create_user(request), "dialog")
 175         return page.send_page()
 176     else: # show create form
 177         request.theme.send_title(_("Create Account"), pagename=pagename)
 178 
 179         request.write(request.formatter.startContent("content"))
 180 
 181         # THIS IS A BIG HACK. IT NEEDS TO BE CLEANED UP
 182         request.write(_create_form(request))
 183 
 184         request.write(request.formatter.endContent())
 185 
 186         request.theme.send_footer(pagename)
 187         request.theme.send_closing_html()

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2009-12-24 13:44:46, 0.6 KB) [[attachment:myauth.py]]
  • [get | view] (2009-12-24 13:45:01, 6.2 KB) [[attachment:newaccount.py]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.