1
0
Fork 0
mirror of https://github.com/plankanban/planka.git synced 2025-07-19 05:09:43 +02:00

Add email and password change functionality for a current user, remove deep compare hooks

This commit is contained in:
Maksim Eltyshev 2019-10-18 08:06:34 +05:00
parent b53e5bf94c
commit 680d664279
67 changed files with 1232 additions and 267 deletions

View file

@ -0,0 +1,83 @@
const bcrypt = require('bcrypt');
const Errors = {
USER_NOT_FOUND: {
notFound: 'User is not found'
},
CURRENT_PASSWORD_NOT_VALID: {
forbidden: 'Current password is not valid'
},
USER_EXIST: {
conflict: 'User is already exist'
}
};
module.exports = {
inputs: {
id: {
type: 'string',
regex: /^[0-9]+$/,
required: true
},
email: {
type: 'string',
isEmail: true,
required: true
},
currentPassword: {
type: 'string',
isNotEmptyString: true
}
},
exits: {
notFound: {
responseType: 'notFound'
},
forbidden: {
responseType: 'forbidden'
},
conflict: {
responseType: 'conflict'
}
},
fn: async function(inputs, exits) {
const { currentUser } = this.req;
if (inputs.id === currentUser.id) {
if (!inputs.currentPassword) {
throw Errors.CURRENT_PASSWORD_NOT_VALID;
}
} else if (!currentUser.isAdmin) {
throw Errors.USER_NOT_FOUND; // Forbidden
}
let user = await sails.helpers.getUser(inputs.id);
if (!user) {
throw Errors.USER_NOT_FOUND;
}
if (
inputs.id === currentUser.id &&
!bcrypt.compareSync(inputs.currentPassword, user.password)
) {
throw Errors.CURRENT_PASSWORD_NOT_VALID;
}
const values = _.pick(inputs, ['email']);
user = await sails.helpers
.updateUser(user, values, this.req)
.intercept('conflict', () => Errors.USER_EXIST);
if (!user) {
throw Errors.USER_NOT_FOUND;
}
return exits.success({
item: user.email
});
}
};

View file

@ -0,0 +1,74 @@
const bcrypt = require('bcrypt');
const Errors = {
USER_NOT_FOUND: {
notFound: 'User is not found'
},
CURRENT_PASSWORD_NOT_VALID: {
forbidden: 'Current password is not valid'
}
};
module.exports = {
inputs: {
id: {
type: 'string',
regex: /^[0-9]+$/,
required: true
},
password: {
type: 'string',
required: true
},
currentPassword: {
type: 'string',
isNotEmptyString: true
}
},
exits: {
notFound: {
responseType: 'notFound'
},
forbidden: {
responseType: 'forbidden'
}
},
fn: async function(inputs, exits) {
const { currentUser } = this.req;
if (inputs.id === currentUser.id) {
if (!inputs.currentPassword) {
throw Errors.CURRENT_PASSWORD_NOT_VALID;
}
} else if (!currentUser.isAdmin) {
throw Errors.USER_NOT_FOUND; // Forbidden
}
let user = await sails.helpers.getUser(inputs.id);
if (!user) {
throw Errors.USER_NOT_FOUND;
}
if (
inputs.id === currentUser.id &&
!bcrypt.compareSync(inputs.currentPassword, user.password)
) {
throw Errors.CURRENT_PASSWORD_NOT_VALID;
}
const values = _.pick(inputs, ['password']);
user = await sails.helpers.updateUser(user, values, this.req);
if (!user) {
throw Errors.USER_NOT_FOUND;
}
return exits.success({
item: null
});
}
};

View file

@ -118,7 +118,7 @@ module.exports = {
}
return this.res.json({
item: user
item: user.avatar
});
});
}