2025-05-10 02:09:06 +02:00
|
|
|
/*!
|
|
|
|
* Copyright (c) 2024 PLANKA Software GmbH
|
|
|
|
* Licensed under the Fair Use License: https://github.com/plankanban/planka/blob/master/LICENSE.md
|
|
|
|
*/
|
2020-08-04 01:32:46 +05:00
|
|
|
|
2025-05-10 02:09:06 +02:00
|
|
|
const bcrypt = require('bcrypt');
|
2020-08-04 01:32:46 +05:00
|
|
|
|
2022-12-26 21:10:50 +01:00
|
|
|
module.exports = {
|
|
|
|
inputs: {
|
|
|
|
values: {
|
|
|
|
type: 'json',
|
2019-11-05 18:01:42 +05:00
|
|
|
required: true,
|
2019-08-31 04:07:25 +05:00
|
|
|
},
|
2024-06-12 00:51:36 +02:00
|
|
|
actorUser: {
|
|
|
|
type: 'ref',
|
|
|
|
required: true,
|
|
|
|
},
|
2019-08-31 04:07:25 +05:00
|
|
|
request: {
|
2019-11-05 18:01:42 +05:00
|
|
|
type: 'ref',
|
|
|
|
},
|
2019-08-31 04:07:25 +05:00
|
|
|
},
|
|
|
|
|
|
|
|
exits: {
|
2020-04-03 00:35:25 +05:00
|
|
|
emailAlreadyInUse: {},
|
|
|
|
usernameAlreadyInUse: {},
|
2025-05-10 02:09:06 +02:00
|
|
|
activeLimitReached: {},
|
2019-08-31 04:07:25 +05:00
|
|
|
},
|
|
|
|
|
2021-06-24 01:05:22 +05:00
|
|
|
async fn(inputs) {
|
2022-12-26 21:10:50 +01:00
|
|
|
const { values } = inputs;
|
|
|
|
|
2023-10-17 19:18:19 +02:00
|
|
|
if (values.password) {
|
2025-05-10 02:09:06 +02:00
|
|
|
values.password = await bcrypt.hash(values.password, 10);
|
2023-10-17 19:18:19 +02:00
|
|
|
}
|
|
|
|
|
2022-12-26 21:10:50 +01:00
|
|
|
if (values.username) {
|
|
|
|
values.username = values.username.toLowerCase();
|
2020-04-03 00:35:25 +05:00
|
|
|
}
|
|
|
|
|
2025-05-10 02:09:06 +02:00
|
|
|
let user;
|
|
|
|
try {
|
|
|
|
user = await User.qm.createOne({
|
|
|
|
...values,
|
|
|
|
email: values.email.toLowerCase(),
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
if (error.code === 'E_UNIQUE') {
|
|
|
|
throw 'emailAlreadyInUse';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
error.name === 'AdapterError' &&
|
|
|
|
error.raw.constraint === 'user_account_username_unique'
|
|
|
|
) {
|
|
|
|
throw 'usernameAlreadyInUse';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error.message === 'activeLimitReached') {
|
|
|
|
throw 'activeLimitReached';
|
|
|
|
}
|
|
|
|
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
|
|
|
const scoper = sails.helpers.users.makeScoper(user);
|
|
|
|
const privateUserRelatedUserIds = await scoper.getPrivateUserRelatedUserIds();
|
|
|
|
|
|
|
|
privateUserRelatedUserIds.forEach((userId) => {
|
|
|
|
sails.sockets.broadcast(
|
|
|
|
`user:${userId}`,
|
|
|
|
'userCreate',
|
2020-04-03 00:35:25 +05:00
|
|
|
{
|
2025-05-10 02:09:06 +02:00
|
|
|
// FIXME: hack
|
|
|
|
item: sails.helpers.users.presentOne(user, {
|
|
|
|
id: userId,
|
|
|
|
role: User.Roles.ADMIN,
|
|
|
|
}),
|
2020-04-03 00:35:25 +05:00
|
|
|
},
|
2025-05-10 02:09:06 +02:00
|
|
|
inputs.request,
|
|
|
|
);
|
|
|
|
});
|
2021-06-24 01:05:22 +05:00
|
|
|
|
2025-05-10 02:09:06 +02:00
|
|
|
const publicUserRelatedUserIds = await scoper.getPublicUserRelatedUserIds(true);
|
2019-08-31 04:07:25 +05:00
|
|
|
|
2025-05-10 02:09:06 +02:00
|
|
|
publicUserRelatedUserIds.forEach((userId) => {
|
2019-08-31 04:07:25 +05:00
|
|
|
sails.sockets.broadcast(
|
|
|
|
`user:${userId}`,
|
|
|
|
'userCreate',
|
|
|
|
{
|
2025-05-10 02:09:06 +02:00
|
|
|
// FIXME: hack
|
|
|
|
item: sails.helpers.users.presentOne(user, {
|
|
|
|
id: userId,
|
|
|
|
}),
|
2019-08-31 04:07:25 +05:00
|
|
|
},
|
2019-11-05 18:01:42 +05:00
|
|
|
inputs.request,
|
2019-08-31 04:07:25 +05:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2025-07-04 22:04:11 +02:00
|
|
|
const webhooks = await Webhook.qm.getAll();
|
|
|
|
|
2024-06-12 00:51:36 +02:00
|
|
|
sails.helpers.utils.sendWebhooks.with({
|
2025-07-04 22:04:11 +02:00
|
|
|
webhooks,
|
|
|
|
event: Webhook.Events.USER_CREATE,
|
2025-05-10 02:09:06 +02:00
|
|
|
buildData: () => ({
|
|
|
|
item: sails.helpers.users.presentOne(user),
|
|
|
|
}),
|
2024-06-12 00:51:36 +02:00
|
|
|
user: inputs.actorUser,
|
2024-06-06 20:22:14 +02:00
|
|
|
});
|
|
|
|
|
2021-06-24 01:05:22 +05:00
|
|
|
return user;
|
2019-11-05 18:01:42 +05:00
|
|
|
},
|
2019-08-31 04:07:25 +05:00
|
|
|
};
|