174 lines
5.0 KiB
JavaScript
Raw Permalink Normal View History

'use strict';
const _ = require('lodash');
2021-04-29 13:51:12 +02:00
const { contentTypes: contentTypesUtils } = require('@strapi/utils');
2022-08-08 23:33:39 +02:00
const { ApplicationError, ValidationError, NotFoundError, ForbiddenError } =
require('@strapi/utils').errors;
const { validateCreateUserBody, validateUpdateUserBody } = require('./validation/user');
const { UPDATED_BY_ATTRIBUTE, CREATED_BY_ATTRIBUTE } = contentTypesUtils.constants;
2021-08-06 18:09:49 +02:00
const userModel = 'plugin::users-permissions.user';
const ACTIONS = {
2021-08-06 18:09:49 +02:00
read: 'plugin::content-manager.explorer.read',
create: 'plugin::content-manager.explorer.create',
edit: 'plugin::content-manager.explorer.update',
delete: 'plugin::content-manager.explorer.delete',
};
const findEntityAndCheckPermissions = async (ability, action, model, id) => {
2024-03-18 21:44:32 +01:00
const doc = await strapi.service('plugin::content-manager.document-manager').findOne(id, model, {
populate: [`${CREATED_BY_ATTRIBUTE}.roles`],
});
2024-03-18 21:44:32 +01:00
if (_.isNil(doc)) {
2021-10-20 17:30:05 +02:00
throw new NotFoundError();
}
const pm = strapi
.service('admin::permission')
.createPermissionsManager({ ability, action, model });
2024-03-18 21:44:32 +01:00
if (pm.ability.cannot(pm.action, pm.toSubject(doc))) {
2021-10-20 17:30:05 +02:00
throw new ForbiddenError();
}
2024-03-18 21:44:32 +01:00
const docWithoutCreatorRoles = _.omit(doc, `${CREATED_BY_ATTRIBUTE}.roles`);
2024-03-18 21:44:32 +01:00
return { pm, doc: docWithoutCreatorRoles };
};
module.exports = {
/**
* Create a/an user record.
* @return {Object}
*/
async create(ctx) {
2021-09-24 09:04:44 +02:00
const { body } = ctx.request;
const { user: admin, userAbility } = ctx.state;
2021-10-20 17:30:05 +02:00
const { email, username } = body;
const pm = strapi.service('admin::permission').createPermissionsManager({
ability: userAbility,
action: ACTIONS.create,
model: userModel,
});
if (!pm.isAllowed) {
2021-10-20 17:30:05 +02:00
return ctx.forbidden();
}
const sanitizedBody = await pm.pickPermittedFieldsOf(body, { subject: userModel });
const advanced = await strapi
.store({ type: 'plugin', name: 'users-permissions', key: 'advanced' })
.get();
2021-10-20 17:30:05 +02:00
await validateCreateUserBody(ctx.request.body);
2024-03-13 15:40:30 +01:00
const userWithSameUsername = await strapi.db
2021-08-06 18:09:49 +02:00
.query('plugin::users-permissions.user')
2021-07-08 18:15:32 +02:00
.findOne({ where: { username } });
if (userWithSameUsername) {
2021-10-20 17:30:05 +02:00
throw new ApplicationError('Username already taken');
}
if (advanced.unique_email) {
2024-03-13 15:40:30 +01:00
const userWithSameEmail = await strapi.db
2021-08-06 18:09:49 +02:00
.query('plugin::users-permissions.user')
2021-07-08 18:15:32 +02:00
.findOne({ where: { email: email.toLowerCase() } });
if (userWithSameEmail) {
2021-10-20 17:30:05 +02:00
throw new ApplicationError('Email already taken');
}
}
const user = {
...sanitizedBody,
provider: 'local',
[CREATED_BY_ATTRIBUTE]: admin.id,
[UPDATED_BY_ATTRIBUTE]: admin.id,
};
2021-09-24 09:04:44 +02:00
user.email = _.toLower(user.email);
try {
const data = await strapi
2024-03-18 21:44:32 +01:00
.service('plugin::content-manager.document-manager')
.create(userModel, { data: user });
const sanitizedData = await pm.sanitizeOutput(data, { action: ACTIONS.read });
ctx.created(sanitizedData);
} catch (error) {
2021-10-20 17:30:05 +02:00
throw new ApplicationError(error.message);
}
},
/**
* Update a/an user record.
* @return {Object}
*/
async update(ctx) {
2024-03-18 21:44:32 +01:00
const { id: documentId } = ctx.params;
2021-09-24 09:04:44 +02:00
const { body } = ctx.request;
const { user: admin, userAbility } = ctx.state;
const advancedConfigs = await strapi
.store({ type: 'plugin', name: 'users-permissions', key: 'advanced' })
.get();
const { email, username, password } = body;
2024-03-18 21:44:32 +01:00
const { pm, doc } = await findEntityAndCheckPermissions(
userAbility,
ACTIONS.edit,
userModel,
2024-03-18 21:44:32 +01:00
documentId
);
2024-03-18 21:44:32 +01:00
const user = doc;
2021-10-20 17:30:05 +02:00
await validateUpdateUserBody(ctx.request.body);
if (_.has(body, 'password') && !password && user.provider === 'local') {
2021-10-20 17:30:05 +02:00
throw new ValidationError('password.notNull');
}
if (_.has(body, 'username')) {
2024-03-13 15:40:30 +01:00
const userWithSameUsername = await strapi.db
2021-08-06 18:09:49 +02:00
.query('plugin::users-permissions.user')
2021-07-08 18:15:32 +02:00
.findOne({ where: { username } });
2024-03-18 21:44:32 +01:00
if (userWithSameUsername && _.toString(userWithSameUsername.id) !== _.toString(user.id)) {
2021-10-20 17:30:05 +02:00
throw new ApplicationError('Username already taken');
}
}
if (_.has(body, 'email') && advancedConfigs.unique_email) {
2024-03-13 15:40:30 +01:00
const userWithSameEmail = await strapi.db
2021-08-06 18:09:49 +02:00
.query('plugin::users-permissions.user')
2021-09-24 09:04:44 +02:00
.findOne({ where: { email: _.toLower(email) } });
2024-03-18 21:44:32 +01:00
if (userWithSameEmail && _.toString(userWithSameEmail.id) !== _.toString(user.id)) {
2021-10-20 17:30:05 +02:00
throw new ApplicationError('Email already taken');
}
2024-03-18 21:44:32 +01:00
2021-09-24 09:04:44 +02:00
body.email = _.toLower(body.email);
}
const sanitizedData = await pm.pickPermittedFieldsOf(body, { subject: pm.toSubject(user) });
2021-09-22 17:04:57 +02:00
const updateData = _.omit({ ...sanitizedData, updatedBy: admin.id }, 'createdBy');
const data = await strapi
2024-03-18 21:44:32 +01:00
.service('plugin::content-manager.document-manager')
.update(documentId, userModel, {
data: updateData,
});
ctx.body = await pm.sanitizeOutput(data, { action: ACTIONS.read });
},
};