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

134 lines
2.8 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 { isPassword } = require('../../../utils/validators');
2019-08-31 04:07:25 +05:00
const Errors = {
NOT_ENOUGH_RIGHTS: {
notEnoughRights: 'Not enough rights',
},
2020-04-03 00:35:25 +05:00
EMAIL_ALREADY_IN_USE: {
emailAlreadyInUse: 'Email already in use',
},
USERNAME_ALREADY_IN_USE: {
usernameAlreadyInUse: 'Username already in use',
},
ACTIVE_LIMIT_REACHED: {
activeLimitReached: 'Active limit reached',
},
2019-08-31 04:07:25 +05:00
};
module.exports = {
inputs: {
email: {
type: 'string',
maxLength: 256,
2019-08-31 04:07:25 +05:00
isEmail: true,
required: true,
2019-08-31 04:07:25 +05:00
},
password: {
type: 'string',
maxLength: 256,
custom: isPassword,
required: true,
},
role: {
type: 'string',
isIn: Object.values(User.Roles),
required: true,
2019-08-31 04:07:25 +05:00
},
name: {
type: 'string',
maxLength: 128,
required: true,
},
2020-04-03 00:35:25 +05:00
username: {
type: 'string',
isNotEmptyString: true,
minLength: 3,
maxLength: 16,
2021-04-13 18:59:02 +05:00
regex: /^[a-zA-Z0-9]+((_|\.)?[a-zA-Z0-9])*$/,
2020-04-03 00:35:25 +05:00
allowNull: true,
},
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',
},
2019-08-31 04:07:25 +05:00
},
exits: {
notEnoughRights: {
responseType: 'forbidden',
},
2020-04-03 00:35:25 +05:00
emailAlreadyInUse: {
responseType: 'conflict',
},
usernameAlreadyInUse: {
responseType: 'conflict',
},
activeLimitReached: {
responseType: 'conflict',
},
2019-08-31 04:07:25 +05:00
},
async fn(inputs) {
const { currentUser } = this.req;
if (sails.config.custom.oidcEnforced) {
throw Errors.NOT_ENOUGH_RIGHTS;
}
const values = _.pick(inputs, [
'email',
'password',
'role',
'name',
'username',
'phone',
'organization',
'language',
'subscribeToOwnCards',
'subscribeToCardWhenCommenting',
'turnOffRecentCardHighlighting',
]);
2019-08-31 04:07:25 +05:00
2022-12-26 21:10:50 +01:00
const user = await sails.helpers.users.createOne
.with({
values,
actorUser: currentUser,
2022-12-26 21:10:50 +01:00
request: this.req,
})
2020-04-03 00:35:25 +05:00
.intercept('emailAlreadyInUse', () => Errors.EMAIL_ALREADY_IN_USE)
.intercept('usernameAlreadyInUse', () => Errors.USERNAME_ALREADY_IN_USE)
.intercept('activeLimitReached', () => Errors.ACTIVE_LIMIT_REACHED);
2019-08-31 04:07:25 +05:00
return {
item: sails.helpers.users.presentOne(user, currentUser),
};
},
2019-08-31 04:07:25 +05:00
};