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

171 lines
3.7 KiB
JavaScript
Raw Normal View History

/*!
* Copyright (c) 2024 PLANKA Software GmbH
* Licensed under the Fair Use License: https://github.com/plankanban/planka/blob/master/LICENSE.md
*/
const { idInput } = require('../../../utils/inputs');
2019-08-31 04:07:25 +05:00
const Errors = {
NOT_ENOUGH_RIGHTS: {
notEnoughRights: 'Not enough rights',
},
2019-08-31 04:07:25 +05:00
USER_NOT_FOUND: {
2020-04-03 00:35:25 +05:00
userNotFound: 'User not found',
},
ACTIVE_LIMIT_REACHED: {
activeLimitReached: 'Active limit reached',
},
2019-08-31 04:07:25 +05:00
};
module.exports = {
inputs: {
id: {
...idInput,
required: true,
2019-08-31 04:07:25 +05:00
},
role: {
type: 'string',
isIn: Object.values(User.Roles),
2019-08-31 04:07:25 +05:00
},
name: {
type: 'string',
isNotEmptyString: true,
maxLength: 128,
2019-08-31 04:07:25 +05:00
},
avatar: {
2019-08-31 04:07:25 +05:00
type: 'json',
custom: _.isNull,
},
phone: {
type: 'string',
isNotEmptyString: true,
maxLength: 128,
allowNull: true,
},
organization: {
type: 'string',
isNotEmptyString: true,
maxLength: 128,
allowNull: true,
},
language: {
type: 'string',
2024-07-21 19:33:57 +02:00
isIn: User.LANGUAGES,
allowNull: true,
},
subscribeToOwnCards: {
type: 'boolean',
},
subscribeToCardWhenCommenting: {
type: 'boolean',
},
turnOffRecentCardHighlighting: {
type: 'boolean',
},
enableFavoritesByDefault: {
type: 'boolean',
},
defaultEditorMode: {
type: 'string',
isIn: Object.values(User.EditorModes),
},
defaultHomeView: {
type: 'string',
isIn: Object.values(User.HomeViews),
},
defaultProjectsOrder: {
type: 'string',
isIn: Object.values(User.ProjectOrders),
},
isDeactivated: {
type: 'boolean',
},
2019-08-31 04:07:25 +05:00
},
exits: {
notEnoughRights: {
responseType: 'forbidden',
},
2020-04-03 00:35:25 +05:00
userNotFound: {
responseType: 'notFound',
},
activeLimitReached: {
responseType: 'conflict',
},
2019-08-31 04:07:25 +05:00
},
async fn(inputs) {
2019-08-31 04:07:25 +05:00
const { currentUser } = this.req;
const availableInputKeys = ['id', 'name', 'avatar', 'phone', 'organization'];
if (inputs.id === currentUser.id) {
availableInputKeys.push(...User.PERSONAL_FIELD_NAMES);
} else if (currentUser.role === User.Roles.ADMIN) {
availableInputKeys.push('role', 'isDeactivated');
} else {
throw Errors.USER_NOT_FOUND; // Forbidden
}
2019-08-31 04:07:25 +05:00
if (_.difference(Object.keys(inputs), availableInputKeys).length > 0) {
throw Errors.NOT_ENOUGH_RIGHTS;
2019-08-31 04:07:25 +05:00
}
let user = await User.qm.getOneById(inputs.id);
2019-08-31 04:07:25 +05:00
if (!user) {
throw Errors.USER_NOT_FOUND;
}
// TODO: refactor
if (user.email === sails.config.custom.defaultAdminEmail) {
if (inputs.role || inputs.name) {
throw Errors.NOT_ENOUGH_RIGHTS;
}
} else if (user.isSsoUser) {
if (!sails.config.custom.oidcIgnoreRoles && inputs.role) {
throw Errors.NOT_ENOUGH_RIGHTS;
}
if (inputs.name) {
throw Errors.NOT_ENOUGH_RIGHTS;
}
}
const values = {
..._.pick(inputs, [
'role',
'name',
'avatar',
'phone',
'organization',
'language',
'subscribeToOwnCards',
'subscribeToCardWhenCommenting',
'turnOffRecentCardHighlighting',
'enableFavoritesByDefault',
'defaultEditorMode',
'defaultHomeView',
'defaultProjectsOrder',
'isDeactivated',
]),
};
2019-08-31 04:07:25 +05:00
user = await sails.helpers.users.updateOne
.with({
values,
record: user,
actorUser: currentUser,
request: this.req,
})
.intercept('activeLimitReached', () => Errors.ACTIVE_LIMIT_REACHED);
2019-08-31 04:07:25 +05:00
if (!user) {
throw Errors.USER_NOT_FOUND;
}
return {
item: sails.helpers.users.presentOne(user, currentUser),
};
},
2019-08-31 04:07:25 +05:00
};