219 lines
6.6 KiB
JavaScript
Raw Normal View History

'use strict';
2022-08-30 15:11:38 +02:00
const { prop, isEmpty } = require('lodash/fp');
2022-08-09 19:35:48 +02:00
const { hasDraftAndPublish } = require('@strapi/utils').contentTypes;
2021-04-29 13:51:12 +02:00
const { PUBLISHED_AT_ATTRIBUTE } = require('@strapi/utils').contentTypes.constants;
const { MANY_RELATIONS } = require('@strapi/utils').relations.constants;
const { getService } = require('../utils');
const { validateFindAvailable, validateFindExisting } = require('./validation/relations');
2022-09-01 17:24:34 +02:00
const addFiltersClause = (params, filtersClause) => {
params.filters = params.filters || {};
if (params.filters.$and) {
params.filters.$and.push(filtersClause);
2022-08-16 19:36:10 +02:00
} else {
2022-09-01 17:24:34 +02:00
params.filters.$and = [filtersClause];
2022-08-16 19:36:10 +02:00
}
};
module.exports = {
async findAvailable(ctx) {
const { userAbility } = ctx.state;
const { model, targetField } = ctx.params;
await validateFindAvailable(ctx.request.query);
2022-08-05 16:05:52 +02:00
2022-10-03 18:11:33 +02:00
// idsToOmit: used to exclude relations that the front already added but that were not saved yet
// idsToInclude: used to include relations that the front removed but not saved yes
2022-09-30 15:38:49 +02:00
const { component, entityId, idsToOmit, idsToInclude, _q, ...query } = ctx.request.query;
2022-08-09 19:35:48 +02:00
const sourceModelUid = component || model;
2022-08-09 19:35:48 +02:00
const sourceModel = strapi.getModel(sourceModelUid);
if (!sourceModel) {
2022-08-05 16:05:52 +02:00
return ctx.badRequest("The model doesn't exist");
}
2022-09-01 17:24:34 +02:00
const permissionChecker = getService('permission-checker').create({
userAbility,
model,
});
if (permissionChecker.cannot.read()) {
2022-09-01 17:24:34 +02:00
return ctx.forbidden();
}
const attribute = sourceModel.attributes[targetField];
if (!attribute || attribute.type !== 'relation') {
return ctx.badRequest("This relational field doesn't exist");
}
2022-09-01 17:24:34 +02:00
// TODO: find a way to check field permission for component
if (!component && permissionChecker.cannot.read(null, targetField)) {
2022-09-01 17:24:34 +02:00
return ctx.forbidden();
}
if (entityId) {
const entityManager = getService('entity-manager');
const entity = await entityManager.findOneWithCreatorRoles(entityId, model);
if (!entity) {
return ctx.notFound();
}
2022-09-01 17:24:34 +02:00
if (!component && permissionChecker.cannot.read(entity, targetField)) {
return ctx.forbidden();
}
// TODO: find a way to check field permission for component
if (component && permissionChecker.cannot.read(entity)) {
return ctx.forbidden();
}
}
2022-08-05 16:05:52 +02:00
const targetedModel = strapi.getModel(attribute.target);
2022-08-05 16:05:52 +02:00
const modelConfig = component
2022-08-09 19:35:48 +02:00
? await getService('components').findConfiguration(sourceModel)
: await getService('content-types').findConfiguration(sourceModel);
2022-08-05 16:05:52 +02:00
const mainField = prop(`metadatas.${targetField}.edit.mainField`, modelConfig) || 'id';
2022-08-16 18:48:50 +02:00
const fieldsToSelect = ['id', mainField];
if (hasDraftAndPublish(targetedModel)) {
fieldsToSelect.push(PUBLISHED_AT_ATTRIBUTE);
}
2022-08-30 15:11:38 +02:00
const queryParams = {
2022-09-01 17:24:34 +02:00
sort: mainField,
...query,
fields: fieldsToSelect, // cannot select other fields as the user may not have the permissions
filters: {}, // cannot filter for RBAC reasons
2022-08-30 15:11:38 +02:00
};
2022-08-10 19:14:04 +02:00
2022-08-05 16:05:52 +02:00
if (!isEmpty(idsToOmit)) {
2022-09-01 17:24:34 +02:00
addFiltersClause(queryParams, { id: { $notIn: idsToOmit } });
}
2022-08-17 11:58:18 +02:00
// searching should be allowed only on mainField for permission reasons
if (_q) {
2022-09-01 17:24:34 +02:00
addFiltersClause(queryParams, { [mainField]: { $containsi: _q } });
2022-08-17 11:58:18 +02:00
}
2022-08-05 16:05:52 +02:00
if (entityId) {
2022-08-16 17:01:52 +02:00
const subQuery = strapi.db.queryBuilder(sourceModel.uid);
const alias = subQuery.getAlias();
2022-09-30 15:38:49 +02:00
const where = {
id: entityId,
[`${alias}.id`]: { $notNull: true },
};
if (!isEmpty(idsToInclude)) {
where[`${alias}.id`].$notIn = idsToInclude;
}
2022-08-16 17:01:52 +02:00
const knexSubQuery = subQuery
2022-09-30 15:38:49 +02:00
.where(where)
2022-08-16 17:01:52 +02:00
.join({ alias, targetField })
.select(`${alias}.id`)
2022-08-05 16:05:52 +02:00
.getKnexQuery();
2022-09-01 17:24:34 +02:00
addFiltersClause(queryParams, { id: { $notIn: knexSubQuery } });
2022-08-05 16:05:52 +02:00
}
2022-09-01 17:24:34 +02:00
ctx.body = await strapi.entityService.findPage(targetedModel.uid, queryParams);
},
async findExisting(ctx) {
const { userAbility } = ctx.state;
const { model, id, targetField } = ctx.params;
await validateFindExisting(ctx.request.query);
2022-10-03 18:11:33 +02:00
const { component, ...query } = ctx.request.query;
const sourceModelUid = component || model;
const sourceModel = strapi.getModel(sourceModelUid);
if (!sourceModel) {
return ctx.badRequest("The model doesn't exist");
}
const entityManager = getService('entity-manager');
const permissionChecker = getService('permission-checker').create({
userAbility,
model,
});
if (permissionChecker.cannot.read()) {
2022-09-01 17:24:34 +02:00
return ctx.forbidden();
}
const attribute = sourceModel.attributes[targetField];
if (!attribute || attribute.type !== 'relation') {
return ctx.badRequest("This relational field doesn't exist");
}
2022-09-01 17:24:34 +02:00
// TODO: find a way to check field permission for component
if (!component && permissionChecker.cannot.read(null, targetField)) {
return ctx.forbidden();
}
const entity = await entityManager.findOneWithCreatorRoles(id, model);
if (!entity) {
return ctx.notFound();
}
2022-09-01 17:24:34 +02:00
if (!component && permissionChecker.cannot.read(entity, targetField)) {
return ctx.forbidden();
}
// TODO: find a way to check field permission for component
if (component && permissionChecker.cannot.read(entity)) {
return ctx.forbidden();
}
const targetedModel = strapi.getModel(attribute.target);
const modelConfig = component
? await getService('components').findConfiguration(sourceModel)
: await getService('content-types').findConfiguration(sourceModel);
const mainField = prop(`metadatas.${targetField}.edit.mainField`, modelConfig) || 'id';
const fieldsToSelect = ['id', mainField];
if (hasDraftAndPublish(targetedModel)) {
fieldsToSelect.push(PUBLISHED_AT_ATTRIBUTE);
}
const queryParams = {
2022-10-03 17:52:12 +02:00
fields: fieldsToSelect,
};
if (MANY_RELATIONS.includes(attribute.relation)) {
2022-10-03 17:52:12 +02:00
const res = await strapi.entityService.loadPages(sourceModelUid, { id }, targetField, {
...queryParams,
page: query.page,
pageSize: query.pageSize,
ordering: 'desc',
});
ctx.body = res;
} else {
2022-10-03 17:52:12 +02:00
const result = await strapi.entityService.load(
sourceModelUid,
{ id },
targetField,
queryParams
);
// const result = await strapi.db.query(sourceModelUid).load({ id }, targetField, queryParams);
// TODO: Temporary fix (use data instead)
ctx.body = {
results: result ? [result] : [],
pagination: { page: 1, pageSize: 5, pageCount: 1, total: 1 },
};
}
},
};