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.js

107 lines
2 KiB
JavaScript
Raw Normal View History

2019-08-31 04:07:25 +05:00
const Errors = {
USER_NOT_FOUND: {
2020-04-03 00:35:25 +05:00
userNotFound: 'User not found',
},
2019-08-31 04:07:25 +05:00
};
2022-12-26 21:10:50 +01:00
const avatarUrlValidator = (value) => _.isNull(value);
2019-08-31 04:07:25 +05:00
module.exports = {
inputs: {
id: {
type: 'string',
regex: /^[0-9]+$/,
required: true,
2019-08-31 04:07:25 +05:00
},
isAdmin: {
type: 'boolean',
2019-08-31 04:07:25 +05:00
},
name: {
type: 'string',
isNotEmptyString: true,
2019-08-31 04:07:25 +05:00
},
2020-04-21 05:04:34 +05:00
avatarUrl: {
2019-08-31 04:07:25 +05:00
type: 'json',
2022-12-26 21:10:50 +01:00
custom: avatarUrlValidator,
},
phone: {
type: 'string',
isNotEmptyString: true,
allowNull: true,
},
organization: {
type: 'string',
isNotEmptyString: true,
allowNull: true,
},
language: {
type: 'string',
isNotEmptyString: true,
allowNull: true,
},
subscribeToOwnCards: {
type: 'boolean',
},
2019-08-31 04:07:25 +05:00
},
exits: {
2020-04-03 00:35:25 +05:00
userNotFound: {
responseType: 'notFound',
},
2019-08-31 04:07:25 +05:00
},
async fn(inputs) {
2019-08-31 04:07:25 +05:00
const { currentUser } = this.req;
if (!currentUser.isAdmin) {
if (inputs.id !== currentUser.id) {
throw Errors.USER_NOT_FOUND; // Forbidden
}
delete inputs.isAdmin; // eslint-disable-line no-param-reassign
2019-08-31 04:07:25 +05:00
}
let user = await sails.helpers.users.getOne(inputs.id);
2019-08-31 04:07:25 +05:00
if (!user) {
throw Errors.USER_NOT_FOUND;
}
if (user.email === sails.config.custom.defaultAdminEmail) {
/* eslint-disable no-param-reassign */
delete inputs.isAdmin;
delete inputs.name;
/* eslint-enable no-param-reassign */
2023-10-17 19:18:19 +02:00
} else if (user.isSso) {
delete inputs.name; // eslint-disable-line no-param-reassign
}
const values = {
..._.pick(inputs, [
'isAdmin',
'name',
'phone',
'organization',
'language',
'subscribeToOwnCards',
]),
avatar: inputs.avatarUrl,
};
2019-08-31 04:07:25 +05:00
2022-12-26 21:10:50 +01:00
user = await sails.helpers.users.updateOne.with({
values,
record: user,
user: currentUser,
request: this.req,
});
2019-08-31 04:07:25 +05:00
if (!user) {
throw Errors.USER_NOT_FOUND;
}
return {
item: user,
};
},
2019-08-31 04:07:25 +05:00
};