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

91 lines
1.6 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
};
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',
2020-04-03 00:35:25 +05:00
custom: (value) => _.isNull(value),
},
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;
}
const values = {
..._.pick(inputs, [
'isAdmin',
'name',
'phone',
'organization',
'language',
'subscribeToOwnCards',
]),
avatar: inputs.avatarUrl,
};
2019-08-31 04:07:25 +05:00
user = await sails.helpers.users.updateOne(user, values, currentUser, 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
};