From 3e8b3d565b31b94af46c65f0986c332e86b4efc5 Mon Sep 17 00:00:00 2001 From: Alexandre Bodin Date: Mon, 18 Mar 2024 21:44:32 +0100 Subject: [PATCH] chore: update u&p --- .../EditView/components/DocumentActions.tsx | 1 + .../components/FormInputs/Relations.tsx | 2 +- .../server/src/controllers/relations.ts | 2 +- .../server/src/middlewares/routing.ts | 14 +++--- .../controllers/content-manager-user.js | 44 +++++++++---------- 5 files changed, 29 insertions(+), 34 deletions(-) diff --git a/packages/core/admin/admin/src/content-manager/pages/EditView/components/DocumentActions.tsx b/packages/core/admin/admin/src/content-manager/pages/EditView/components/DocumentActions.tsx index df226fa1fa..6bffd61d2d 100644 --- a/packages/core/admin/admin/src/content-manager/pages/EditView/components/DocumentActions.tsx +++ b/packages/core/admin/admin/src/content-manager/pages/EditView/components/DocumentActions.tsx @@ -641,6 +641,7 @@ const UpdateAction: DocumentActionComponent = ({ }), onClick: async () => { setSubmitting(true); + try { const { errors } = await validate(); diff --git a/packages/core/admin/admin/src/content-manager/pages/EditView/components/FormInputs/Relations.tsx b/packages/core/admin/admin/src/content-manager/pages/EditView/components/FormInputs/Relations.tsx index 86d32baad2..d35dea3cdc 100644 --- a/packages/core/admin/admin/src/content-manager/pages/EditView/components/FormInputs/Relations.tsx +++ b/packages/core/admin/admin/src/content-manager/pages/EditView/components/FormInputs/Relations.tsx @@ -228,7 +228,7 @@ const RelationsField = React.forwardRef( }; if (ONE_WAY_RELATIONS.includes(props.attribute.relation)) { - field.onChange(props.name, { connect: [item] }); + field.onChange(`${props.name}.connect`, [item]); } else { field.onChange(`${props.name}.connect`, [...(field.value?.connect ?? []), item]); } diff --git a/packages/core/content-manager/server/src/controllers/relations.ts b/packages/core/content-manager/server/src/controllers/relations.ts index 86da98102b..dc75cb7158 100644 --- a/packages/core/content-manager/server/src/controllers/relations.ts +++ b/packages/core/content-manager/server/src/controllers/relations.ts @@ -91,7 +91,7 @@ const getPublishedAtClause = ( * As it only contains entries with publishedAt set. */ if (!contentTypes.hasDraftAndPublish(targetModel)) { - return { $ne: null }; + return {}; } /** diff --git a/packages/core/content-manager/server/src/middlewares/routing.ts b/packages/core/content-manager/server/src/middlewares/routing.ts index 23cb950ea3..33ee66386e 100644 --- a/packages/core/content-manager/server/src/middlewares/routing.ts +++ b/packages/core/content-manager/server/src/middlewares/routing.ts @@ -30,14 +30,12 @@ export default async (ctx: Context, next: Next) => { const [, action] = route.handler.split('.'); - const configPath = - ct.plugin === 'admin' - ? ['admin.layout', ct.modelName, 'actions', action] - : ['plugin', ct.plugin, 'layout', ct.modelName, 'actions', action]; - - // TODO - // @ts-expect-error check input for strapi.config.get - const actionConfig: string | undefined = strapi.config.get(configPath); + let actionConfig: any; + if (!ct.plugin || ct.plugin === 'admin') { + actionConfig = strapi.config.get(`admin.layout.${ct.modelName}.actions.${action}`); + } else { + actionConfig = strapi.plugin(ct.plugin).config(`layout.${ct.modelName}.actions.${action}`); + } if (!isNil(actionConfig)) { const [controller, action] = actionConfig.split('.'); diff --git a/packages/plugins/users-permissions/server/controllers/content-manager-user.js b/packages/plugins/users-permissions/server/controllers/content-manager-user.js index 3c4af0e0ed..050a90d202 100644 --- a/packages/plugins/users-permissions/server/controllers/content-manager-user.js +++ b/packages/plugins/users-permissions/server/controllers/content-manager-user.js @@ -17,24 +17,23 @@ const ACTIONS = { }; const findEntityAndCheckPermissions = async (ability, action, model, id) => { - const entity = await strapi.db.query(userModel).findOne({ - where: { id }, + const doc = await strapi.service('plugin::content-manager.document-manager').findOne(id, model, { populate: [`${CREATED_BY_ATTRIBUTE}.roles`], }); - if (_.isNil(entity)) { + if (_.isNil(doc)) { throw new NotFoundError(); } const pm = strapi.admin.services.permission.createPermissionsManager({ ability, action, model }); - if (pm.ability.cannot(pm.action, pm.toSubject(entity))) { + if (pm.ability.cannot(pm.action, pm.toSubject(doc))) { throw new ForbiddenError(); } - const entityWithoutCreatorRoles = _.omit(entity, `${CREATED_BY_ATTRIBUTE}.roles`); + const docWithoutCreatorRoles = _.omit(doc, `${CREATED_BY_ATTRIBUTE}.roles`); - return { pm, entity: entityWithoutCreatorRoles }; + return { pm, doc: docWithoutCreatorRoles }; }; module.exports = { @@ -93,18 +92,11 @@ module.exports = { user.email = _.toLower(user.email); - if (!user.role) { - const defaultRole = await strapi.db - .query('plugin::users-permissions.role') - .findOne({ where: { type: advanced.default_role } }); - - user.role = defaultRole.id; - } - try { const data = await strapi - .service('plugin::content-manager.entity-manager') - .create(user, userModel); + .service('plugin::content-manager.document-manager') + .create(userModel, { data: user }); + const sanitizedData = await pm.sanitizeOutput(data, { action: ACTIONS.read }); ctx.created(sanitizedData); @@ -118,7 +110,7 @@ module.exports = { */ async update(ctx) { - const { id } = ctx.params; + const { id: documentId } = ctx.params; const { body } = ctx.request; const { user: admin, userAbility } = ctx.state; @@ -128,13 +120,14 @@ module.exports = { const { email, username, password } = body; - const { pm, entity } = await findEntityAndCheckPermissions( + const { pm, doc } = await findEntityAndCheckPermissions( userAbility, ACTIONS.edit, userModel, - id + documentId ); - const user = entity; + + const user = doc; await validateUpdateUserBody(ctx.request.body); @@ -147,7 +140,7 @@ module.exports = { .query('plugin::users-permissions.user') .findOne({ where: { username } }); - if (userWithSameUsername && _.toString(userWithSameUsername.id) !== _.toString(id)) { + if (userWithSameUsername && _.toString(userWithSameUsername.id) !== _.toString(user.id)) { throw new ApplicationError('Username already taken'); } } @@ -157,9 +150,10 @@ module.exports = { .query('plugin::users-permissions.user') .findOne({ where: { email: _.toLower(email) } }); - if (userWithSameEmail && _.toString(userWithSameEmail.id) !== _.toString(id)) { + if (userWithSameEmail && _.toString(userWithSameEmail.id) !== _.toString(user.id)) { throw new ApplicationError('Email already taken'); } + body.email = _.toLower(body.email); } @@ -167,8 +161,10 @@ module.exports = { const updateData = _.omit({ ...sanitizedData, updatedBy: admin.id }, 'createdBy'); const data = await strapi - .service('plugin::content-manager.entity-manager') - .update({ id }, updateData, userModel); + .service('plugin::content-manager.document-manager') + .update(documentId, userModel, { + data: updateData, + }); ctx.body = await pm.sanitizeOutput(data, { action: ACTIONS.read }); },