2019-07-22 12:15:54 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const _ = require('lodash');
|
2019-07-22 17:01:25 +02:00
|
|
|
const storeUtils = require('../../services/utils/store');
|
2019-07-23 10:54:44 +02:00
|
|
|
const {
|
|
|
|
createDefaultConfiguration,
|
|
|
|
syncConfiguration,
|
|
|
|
} = require('../../services/utils/configuration');
|
|
|
|
|
2019-11-14 12:21:21 +01:00
|
|
|
const contentTypeService = require('../../services/ContentTypes');
|
|
|
|
const componentService = require('../../services/Components');
|
|
|
|
|
2020-06-09 17:45:53 +02:00
|
|
|
const updateContentTypes = async configurations => {
|
2019-07-23 10:54:44 +02:00
|
|
|
const updateConfiguration = async uid => {
|
|
|
|
const conf = configurations.find(conf => conf.uid === uid);
|
2019-11-14 12:21:21 +01:00
|
|
|
|
|
|
|
return contentTypeService.setConfiguration(
|
|
|
|
uid,
|
|
|
|
await syncConfiguration(conf, strapi.contentTypes[uid])
|
2019-07-23 10:54:44 +02:00
|
|
|
);
|
2019-07-22 12:15:54 +02:00
|
|
|
};
|
2019-07-22 14:36:46 +02:00
|
|
|
|
2019-07-23 10:54:44 +02:00
|
|
|
const generateNewConfiguration = async uid => {
|
2019-11-14 12:21:21 +01:00
|
|
|
return contentTypeService.setConfiguration(
|
|
|
|
uid,
|
|
|
|
await createDefaultConfiguration(strapi.contentTypes[uid])
|
2019-07-23 10:54:44 +02:00
|
|
|
);
|
|
|
|
};
|
2019-07-22 14:36:46 +02:00
|
|
|
|
2019-11-14 12:21:21 +01:00
|
|
|
const currentUIDS = Object.keys(strapi.contentTypes);
|
2019-07-23 10:54:44 +02:00
|
|
|
const DBUIDs = configurations.map(({ uid }) => uid);
|
2019-11-14 12:21:21 +01:00
|
|
|
|
|
|
|
const contentTypesToUpdate = _.intersection(currentUIDS, DBUIDs);
|
|
|
|
const contentTypesToAdd = _.difference(currentUIDS, DBUIDs);
|
|
|
|
const contentTypesToDelete = _.difference(DBUIDs, currentUIDS);
|
2019-07-22 14:36:46 +02:00
|
|
|
|
2019-07-23 10:54:44 +02:00
|
|
|
// delette old schemas
|
2020-06-04 18:30:26 +02:00
|
|
|
await Promise.all(contentTypesToDelete.map(uid => contentTypeService.deleteConfiguration(uid)));
|
2019-07-22 14:36:46 +02:00
|
|
|
|
2019-07-23 10:54:44 +02:00
|
|
|
// create new schemas
|
2020-06-04 18:30:26 +02:00
|
|
|
await Promise.all(contentTypesToAdd.map(uid => generateNewConfiguration(uid)));
|
2019-07-22 16:24:44 +02:00
|
|
|
|
2019-07-23 10:54:44 +02:00
|
|
|
// update current schemas
|
|
|
|
await Promise.all(contentTypesToUpdate.map(uid => updateConfiguration(uid)));
|
2020-06-09 17:45:53 +02:00
|
|
|
};
|
2019-07-22 16:24:44 +02:00
|
|
|
|
2020-06-09 17:45:53 +02:00
|
|
|
const syncContentTypesSchemas = async () => {
|
|
|
|
const configurations = await storeUtils.findByKey(
|
|
|
|
'plugin_content_manager_configuration_content_types'
|
|
|
|
);
|
|
|
|
|
|
|
|
await updateContentTypes(configurations);
|
|
|
|
};
|
|
|
|
|
|
|
|
const syncComponentsSchemas = async () => {
|
2019-07-23 10:54:44 +02:00
|
|
|
const updateConfiguration = async uid => {
|
|
|
|
const conf = configurations.find(conf => conf.uid === uid);
|
2019-11-14 12:21:21 +01:00
|
|
|
|
|
|
|
return componentService.setConfiguration(
|
|
|
|
uid,
|
|
|
|
await syncConfiguration(conf, strapi.components[uid])
|
|
|
|
);
|
2019-07-23 10:54:44 +02:00
|
|
|
};
|
2019-07-22 16:24:44 +02:00
|
|
|
|
2019-07-23 10:54:44 +02:00
|
|
|
const generateNewConfiguration = async uid => {
|
2019-11-14 12:21:21 +01:00
|
|
|
return componentService.setConfiguration(
|
2019-07-23 10:54:44 +02:00
|
|
|
uid,
|
2019-10-22 18:01:03 +02:00
|
|
|
await createDefaultConfiguration(strapi.components[uid])
|
2019-07-22 16:24:44 +02:00
|
|
|
);
|
2019-07-23 10:54:44 +02:00
|
|
|
};
|
2019-07-22 16:24:44 +02:00
|
|
|
|
2019-07-23 10:54:44 +02:00
|
|
|
const configurations = await storeUtils.findByKey(
|
2019-10-22 18:01:03 +02:00
|
|
|
'plugin_content_manager_configuration_components'
|
2019-07-22 16:24:44 +02:00
|
|
|
);
|
|
|
|
|
2019-10-22 18:01:03 +02:00
|
|
|
const realUIDs = Object.keys(strapi.components);
|
2019-07-23 10:54:44 +02:00
|
|
|
const DBUIDs = configurations.map(({ uid }) => uid);
|
2019-10-22 18:01:03 +02:00
|
|
|
const componentsToUpdate = _.intersection(realUIDs, DBUIDs);
|
|
|
|
const componentsToAdd = _.difference(realUIDs, DBUIDs);
|
|
|
|
const componentsToDelete = _.difference(DBUIDs, realUIDs);
|
2019-07-22 12:15:54 +02:00
|
|
|
|
2019-07-23 10:54:44 +02:00
|
|
|
// delette old schemas
|
2020-06-04 18:30:26 +02:00
|
|
|
await Promise.all(componentsToDelete.map(uid => componentService.deleteConfiguration(uid)));
|
2019-07-22 14:36:46 +02:00
|
|
|
|
2019-07-23 10:54:44 +02:00
|
|
|
// create new schemas
|
2019-10-22 18:01:03 +02:00
|
|
|
await Promise.all(componentsToAdd.map(uid => generateNewConfiguration(uid)));
|
2019-07-22 12:15:54 +02:00
|
|
|
|
2019-07-23 10:54:44 +02:00
|
|
|
// update current schemas
|
2019-10-22 18:01:03 +02:00
|
|
|
await Promise.all(componentsToUpdate.map(uid => updateConfiguration(uid)));
|
2020-06-09 17:45:53 +02:00
|
|
|
};
|
2020-06-04 18:30:26 +02:00
|
|
|
|
2020-06-09 17:45:53 +02:00
|
|
|
const registerPermissions = () => {
|
2020-06-08 19:12:25 +02:00
|
|
|
const contentTypesUids = strapi.plugins[
|
|
|
|
'content-manager'
|
|
|
|
].services.contenttypes.getDisplayedContentTypesUids();
|
2020-06-04 18:30:26 +02:00
|
|
|
|
2020-08-11 16:39:05 +02:00
|
|
|
const hasDraftAndPublish = uid => strapi.contentTypes[uid].options.draftAndPublish;
|
|
|
|
|
2020-06-08 11:01:20 +02:00
|
|
|
const actions = [
|
2020-06-04 18:30:26 +02:00
|
|
|
{
|
|
|
|
section: 'contentTypes',
|
|
|
|
displayName: 'Create',
|
2020-06-18 11:53:35 +02:00
|
|
|
uid: 'explorer.create',
|
2020-06-04 18:30:26 +02:00
|
|
|
pluginName: 'content-manager',
|
|
|
|
subjects: contentTypesUids,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
section: 'contentTypes',
|
|
|
|
displayName: 'Read',
|
2020-06-18 11:53:35 +02:00
|
|
|
uid: 'explorer.read',
|
2020-06-04 18:30:26 +02:00
|
|
|
pluginName: 'content-manager',
|
|
|
|
subjects: contentTypesUids,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
section: 'contentTypes',
|
|
|
|
displayName: 'Update',
|
2020-06-18 11:53:35 +02:00
|
|
|
uid: 'explorer.update',
|
2020-06-04 18:30:26 +02:00
|
|
|
pluginName: 'content-manager',
|
|
|
|
subjects: contentTypesUids,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
section: 'contentTypes',
|
|
|
|
displayName: 'Delete',
|
2020-06-18 11:53:35 +02:00
|
|
|
uid: 'explorer.delete',
|
2020-06-04 18:30:26 +02:00
|
|
|
pluginName: 'content-manager',
|
|
|
|
subjects: contentTypesUids,
|
2020-08-11 16:39:05 +02:00
|
|
|
options: {
|
|
|
|
fieldsGranularity: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
section: 'contentTypes',
|
|
|
|
displayName: 'Publish',
|
|
|
|
uid: 'explorer.publish',
|
|
|
|
pluginName: 'content-manager',
|
|
|
|
subjects: contentTypesUids.filter(hasDraftAndPublish),
|
|
|
|
options: {
|
|
|
|
fieldsGranularity: false,
|
|
|
|
},
|
2020-06-04 18:30:26 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
section: 'plugins',
|
|
|
|
displayName: 'Configure view',
|
2020-06-05 14:25:44 +02:00
|
|
|
uid: 'single-types.configure-view',
|
2020-06-04 18:30:26 +02:00
|
|
|
subCategory: 'single types',
|
|
|
|
pluginName: 'content-manager',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
section: 'plugins',
|
|
|
|
displayName: 'Configure view',
|
2020-06-05 14:25:44 +02:00
|
|
|
uid: 'collection-types.configure-view',
|
2020-06-04 18:30:26 +02:00
|
|
|
subCategory: 'collection types',
|
|
|
|
pluginName: 'content-manager',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
section: 'plugins',
|
|
|
|
displayName: 'Configure Layout',
|
2020-06-05 14:25:44 +02:00
|
|
|
uid: 'components.configure-layout',
|
2020-06-04 18:30:26 +02:00
|
|
|
subCategory: 'components',
|
|
|
|
pluginName: 'content-manager',
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2020-06-09 19:00:57 +02:00
|
|
|
const actionProvider = strapi.admin.services.permission.actionProvider;
|
2020-06-08 11:01:20 +02:00
|
|
|
actionProvider.register(actions);
|
2020-06-09 17:45:53 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = async () => {
|
|
|
|
await syncContentTypesSchemas();
|
|
|
|
await syncComponentsSchemas();
|
|
|
|
registerPermissions();
|
|
|
|
};
|