mirror of
https://github.com/plankanban/planka.git
synced 2025-07-18 20:59:44 +02:00
83 lines
1.6 KiB
JavaScript
83 lines
1.6 KiB
JavaScript
const bcrypt = require('bcrypt');
|
|
|
|
const Errors = {
|
|
USER_NOT_FOUND: {
|
|
userNotFound: 'User not found',
|
|
},
|
|
INVALID_CURRENT_PASSWORD: {
|
|
invalidCurrentPassword: 'Invalid current password',
|
|
},
|
|
EMAIL_ALREADY_IN_USE: {
|
|
emailAlreadyInUse: 'Email already in use',
|
|
},
|
|
};
|
|
|
|
module.exports = {
|
|
inputs: {
|
|
id: {
|
|
type: 'string',
|
|
regex: /^[0-9]+$/,
|
|
required: true,
|
|
},
|
|
email: {
|
|
type: 'string',
|
|
isEmail: true,
|
|
required: true,
|
|
},
|
|
currentPassword: {
|
|
type: 'string',
|
|
isNotEmptyString: true,
|
|
},
|
|
},
|
|
|
|
exits: {
|
|
userNotFound: {
|
|
responseType: 'notFound',
|
|
},
|
|
invalidCurrentPassword: {
|
|
responseType: 'forbidden',
|
|
},
|
|
emailAlreadyInUse: {
|
|
responseType: 'conflict',
|
|
},
|
|
},
|
|
|
|
async fn(inputs) {
|
|
const { currentUser } = this.req;
|
|
|
|
if (inputs.id === currentUser.id) {
|
|
if (!inputs.currentPassword) {
|
|
throw Errors.INVALID_CURRENT_PASSWORD;
|
|
}
|
|
} else if (!currentUser.isAdmin) {
|
|
throw Errors.USER_NOT_FOUND; // Forbidden
|
|
}
|
|
|
|
let user = await sails.helpers.users.getOne(inputs.id);
|
|
|
|
if (!user) {
|
|
throw Errors.USER_NOT_FOUND;
|
|
}
|
|
|
|
if (
|
|
inputs.id === currentUser.id &&
|
|
!bcrypt.compareSync(inputs.currentPassword, user.password)
|
|
) {
|
|
throw Errors.INVALID_CURRENT_PASSWORD;
|
|
}
|
|
|
|
const values = _.pick(inputs, ['email']);
|
|
|
|
user = await sails.helpers.users
|
|
.update(user, values, this.req)
|
|
.intercept('emailAlreadyInUse', () => Errors.EMAIL_ALREADY_IN_USE);
|
|
|
|
if (!user) {
|
|
throw Errors.USER_NOT_FOUND;
|
|
}
|
|
|
|
return {
|
|
item: user,
|
|
};
|
|
},
|
|
};
|