1
0
Fork 0
mirror of https://github.com/plankanban/planka.git synced 2025-07-24 07:39:44 +02:00

Add username to user

This commit is contained in:
Maksim Eltyshev 2020-04-03 00:35:25 +05:00
parent ec222a5e32
commit af00e3e191
143 changed files with 1051 additions and 420 deletions

View file

@ -1,6 +1,9 @@
const Errors = {
USER_EXIST: {
conflict: 'User is already exist',
EMAIL_ALREADY_IN_USE: {
emailAlreadyInUse: 'Email already in use',
},
USERNAME_ALREADY_IN_USE: {
usernameAlreadyInUse: 'Username already in use',
},
};
@ -19,20 +22,32 @@ module.exports = {
type: 'string',
required: true,
},
username: {
type: 'string',
isNotEmptyString: true,
minLength: 3,
maxLength: 16,
regex: /^[a-zA-Z0-9]+(_?[a-zA-Z0-9])*$/,
allowNull: true,
},
},
exits: {
conflict: {
emailAlreadyInUse: {
responseType: 'conflict',
},
usernameAlreadyInUse: {
responseType: 'conflict',
},
},
async fn(inputs, exits) {
const values = _.pick(inputs, ['email', 'password', 'name']);
const values = _.pick(inputs, ['email', 'password', 'name', 'username']);
const user = await sails.helpers
.createUser(values, this.req)
.intercept('conflict', () => Errors.USER_EXIST);
.intercept('emailAlreadyInUse', () => Errors.EMAIL_ALREADY_IN_USE)
.intercept('usernameAlreadyInUse', () => Errors.USERNAME_ALREADY_IN_USE);
return exits.success({
item: user,

View file

@ -1,6 +1,6 @@
const Errors = {
USER_NOT_FOUND: {
notFound: 'User is not found',
userNotFound: 'User not found',
},
};
@ -14,7 +14,7 @@ module.exports = {
},
exits: {
notFound: {
userNotFound: {
responseType: 'notFound',
},
},

View file

@ -2,13 +2,13 @@ const bcrypt = require('bcrypt');
const Errors = {
USER_NOT_FOUND: {
notFound: 'User is not found',
userNotFound: 'User not found',
},
CURRENT_PASSWORD_NOT_VALID: {
forbidden: 'Current password is not valid',
INVALID_CURRENT_PASSWORD: {
invalidCurrentPassword: 'Invalid current password',
},
USER_EXIST: {
conflict: 'User is already exist',
EMAIL_ALREADY_IN_USE: {
emailAlreadyInUse: 'Email already in use',
},
};
@ -31,13 +31,13 @@ module.exports = {
},
exits: {
notFound: {
userNotFound: {
responseType: 'notFound',
},
forbidden: {
invalidCurrentPassword: {
responseType: 'forbidden',
},
conflict: {
emailAlreadyInUse: {
responseType: 'conflict',
},
},
@ -47,7 +47,7 @@ module.exports = {
if (inputs.id === currentUser.id) {
if (!inputs.currentPassword) {
throw Errors.CURRENT_PASSWORD_NOT_VALID;
throw Errors.INVALID_CURRENT_PASSWORD;
}
} else if (!currentUser.isAdmin) {
throw Errors.USER_NOT_FOUND; // Forbidden
@ -63,14 +63,14 @@ module.exports = {
inputs.id === currentUser.id &&
!bcrypt.compareSync(inputs.currentPassword, user.password)
) {
throw Errors.CURRENT_PASSWORD_NOT_VALID;
throw Errors.INVALID_CURRENT_PASSWORD;
}
const values = _.pick(inputs, ['email']);
user = await sails.helpers
.updateUser(user, values, this.req)
.intercept('conflict', () => Errors.USER_EXIST);
.intercept('emailAlreadyInUse', () => Errors.EMAIL_ALREADY_IN_USE);
if (!user) {
throw Errors.USER_NOT_FOUND;

View file

@ -2,10 +2,10 @@ const bcrypt = require('bcrypt');
const Errors = {
USER_NOT_FOUND: {
notFound: 'User is not found',
userNotFound: 'User not found',
},
CURRENT_PASSWORD_NOT_VALID: {
forbidden: 'Current password is not valid',
INVALID_CURRENT_PASSWORD: {
invalidCurrentPassword: 'Invalid current password',
},
};
@ -27,10 +27,10 @@ module.exports = {
},
exits: {
notFound: {
userNotFound: {
responseType: 'notFound',
},
forbidden: {
invalidCurrentPassword: {
responseType: 'forbidden',
},
},
@ -40,7 +40,7 @@ module.exports = {
if (inputs.id === currentUser.id) {
if (!inputs.currentPassword) {
throw Errors.CURRENT_PASSWORD_NOT_VALID;
throw Errors.INVALID_CURRENT_PASSWORD;
}
} else if (!currentUser.isAdmin) {
throw Errors.USER_NOT_FOUND; // Forbidden
@ -56,7 +56,7 @@ module.exports = {
inputs.id === currentUser.id &&
!bcrypt.compareSync(inputs.currentPassword, user.password)
) {
throw Errors.CURRENT_PASSWORD_NOT_VALID;
throw Errors.INVALID_CURRENT_PASSWORD;
}
const values = _.pick(inputs, ['password']);

View file

@ -0,0 +1,85 @@
const bcrypt = require('bcrypt');
const Errors = {
USER_NOT_FOUND: {
userNotFound: 'User not found',
},
INVALID_CURRENT_PASSWORD: {
invalidCurrentPassword: 'Invalid current password',
},
USERNAME_ALREADY_IN_USE: {
usernameAlreadyInUse: 'Username already in use',
},
};
module.exports = {
inputs: {
id: {
type: 'string',
regex: /^[0-9]+$/,
required: true,
},
username: {
isNotEmptyString: true,
minLength: 3,
maxLength: 16,
regex: /^[a-zA-Z0-9]+(_?[a-zA-Z0-9])*$/,
allowNull: true,
},
currentPassword: {
type: 'string',
isNotEmptyString: true,
},
},
exits: {
userNotFound: {
responseType: 'notFound',
},
invalidCurrentPassword: {
responseType: 'forbidden',
},
usernameAlreadyInUse: {
responseType: 'conflict',
},
},
async fn(inputs, exits) {
const { currentUser } = this.req;
if (inputs.id === currentUser.id) {
if (!inputs.currentPassword) {
throw Errors.INVALID_CURRENT_PASSWORD;
}
} else if (!currentUser.isAdmin) {
throw Errors.USER_NOT_FOUND; // Forbidden
}
let user = await sails.helpers.getUser(inputs.id);
if (!user) {
throw Errors.USER_NOT_FOUND;
}
if (
inputs.id === currentUser.id &&
!bcrypt.compareSync(inputs.currentPassword, user.password)
) {
throw Errors.INVALID_CURRENT_PASSWORD;
}
const values = _.pick(inputs, ['username']);
user = await sails.helpers
.updateUser(user, values, this.req)
.intercept('usernameAlreadyInUse', () => Errors.USERNAME_ALREADY_IN_USE);
if (!user) {
throw Errors.USER_NOT_FOUND;
}
return exits.success({
item: user.username,
});
},
};

View file

@ -1,6 +1,6 @@
const Errors = {
USER_NOT_FOUND: {
notFound: 'User is not found',
userNotFound: 'User not found',
},
};
@ -20,12 +20,12 @@ module.exports = {
},
avatar: {
type: 'json',
custom: value => _.isNull(value),
custom: (value) => _.isNull(value),
},
},
exits: {
notFound: {
userNotFound: {
responseType: 'notFound',
},
},

View file

@ -7,7 +7,7 @@ const sharp = require('sharp');
const Errors = {
USER_NOT_FOUND: {
notFound: 'User is not found',
userNotFound: 'User not found',
},
};
@ -32,9 +32,7 @@ const createReceiver = () => {
}
firstFileHandled = true;
const resize = sharp()
.resize(36, 36)
.jpeg();
const resize = sharp().resize(36, 36).jpeg();
const transform = new stream.Transform({
transform(chunk, streamEncoding, callback) {
@ -71,10 +69,10 @@ module.exports = {
},
exits: {
notFound: {
userNotFound: {
responseType: 'notFound',
},
unprocessableEntity: {
uploadError: {
responseType: 'unprocessableEntity',
},
},
@ -97,11 +95,11 @@ module.exports = {
this.req.file('file').upload(createReceiver(), async (error, files) => {
if (error) {
return exits.unprocessableEntity(error.message);
return exits.uploadError(error.message);
}
if (files.length === 0) {
return exits.unprocessableEntity('No file was uploaded');
return exits.uploadError('No file was uploaded');
}
user = await sails.helpers.updateUser(