fix: remove review workflows from users and permissions

This commit is contained in:
Marc-Roig 2023-08-30 15:00:13 +02:00
parent 41844c2867
commit f0d5be0b37
No known key found for this signature in database
GPG Key ID: FB4E2C43A0BEE249
5 changed files with 60 additions and 5 deletions

View File

@ -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,

View File

@ -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;

View File

@ -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);

View File

@ -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);
},
};
};

View File

@ -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]);