diff --git a/scribeengine/controllers/account.py b/scribeengine/controllers/account.py index bfd47df..9306051 100644 --- a/scribeengine/controllers/account.py +++ b/scribeengine/controllers/account.py @@ -46,24 +46,24 @@ class AccountController(BaseController): @jsvalidate(u'register-form') def register_jsschema(self): return { - u'email': JSEmail(required=True, message=u'You haven\'t typed in an e-mail address.'), - u'password': JSString(required=True, message=u'You haven\'t typed in a password.'), - u'confirm-password': JSString(required=True, equalTo=u'#password', message=u'Your passwords don\'t match.') + u'register-email': JSEmail(required=True, message=u'You haven\'t typed in an e-mail address.'), + u'register-password': JSString(required=True, message=u'You haven\'t typed in a password.'), + u'register-confirm': JSString(required=True, equalTo=u'#password', message=u'Your passwords don\'t match.') } def register_schema(self): return { - 'email': Email(not_empty=True, messages={'empty': u'You haven\'t typed in an e-mail address.'}), - 'password': UnicodeString(not_empty=True, messages={'empty': u'You haven\'t typed in a password.'}), - 'confirm': [FieldsMatch('password', 'confirm-passsword', messages={'invalid': u'Your passwords don\'t match.'})] + 'register-email': Email(not_empty=True, messages={'empty': u'You haven\'t typed in an e-mail address.'}), + 'register-password': UnicodeString(not_empty=True, messages={'empty': u'You haven\'t typed in a password.'}), + 'confirm-password': [FieldsMatch('register-password', 'register-confirm', messages={'invalid': u'Your passwords don\'t match.'})] } def register_POST(self): activation_code = u''.join(random.sample(string.letters + string.digits, 40)) user = User( - nick=c.form_values[u'nick'], - email=c.form_values[u'email'], - password=utils.hash_password(c.form_values[u'password']), + nick=c.form_values[u'register-nick'], + email=c.form_values[u'register-email'], + password=utils.hash_password(c.form_values[u'register-password']), activation_key=activation_code ) Session.add(user) @@ -121,29 +121,24 @@ class AccountController(BaseController): c.page_title = u'Reset Password' return render(u'/account/reset.mako') - @jsvalidate(u'reset-form') + @jsvalidate(u'account-reset') def reset_jsschema(self): return { - u'email': JSEmail(required=True, message=u'You haven\'t typed in an e-mail address.'), - u'password': JSString(required=True, message=u'You haven\'t typed in a password.'), - u'confirm-password': JSString(required=True, equalTo=u'#password', message=u'Your passwords don\'t match.') + u'reset-email': JSEmail(required=True, message=u'You haven\'t typed in a valid e-mail address.') } def reset_schema(self): return { - 'email': Email(not_empty=True, messages={'empty': u'You haven\'t typed in an e-mail address.'}), - 'password': UnicodeString(not_empty=True, messages={'empty': u'You haven\'t typed in a password.'}), - 'confirm': [FieldsMatch('password', 'confirm-passsword', messages={'invalid': u'Your passwords don\'t match.'})] + 'reset-email': Email(not_empty=True, messages={'empty': u'You haven\'t typed in a valid e-mail address.'}) } def reset_POST(self): - email = c.form_values[u'email'] + email = c.form_values[u'reset-email'] user = Session.query(User).filter_by(email=email).first() if not user: h.flash.set_message(u'Your e-mail address is not in the system.', u'error') else: activation_code = u''.join(random.sample(string.letters + string.digits, 40)) - user.password = utils.hash_password(activation_code), user.activation_key = activation_code user.modified = datetime.now() Session.add(user) @@ -157,40 +152,77 @@ class AccountController(BaseController): blog_host = Variable(key=u'blog host', value=url) Session.add(blog_host) Session.commit() - utils.send_mail(u'/email/activate.mako', u'%s <%s>' % (user.nick, user.email), + utils.send_mail(u'/email/reset.mako', u'%s <%s>' % (user.nick, user.email), u'%s <%s>' % (blog_mail.value, blog_title.value), - u'[%s] Activate your account!' % blog_title.value, + u'[%s] Reset your password!' % blog_title.value, { 'user': user, 'blog_title': blog_title.value, 'blog_host': blog_host.value }) h.flash.set_message(u'An e-mail has been sent to your e-mail address. ' - u'Please activate your account by clicking on the link in your ' + u'Please reset your password by clicking on the link in your ' u'e-mail.', u'success') - h.redirect_to('/') + h.redirect_to('/account/reset') + + def password(self, id=None): + if not id or not request.GET.get(u'code'): + h.flash.set_message(u'There was a problem with your activation code, please reset your password again.', u'error') + h.redirect_to(h.url_for(controller=u'account', action=u'login')) + c.user = Session.query(User).get(id) + if not c.user: + h.flash.set_message(u'There was a problem with your account, please reset your password again.', u'error') + h.redirect_to(h.url_for(controller=u'account', action=u'login')) + c.page_title = u'Change Password' + return render(u'/account/password.mako') + + @jsvalidate(u'account-password') + def password_jsschema(self): + return { + u'password-password': JSString(required=True, message=u'You haven\'t typed in a password.'), + u'password-confirm': JSString(required=True, equalTo=u'#password-password', message=u'Your passwords don\'t match.') + } + + def password_schema(self): + return { + 'user_id': Int(), + 'password-password': UnicodeString(not_empty=True, messages={'empty': u'You haven\'t typed in a password.'}), + 'confirm-password': [FieldsMatch('password-password', 'password-confirm', messages={'invalid': u'Your passwords don\'t match.'})] + } + + def password_POST(self): + user = Session.query(User).get(c.form_values[u'user_id']) + if not user: + h.flash.set_message(u'There was a problem with your account, please reset your password again.', u'error') + h.redirect_to(h.url_for(controller=u'account', action=u'login')) + user.password = utils.hash_password(c.form_values[u'password-password']) + user.modified = datetime.now() + Session.add(user) + Session.commit() + h.flash.set_message(u'Successfully updated your password. Please login with your new password.', u'success') + h.redirect_to('/account/login') def login(self): c.page_title = u'Login' return render(u'/account/login.mako') - @jsvalidate(u'login-form') + @jsvalidate(u'account-login') def login_jsschema(self): return { - u'email': JSEmail(required=True, message=u'You haven\'t typed in an e-mail address.'), - u'password': JSString(required=True, message=u'You haven\'t typed in a password.') + u'login-email': JSEmail(required=True, message=u'You haven\'t typed in an e-mail address.'), + u'login-password': JSString(required=True, message=u'You haven\'t typed in a password.') } def login_schema(self): return { - 'email': Email(not_empty=True, messages={'empty': u'You haven\'t typed in an e-mail address.'}), - 'password': UnicodeString(not_empty=True, messages={'empty': u'You haven\'t typed in a password.'}) + 'login-email': Email(not_empty=True, messages={'empty': u'You haven\'t typed in an e-mail address.'}), + 'login-password': UnicodeString(not_empty=True, messages={'empty': u'You haven\'t typed in a password.'}) } def login_POST(self): - log.debug('Logging in as "%s" with password "%s"', c.form_values[u'email'], c.form_values[u'password']) - user = Session.query(User).filter_by(email=c.form_values[u'email']).first() - password = utils.hash_password(c.form_values[u'password']) + log.debug('Logging in as "%s" with password "%s"', c.form_values[u'login-email'], c.form_values[u'login-password']) + user = Session.query(User).filter_by(email=c.form_values[u'login-email']).first() + password = utils.hash_password(c.form_values[u'login-password']) if not user or user.password != password: log.debug('Username or password are incorrect.') h.flash.set_message(u'Your username or password are incorrect.', u'error') diff --git a/scribeengine/templates/account/login.mako b/scribeengine/templates/account/login.mako index ed11b2c..bb94d38 100644 --- a/scribeengine/templates/account/login.mako +++ b/scribeengine/templates/account/login.mako @@ -3,19 +3,23 @@