Checks for dependencies during deletion.
This commit is contained in:
parent
0a08088893
commit
efbfa2b366
20 changed files with 246 additions and 121 deletions
58
lib/dependency-helpers.js
Normal file
58
lib/dependency-helpers.js
Normal file
|
@ -0,0 +1,58 @@
|
|||
'use strict';
|
||||
|
||||
const knex = require('./knex');
|
||||
const interoperableErrors = require('../shared/interoperable-errors');
|
||||
const entitySettings = require('./entity-settings');
|
||||
const shares = require('../models/shares');
|
||||
const { enforce } = require('./helpers');
|
||||
|
||||
const defaultNoOfDependenciesReported = 20;
|
||||
|
||||
async function ensureNoDependencies(tx, context, id, depSpecs) {
|
||||
|
||||
const deps = [];
|
||||
let andMore = false;
|
||||
|
||||
for (const depSpec of depSpecs) {
|
||||
const entityType = entitySettings.getEntityType(depSpec.entityTypeId);
|
||||
|
||||
let rows;
|
||||
|
||||
if (depSpec.query) {
|
||||
rows = await depSpec.query(tx).limit(defaultNoOfDependenciesReported + 1);
|
||||
} else if (depSpec.column) {
|
||||
rows = await tx(entityType.entitiesTable).where(depSpec.column, id).select(['id', 'name']).limit(defaultNoOfDependenciesReported + 1);
|
||||
} else if (depSpec.rows) {
|
||||
rows = await depSpec.rows(tx, defaultNoOfDependenciesReported + 1)
|
||||
}
|
||||
|
||||
for (const row of rows) {
|
||||
if (deps.length === defaultNoOfDependenciesReported) {
|
||||
andMore = true;
|
||||
break;
|
||||
}
|
||||
|
||||
if (await shares.checkEntityPermissionTx(tx, context, depSpec.entityTypeId, row.id, 'view')) {
|
||||
deps.push({
|
||||
entityTypeId: depSpec.entityTypeId,
|
||||
name: row.name,
|
||||
link: entityType.clientLink(row.id)
|
||||
});
|
||||
} else {
|
||||
deps.push({
|
||||
entityTypeId: depSpec.entityTypeId,
|
||||
id: row.id
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (deps.length > 0) {
|
||||
throw new interoperableErrors.DependencyPresentError('', {
|
||||
dependencies: deps,
|
||||
andMore
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
module.exports.ensureNoDependencies = ensureNoDependencies
|
|
@ -10,17 +10,20 @@ const entityTypes = {
|
|||
namespace: {
|
||||
entitiesTable: 'namespaces',
|
||||
sharesTable: 'shares_namespace',
|
||||
permissionsTable: 'permissions_namespace'
|
||||
permissionsTable: 'permissions_namespace',
|
||||
clientLink: id => `/namespaces/${id}`
|
||||
},
|
||||
list: {
|
||||
entitiesTable: 'lists',
|
||||
sharesTable: 'shares_list',
|
||||
permissionsTable: 'permissions_list'
|
||||
permissionsTable: 'permissions_list',
|
||||
clientLink: id => `/lists/${id}`
|
||||
},
|
||||
customForm: {
|
||||
entitiesTable: 'custom_forms',
|
||||
sharesTable: 'shares_custom_form',
|
||||
permissionsTable: 'permissions_custom_form'
|
||||
permissionsTable: 'permissions_custom_form',
|
||||
clientLink: id => `/lists/forms/${id}`
|
||||
},
|
||||
campaign: {
|
||||
entitiesTable: 'campaigns',
|
||||
|
@ -43,7 +46,8 @@ const entityTypes = {
|
|||
},
|
||||
defaultReplacementBehavior: ReplacementBehavior.NONE
|
||||
}
|
||||
}
|
||||
},
|
||||
clientLink: id => `/campaigns/${id}`
|
||||
},
|
||||
template: {
|
||||
entitiesTable: 'templates',
|
||||
|
@ -58,22 +62,26 @@ const entityTypes = {
|
|||
},
|
||||
defaultReplacementBehavior: ReplacementBehavior.REPLACE
|
||||
}
|
||||
}
|
||||
},
|
||||
clientLink: id => `/templates/${id}`
|
||||
},
|
||||
sendConfiguration: {
|
||||
entitiesTable: 'send_configurations',
|
||||
sharesTable: 'shares_send_configuration',
|
||||
permissionsTable: 'permissions_send_configuration'
|
||||
permissionsTable: 'permissions_send_configuration',
|
||||
clientLink: id => `/send-configurations/${id}`
|
||||
},
|
||||
report: {
|
||||
entitiesTable: 'reports',
|
||||
sharesTable: 'shares_report',
|
||||
permissionsTable: 'permissions_report'
|
||||
permissionsTable: 'permissions_report',
|
||||
clientLink: id => `/reports/${id}`
|
||||
},
|
||||
reportTemplate: {
|
||||
entitiesTable: 'report_templates',
|
||||
sharesTable: 'shares_report_template',
|
||||
permissionsTable: 'permissions_report_template'
|
||||
permissionsTable: 'permissions_report_template',
|
||||
clientLink: id => `/reports/templates/${id}`
|
||||
},
|
||||
mosaicoTemplate: {
|
||||
entitiesTable: 'mosaico_templates',
|
||||
|
@ -96,14 +104,31 @@ const entityTypes = {
|
|||
},
|
||||
defaultReplacementBehavior: ReplacementBehavior.REPLACE
|
||||
}
|
||||
}
|
||||
},
|
||||
clientLink: id => `/templates/mosaico/${id}`
|
||||
},
|
||||
user: {
|
||||
entitiesTable: 'users',
|
||||
clientLink: id => `/users/${id}`
|
||||
}
|
||||
};
|
||||
|
||||
const entityTypesWithPermissions = {};
|
||||
for (const key in entityTypes) {
|
||||
if (entityTypes[key].permissionsTable) {
|
||||
entityTypesWithPermissions[key] = entityTypes[key];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function getEntityTypes() {
|
||||
return entityTypes;
|
||||
}
|
||||
|
||||
function getEntityTypesWithPermissions() {
|
||||
return entityTypesWithPermissions;
|
||||
}
|
||||
|
||||
function getEntityType(entityTypeId) {
|
||||
const entityType = entityTypes[entityTypeId];
|
||||
|
||||
|
@ -116,6 +141,7 @@ function getEntityType(entityTypeId) {
|
|||
|
||||
module.exports = {
|
||||
getEntityTypes,
|
||||
getEntityTypesWithPermissions,
|
||||
getEntityType,
|
||||
ReplacementBehavior
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue