mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-04 17:04:59 +03:00
Merge pull request #5523 from Remchi/change-reset-password-refactor
Refactor changePassword and resetPassword
This commit is contained in:
commit
843dd31b74
@ -434,26 +434,6 @@ users = {
|
|||||||
*/
|
*/
|
||||||
changePassword: function changePassword(object, options) {
|
changePassword: function changePassword(object, options) {
|
||||||
var tasks;
|
var tasks;
|
||||||
/**
|
|
||||||
* ### Validation
|
|
||||||
* Ensure we have valid options - special validation just for password
|
|
||||||
* @TODO change User.changePassword to take an object not 4 args
|
|
||||||
* @param {Object} object
|
|
||||||
* @param {Object} options
|
|
||||||
* @returns {Object} options
|
|
||||||
*/
|
|
||||||
function validate(object, options) {
|
|
||||||
options = options || {};
|
|
||||||
return utils.checkObject(object, 'password').then(function (data) {
|
|
||||||
options.data = {
|
|
||||||
oldPassword: data.password[0].oldPassword,
|
|
||||||
newPassword: data.password[0].newPassword,
|
|
||||||
ne2Password: data.password[0].ne2Password,
|
|
||||||
userId: parseInt(data.password[0].user_id)
|
|
||||||
};
|
|
||||||
return options;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ### Handle Permissions
|
* ### Handle Permissions
|
||||||
@ -462,7 +442,7 @@ users = {
|
|||||||
* @returns {Object} options
|
* @returns {Object} options
|
||||||
*/
|
*/
|
||||||
function handlePermissions(options) {
|
function handlePermissions(options) {
|
||||||
return canThis(options.context).edit.user(options.data.userId).then(function permissionGranted() {
|
return canThis(options.context).edit.user(options.data.password[0].user_id).then(function permissionGranted() {
|
||||||
return options;
|
return options;
|
||||||
}).catch(function (error) {
|
}).catch(function (error) {
|
||||||
return errors.handleAPIError(error, 'You do not have permission to change the password for this user');
|
return errors.handleAPIError(error, 'You do not have permission to change the password for this user');
|
||||||
@ -477,16 +457,13 @@ users = {
|
|||||||
*/
|
*/
|
||||||
function doQuery(options) {
|
function doQuery(options) {
|
||||||
return dataProvider.User.changePassword(
|
return dataProvider.User.changePassword(
|
||||||
options.data.oldPassword,
|
options.data.password[0],
|
||||||
options.data.newPassword,
|
|
||||||
options.data.ne2Password,
|
|
||||||
options.data.userId,
|
|
||||||
_.omit(options, ['data'])
|
_.omit(options, ['data'])
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Push all of our tasks into a `tasks` array in the correct order
|
// Push all of our tasks into a `tasks` array in the correct order
|
||||||
tasks = [validate, handlePermissions, utils.convertOptions(allowedIncludes), doQuery];
|
tasks = [utils.validate('password'), handlePermissions, utils.convertOptions(allowedIncludes), doQuery];
|
||||||
|
|
||||||
// Pipeline calls each task passing the result of one to be the arguments for the next
|
// Pipeline calls each task passing the result of one to be the arguments for the next
|
||||||
return pipeline(tasks, object, options).then(function formatResponse() {
|
return pipeline(tasks, object, options).then(function formatResponse() {
|
||||||
|
@ -586,14 +586,15 @@ User = ghostBookshelf.Model.extend({
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Naive change password method
|
* Naive change password method
|
||||||
* @param {String} oldPassword
|
* @param {Object} object
|
||||||
* @param {String} newPassword
|
|
||||||
* @param {String} ne2Password
|
|
||||||
* @param {Integer} userId
|
|
||||||
* @param {Object} options
|
* @param {Object} options
|
||||||
*/
|
*/
|
||||||
changePassword: function changePassword(oldPassword, newPassword, ne2Password, userId, options) {
|
changePassword: function changePassword(object, options) {
|
||||||
var self = this,
|
var self = this,
|
||||||
|
newPassword = object.newPassword,
|
||||||
|
ne2Password = object.ne2Password,
|
||||||
|
userId = object.user_id,
|
||||||
|
oldPassword = object.oldPassword,
|
||||||
user;
|
user;
|
||||||
|
|
||||||
if (newPassword !== ne2Password) {
|
if (newPassword !== ne2Password) {
|
||||||
@ -706,8 +707,12 @@ User = ghostBookshelf.Model.extend({
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
resetPassword: function resetPassword(token, newPassword, ne2Password, dbHash) {
|
resetPassword: function resetPassword(options) {
|
||||||
var self = this;
|
var self = this,
|
||||||
|
token = options.token,
|
||||||
|
newPassword = options.newPassword,
|
||||||
|
ne2Password = options.ne2Password,
|
||||||
|
dbHash = options.dbHash;
|
||||||
|
|
||||||
if (newPassword !== ne2Password) {
|
if (newPassword !== ne2Password) {
|
||||||
return Promise.reject(new Error('Your new passwords do not match'));
|
return Promise.reject(new Error('Your new passwords do not match'));
|
||||||
|
@ -592,7 +592,7 @@ describe('User Model', function run() {
|
|||||||
return UserModel.generateResetToken(firstUser.attributes.email, expires, dbHash);
|
return UserModel.generateResetToken(firstUser.attributes.email, expires, dbHash);
|
||||||
}).then(function (token) {
|
}).then(function (token) {
|
||||||
token = utils.encodeBase64URLsafe(token);
|
token = utils.encodeBase64URLsafe(token);
|
||||||
return UserModel.resetPassword(token, 'newpassword', 'newpassword', dbHash);
|
return UserModel.resetPassword({token: token, newPassword: 'newpassword', ne2Password: 'newpassword', dbHash: dbHash});
|
||||||
}).then(function (resetUser) {
|
}).then(function (resetUser) {
|
||||||
var resetPassword = resetUser.get('password');
|
var resetPassword = resetUser.get('password');
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user