mirror of
https://github.com/strapi/strapi.git
synced 2025-11-08 22:32:02 +00:00
feat: admin clone controller
This commit is contained in:
parent
75c0450400
commit
06bb305691
@ -1,9 +1,12 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const { setCreatorFields, pipeAsync } = require('@strapi/utils');
|
const { setCreatorFields, pipeAsync } = require('@strapi/utils');
|
||||||
|
const { isEmpty } = require('lodash/fp');
|
||||||
|
const { ApplicationError } = require('@strapi/utils').errors;
|
||||||
|
|
||||||
const { getService, pickWritableAttributes } = require('../utils');
|
const { getService, pickWritableAttributes } = require('../utils');
|
||||||
const { validateBulkActionInput } = require('./validation');
|
const { validateBulkActionInput } = require('./validation');
|
||||||
|
const { hasProhibitedCloningFields } = require('../services/utils/clone');
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
async find(ctx) {
|
async find(ctx) {
|
||||||
@ -150,6 +153,43 @@ module.exports = {
|
|||||||
ctx.body = await permissionChecker.sanitizeOutput(updatedEntity);
|
ctx.body = await permissionChecker.sanitizeOutput(updatedEntity);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
async clone(ctx) {
|
||||||
|
const { userAbility, user } = ctx.state;
|
||||||
|
const { model, sourceId: id } = ctx.params;
|
||||||
|
const { body } = ctx.request;
|
||||||
|
|
||||||
|
const entityManager = getService('entity-manager');
|
||||||
|
const permissionChecker = getService('permission-checker').create({ userAbility, model });
|
||||||
|
|
||||||
|
if (permissionChecker.cannot.create()) {
|
||||||
|
return ctx.forbidden();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Trying to automatically clone the entity
|
||||||
|
// and model has unique or relational fields
|
||||||
|
if (isEmpty(body) && hasProhibitedCloningFields(model)) {
|
||||||
|
throw new ApplicationError('clone.prohibitedFields');
|
||||||
|
}
|
||||||
|
|
||||||
|
const entity = await entityManager.findOneWithCreatorRoles(id, model);
|
||||||
|
|
||||||
|
if (!entity) {
|
||||||
|
return ctx.notFound();
|
||||||
|
}
|
||||||
|
|
||||||
|
const pickWritables = pickWritableAttributes({ model });
|
||||||
|
const pickPermittedFields = permissionChecker.sanitizeCreateInput;
|
||||||
|
const setCreator = setCreatorFields({ user });
|
||||||
|
|
||||||
|
const sanitizeFn = pipeAsync(pickWritables, pickPermittedFields, setCreator);
|
||||||
|
|
||||||
|
const sanitizedBody = await sanitizeFn(body);
|
||||||
|
|
||||||
|
const clonedEntity = await entityManager.clone(entity, sanitizedBody, model);
|
||||||
|
|
||||||
|
ctx.body = await permissionChecker.sanitizeOutput(clonedEntity);
|
||||||
|
},
|
||||||
|
|
||||||
async delete(ctx) {
|
async delete(ctx) {
|
||||||
const { userAbility } = ctx.state;
|
const { userAbility } = ctx.state;
|
||||||
const { id, model } = ctx.params;
|
const { id, model } = ctx.params;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user