1
0
Fork 0
mirror of https://github.com/plankanban/planka.git synced 2025-07-18 20:59:44 +02:00
planka/server/api/controllers/users/update-email.js

99 lines
2 KiB
JavaScript
Raw Normal View History

const bcrypt = require('bcrypt');
const Errors = {
2023-10-17 19:18:19 +02:00
NOT_ENOUGH_RIGHTS: {
notEnoughRights: 'Not enough rights',
},
USER_NOT_FOUND: {
2020-04-03 00:35:25 +05:00
userNotFound: 'User not found',
},
2020-04-03 00:35:25 +05:00
INVALID_CURRENT_PASSWORD: {
invalidCurrentPassword: 'Invalid current password',
},
2020-04-03 00:35:25 +05:00
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: {
2023-10-17 19:18:19 +02:00
notEnoughRights: {
responseType: 'forbidden',
},
2020-04-03 00:35:25 +05:00
userNotFound: {
responseType: 'notFound',
},
2020-04-03 00:35:25 +05:00
invalidCurrentPassword: {
responseType: 'forbidden',
},
2020-04-03 00:35:25 +05:00
emailAlreadyInUse: {
responseType: 'conflict',
},
},
async fn(inputs) {
const { currentUser } = this.req;
if (inputs.id === currentUser.id) {
if (!inputs.currentPassword) {
2020-04-03 00:35:25 +05:00
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;
}
2023-10-17 19:18:19 +02:00
if (user.email === sails.config.custom.defaultAdminEmail || user.isSso) {
throw Errors.NOT_ENOUGH_RIGHTS;
}
if (
inputs.id === currentUser.id &&
!bcrypt.compareSync(inputs.currentPassword, user.password)
) {
2020-04-03 00:35:25 +05:00
throw Errors.INVALID_CURRENT_PASSWORD;
}
const values = _.pick(inputs, ['email']);
2022-12-26 21:10:50 +01:00
user = await sails.helpers.users.updateOne
.with({
values,
record: user,
user: currentUser,
request: this.req,
})
2020-04-03 00:35:25 +05:00
.intercept('emailAlreadyInUse', () => Errors.EMAIL_ALREADY_IN_USE);
if (!user) {
throw Errors.USER_NOT_FOUND;
}
return {
2020-05-26 00:46:04 +05:00
item: user,
};
},
};