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

102 lines
2 KiB
JavaScript
Raw Normal View History

const zxcvbn = require('zxcvbn');
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',
},
2019-08-31 04:07:25 +05:00
};
2022-12-26 21:10:50 +01:00
const passwordValidator = (value) => zxcvbn(value).score >= 2; // TODO: move to config
2019-08-31 04:07:25 +05:00
module.exports = {
inputs: {
email: {
type: 'string',
isEmail: true,
required: true,
2019-08-31 04:07:25 +05:00
},
password: {
type: 'string',
2022-12-26 21:10:50 +01:00
custom: passwordValidator,
required: true,
2019-08-31 04:07:25 +05:00
},
name: {
type: 'string',
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,
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: {
notEnoughRights: {
responseType: 'forbidden',
},
2020-04-03 00:35:25 +05:00
emailAlreadyInUse: {
responseType: 'conflict',
},
usernameAlreadyInUse: {
responseType: 'conflict',
},
2019-08-31 04:07:25 +05:00
},
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',
]);
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,
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);
2019-08-31 04:07:25 +05:00
return {
item: user,
};
},
2019-08-31 04:07:25 +05:00
};