From 54415baf37c35fdecdc2cd97ac1a910359c2c85f Mon Sep 17 00:00:00 2001 From: Gabor Javorszky Date: Tue, 6 Aug 2013 00:49:06 +0100 Subject: [PATCH] Users can change password Closes #282 * Added a new route * Added new methods * Triple security! * Passwords are actually changed * Also added a change password button, because 'save' has too much baggage. On security: checks whether you're logged in. Then checks whether your old password is actually the one that belongs to you (gets value from the email field for the email, see caveat no2). Checks the new passwords for === and length > 6 on client and server side as well. And THEN changes passwords. Caveats: * didn't add a test, as mocha fails spectacularly on my machine. SQLITE_CORRUPT: database disk image is malformed. Cute, huh? * Because we don't have / I'm not aware of / could not find a "currentuser" variable, I need to get the email address of the user we want to change from the email field. Theoretically if they replace that with another user's email address, and supply their pw, they will change THEIR password instead of their own. --- ghost/admin/tpl/settings/user-profile.hbs | 3 +- ghost/admin/views/settings.js | 52 ++++++++++++++++++++++- 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/ghost/admin/tpl/settings/user-profile.hbs b/ghost/admin/tpl/settings/user-profile.hbs index 9a0821ec33..3347106837 100644 --- a/ghost/admin/tpl/settings/user-profile.hbs +++ b/ghost/admin/tpl/settings/user-profile.hbs @@ -69,8 +69,9 @@ + - \ No newline at end of file + diff --git a/ghost/admin/views/settings.js b/ghost/admin/views/settings.js index 33e68288bc..789012de2f 100644 --- a/ghost/admin/views/settings.js +++ b/ghost/admin/views/settings.js @@ -176,7 +176,8 @@ }, events: { - 'click .button-save': 'saveUser' + 'click .button-save': 'saveUser', + 'click .button-change-password': 'changePassword' }, saveUser: function () { @@ -194,6 +195,55 @@ }); }, + changePassword: function (event) { + event.preventDefault(); + + var self = this, + email = this.$('#user-email').val(), + oldPassword = this.$('#user-password-old').val(), + newPassword = this.$('#user-password-new').val(), + ne2Password = this.$('#user-new-password-verification').val(); + + if (newPassword !== ne2Password || newPassword.length < 6 || oldPassword.length < 6) { + this.saveError(); + return; + } + + $.ajax({ + url: '/ghost/changepw/', + type: 'POST', + data: { + email: email, + password: oldPassword, + newpassword: newPassword, + ne2password: ne2Password + }, + success: function (msg) { + + self.addSubview(new Ghost.Views.NotificationCollection({ + model: [{ + type: 'success', + message: msg.msg, + status: 'passive', + id: 'success-98' + }] + })); + self.$('#user-password-old').val(''); + self.$('#user-password-new').val(''); + self.$('#user-new-password-verification').val(''); + }, + error: function (obj, string, status) { + self.addSubview(new Ghost.Views.NotificationCollection({ + model: [{ + type: 'error', + message: 'Invalid username or password', + status: 'passive' + }] + })); + } + }); + }, + templateName: 'settings/user-profile', beforeRender: function () {