From f0d5be0b37a81fb16aec2bb7fe01ea44622f609e Mon Sep 17 00:00:00 2001 From: Marc-Roig Date: Wed, 30 Aug 2023 15:00:13 +0200 Subject: [PATCH] fix: remove review workflows from users and permissions --- .../WorkflowAttributes/WorkflowAttributes.js | 2 + ...view-workflows-filter-user-content-type.js | 38 +++++++++++++++++++ packages/core/admin/ee/server/register.js | 4 +- .../workflows/content-types.js | 16 ++++++-- .../admin/ee/server/utils/review-workflows.js | 5 ++- 5 files changed, 60 insertions(+), 5 deletions(-) create mode 100644 packages/core/admin/ee/server/migrations/review-workflows-filter-user-content-type.js diff --git a/packages/core/admin/ee/admin/pages/SettingsPage/pages/ReviewWorkflows/components/WorkflowAttributes/WorkflowAttributes.js b/packages/core/admin/ee/admin/pages/SettingsPage/pages/ReviewWorkflows/components/WorkflowAttributes/WorkflowAttributes.js index 46f6c45f4e..ca7d28de46 100644 --- a/packages/core/admin/ee/admin/pages/SettingsPage/pages/ReviewWorkflows/components/WorkflowAttributes/WorkflowAttributes.js +++ b/packages/core/admin/ee/admin/pages/SettingsPage/pages/ReviewWorkflows/components/WorkflowAttributes/WorkflowAttributes.js @@ -98,6 +98,8 @@ export function WorkflowAttributes({ defaultMessage: 'Collection Types', }), children: collectionTypes + // Filter plugin::users-permissions.user content type + .filter(({ uid }) => uid !== 'plugin::users-permissions.user') .sort((a, b) => formatter.compare(a.info.displayName, b.info.displayName)) .map((contentType) => ({ label: contentType.info.displayName, diff --git a/packages/core/admin/ee/server/migrations/review-workflows-filter-user-content-type.js b/packages/core/admin/ee/server/migrations/review-workflows-filter-user-content-type.js new file mode 100644 index 0000000000..6d3703782e --- /dev/null +++ b/packages/core/admin/ee/server/migrations/review-workflows-filter-user-content-type.js @@ -0,0 +1,38 @@ +'use strict'; + +/** + * In 4.13.0 we decided users&permissions user content type will no longer be assigned to a workflow + * This migration cleanly un-assigns the user content type if it was assigned to a workflow + */ +const { isNil } = require('lodash/fp'); +const { getService } = require('../utils'); +const workflowContentTypesManager = require('../services/review-workflows/workflows/content-types'); + +async function migrateWorkflowsFilterUsersContentType({ oldContentTypes, contentTypes }) { + const userHadWorkflowStage = !isNil( + oldContentTypes['plugin::users-permissions.user']?.attributes.strapi_stage + ); + const userHasWorkflowStage = !isNil( + contentTypes['plugin::users-permissions.user']?.attributes.strapi_stage + ); + + // If the user content type no longer has a workflow stage, it means + // this is the version where we decided to disable RW from the user content type + if (userHadWorkflowStage && !userHasWorkflowStage) { + const workflowService = getService('workflows', { strapi }); + + // Find if the user content type is assigned to a workflow + const assignedWorkflow = await workflowService.getAssignedWorkflow( + 'plugin::users-permissions.user' + ); + + if (assignedWorkflow) { + await workflowContentTypesManager({ strapi }).unassignContentType( + assignedWorkflow, + 'plugin::users-permissions.user' + ); + } + } +} + +module.exports = migrateWorkflowsFilterUsersContentType; diff --git a/packages/core/admin/ee/server/register.js b/packages/core/admin/ee/server/register.js index e700b2ec34..5cd18fc429 100644 --- a/packages/core/admin/ee/server/register.js +++ b/packages/core/admin/ee/server/register.js @@ -11,6 +11,7 @@ const migrateDeletedCTInWorkflows = require('./migrations/review-workflows-delet const createAuditLogsService = require('./services/audit-logs'); const reviewWorkflowsMiddlewares = require('./middlewares/review-workflows'); const { getService } = require('./utils'); +const migrateWorkflowsFilterUsersContentType = require('./migrations/review-workflows-filter-user-content-type'); module.exports = async ({ strapi }) => { const auditLogsIsEnabled = strapi.config.get('admin.auditLogs.enabled', true); @@ -28,7 +29,8 @@ module.exports = async ({ strapi }) => { .register(migrateReviewWorkflowStagesColor) .register(migrateReviewWorkflowName) .register(migrateWorkflowsContentTypes) - .register(migrateDeletedCTInWorkflows); + .register(migrateDeletedCTInWorkflows) + .register(migrateWorkflowsFilterUsersContentType); const reviewWorkflowService = getService('review-workflows'); reviewWorkflowsMiddlewares.contentTypeMiddleware(strapi); diff --git a/packages/core/admin/ee/server/services/review-workflows/workflows/content-types.js b/packages/core/admin/ee/server/services/review-workflows/workflows/content-types.js index 230877647a..6fb1457455 100644 --- a/packages/core/admin/ee/server/services/review-workflows/workflows/content-types.js +++ b/packages/core/admin/ee/server/services/review-workflows/workflows/content-types.js @@ -45,7 +45,7 @@ module.exports = ({ strapi }) => { await stagesService.updateEntitiesStage(uid, { toStageId: stageId }); // Transfer content types from the previous workflow(s) await mapAsync(srcWorkflows, (srcWorkflow) => - this.transferContentTypes(srcWorkflow, uid) + this.removeContentTypeFromWorkflow(srcWorkflow, uid) ); } await updateContentTypeConfig(uid, true); @@ -56,7 +56,7 @@ module.exports = ({ strapi }) => { toStageId: stageId, }); }, - // transferContentTypes can cause race conditions if called in parallel when updating the same workflow + // removeContentTypeFromWorkflow can cause race conditions if called in parallel when updating the same workflow { concurrency: 1 } ); @@ -71,7 +71,7 @@ module.exports = ({ strapi }) => { * @param {Workflow} srcWorkflow - The workflow to transfer from * @param {string} uid - The content type uid */ - async transferContentTypes(srcWorkflow, uid) { + async removeContentTypeFromWorkflow(srcWorkflow, uid) { // Update assignedContentTypes of the previous workflow await strapi.entityService.update(WORKFLOW_MODEL_UID, srcWorkflow.id, { data: { @@ -79,6 +79,16 @@ module.exports = ({ strapi }) => { }, }); }, + + /** + * Removes all configurations related to a content type assigned on a workflow + * @param {*} srcWorkflow + * @param {*} uid + */ + async unassignContentType(srcWorkflow, uid) { + await this.removeContentTypeFromWorkflow(srcWorkflow, uid); + await updateContentTypeConfig(uid, false); + }, }; }; diff --git a/packages/core/admin/ee/server/utils/review-workflows.js b/packages/core/admin/ee/server/utils/review-workflows.js index 505ae4f664..6cc5433fd6 100644 --- a/packages/core/admin/ee/server/utils/review-workflows.js +++ b/packages/core/admin/ee/server/utils/review-workflows.js @@ -1,12 +1,14 @@ 'use strict'; -const { getOr, keys, pickBy, pipe, has, clamp } = require('lodash/fp'); +const { getOr, keys, pickBy, pipe, has, clamp, filter } = require('lodash/fp'); const { ENTITY_STAGE_ATTRIBUTE, MAX_WORKFLOWS, MAX_STAGES_PER_WORKFLOW, } = require('../constants/workflows'); +const RW_EXCLUDED_CONTENT_TYPES = ['plugin::users-permissions.user']; + const getVisibleContentTypesUID = pipe([ // Pick only content-types visible in the content-manager and option is not false pickBy( @@ -16,6 +18,7 @@ const getVisibleContentTypesUID = pipe([ ), // Get UIDs keys, + filter((uid) => !RW_EXCLUDED_CONTENT_TYPES.includes(uid)), ]); const hasStageAttribute = has(['attributes', ENTITY_STAGE_ATTRIBUTE]);