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/create.js
Maksim Eltyshev 5e8027f5d6 feat: Add ability to enforce SSO
Closes #543, closes #545
2024-02-01 00:31:15 +01:00

101 lines
2 KiB
JavaScript
Executable file

const zxcvbn = require('zxcvbn');
const Errors = {
NOT_ENOUGH_RIGHTS: {
notEnoughRights: 'Not enough rights',
},
EMAIL_ALREADY_IN_USE: {
emailAlreadyInUse: 'Email already in use',
},
USERNAME_ALREADY_IN_USE: {
usernameAlreadyInUse: 'Username already in use',
},
};
const passwordValidator = (value) => zxcvbn(value).score >= 2; // TODO: move to config
module.exports = {
inputs: {
email: {
type: 'string',
isEmail: true,
required: true,
},
password: {
type: 'string',
custom: passwordValidator,
required: true,
},
name: {
type: 'string',
required: true,
},
username: {
type: 'string',
isNotEmptyString: true,
minLength: 3,
maxLength: 16,
regex: /^[a-zA-Z0-9]+((_|\.)?[a-zA-Z0-9])*$/,
allowNull: true,
},
phone: {
type: 'string',
isNotEmptyString: true,
allowNull: true,
},
organization: {
type: 'string',
isNotEmptyString: true,
allowNull: true,
},
language: {
type: 'string',
isNotEmptyString: true,
allowNull: true,
},
subscribeToOwnCards: {
type: 'boolean',
},
},
exits: {
notEnoughRights: {
responseType: 'forbidden',
},
emailAlreadyInUse: {
responseType: 'conflict',
},
usernameAlreadyInUse: {
responseType: 'conflict',
},
},
async fn(inputs) {
if (sails.config.custom.oidcEnforced) {
throw Errors.NOT_ENOUGH_RIGHTS;
}
const values = _.pick(inputs, [
'email',
'password',
'name',
'username',
'phone',
'organization',
'language',
'subscribeToOwnCards',
]);
const user = await sails.helpers.users.createOne
.with({
values,
request: this.req,
})
.intercept('emailAlreadyInUse', () => Errors.EMAIL_ALREADY_IN_USE)
.intercept('usernameAlreadyInUse', () => Errors.USERNAME_ALREADY_IN_USE);
return {
item: user,
};
},
};