Back to index

moin  1.9.0~rc2
changepass.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 """
00003     MoinMoin - Password change preferences plugin
00004 
00005     @copyright: 2001-2004 Juergen Hermann <jh@web.de>,
00006                 2003-2007 MoinMoin:ThomasWaldmann
00007                 2007      MoinMoin:JohannesBerg
00008     @license: GNU GPL, see COPYING for details.
00009 """
00010 
00011 from MoinMoin import user
00012 from MoinMoin.widget import html
00013 from MoinMoin.userprefs import UserPrefBase
00014 
00015 
00016 class Settings(UserPrefBase):
00017     def __init__(self, request):
00018         """ Initialize password change form. """
00019         UserPrefBase.__init__(self, request)
00020         self.request = request
00021         self._ = request.getText
00022         _ = request.getText
00023         self.cfg = request.cfg
00024         self.title = _("Change password")
00025         self.name = 'changepass'
00026 
00027 
00028     def allowed(self):
00029         return (not 'password' in self.cfg.user_form_remove and
00030                 not 'password' in self.cfg.user_form_disable and
00031                 UserPrefBase.allowed(self) and
00032                 not 'password' in self.request.user.auth_attribs)
00033 
00034 
00035     def handle_form(self):
00036         _ = self._
00037         request = self.request
00038         form = request.form
00039 
00040         if form.has_key('cancel'):
00041             return
00042 
00043         if request.method != 'POST':
00044             return
00045 
00046         password = form.get('password1', '')
00047         password2 = form.get('password2', '')
00048 
00049         # Check if password is given and matches with password repeat
00050         if password != password2:
00051             return 'error', _("Passwords don't match!")
00052         if not password:
00053             return 'error', _("Please specify a password!")
00054 
00055         pw_checker = request.cfg.password_checker
00056         if pw_checker:
00057             pw_error = pw_checker(request, request.user.name, password)
00058             if pw_error:
00059                 return 'error', _("Password not acceptable: %s") % pw_error
00060 
00061         try:
00062             self.request.user.enc_password = user.encodePassword(password)
00063             self.request.user.save()
00064             return 'info', _("Your password has been changed.")
00065         except UnicodeError, err:
00066             # Should never happen
00067             return "Can't encode password: %s" % str(err)
00068 
00069 
00070     def create_form(self, create_only=False, recover_only=False):
00071         """ Create the complete HTML form code. """
00072         _ = self._
00073         form = self.make_form(html.Text(_("To change your password, "
00074                                           "enter a new password twice.")))
00075 
00076         self.make_row(_('Password'),
00077                       [html.INPUT(type="password", size=36, name="password1")])
00078         self.make_row(_('Password repeat'),
00079                       [html.INPUT(type="password", size=36, name="password2")])
00080 
00081         # Add buttons
00082         self.make_row('', [
00083                 html.INPUT(type="submit", name='save', value=_("Change password")),
00084                 ' ',
00085                 html.INPUT(type="submit", name='cancel', value=_("Cancel")),
00086               ])
00087 
00088         return unicode(form)