diff --git a/index.js b/index.js index 4552f86..fe31541 100644 --- a/index.js +++ b/index.js @@ -11,6 +11,9 @@ const configCtrl = require('./src/controllers/config'); const customActionsCtrl = require('./src/controllers/customactions'); const chartsCtrl = require('./src/controllers/charts'); +// Helpers +const fnHelper = require('./src/helpers/functions'); + const accessControl = (req, res, next) => { const origin = global._amConfig.devMode ? 'http://localhost:3002' : 'https://my.adminmate.io'; res.header('Access-Control-Allow-Origin', origin); @@ -28,7 +31,7 @@ const Adminmate = ({ projectId, secretKey, authKey, masterPassword, models, char global._amConfig.secretKey = secretKey; global._amConfig.authKey = authKey; global._amConfig.masterPassword = masterPassword; - global._amConfig.models = models || []; + global._amConfig.models = fnHelper.initModels(models || []); global._amConfig.charts = charts || []; global._amConfig.authorizedIps = authorizedIps || null; global._amConfig.devMode = !!global.AM_DEV_MODE; diff --git a/src/controllers/config.js b/src/controllers/config.js index 2f4f437..5824421 100644 --- a/src/controllers/config.js +++ b/src/controllers/config.js @@ -8,12 +8,13 @@ module.exports.getConfig = api => { realname: api.getModelRealname(modelConfig.model), properties: api.getModelProperties(modelConfig.model), customactions: [], - segments: [] + segments: [], + options: modelConfig.options }; // Add custom actions if present if (modelConfig.customActions) { - modelObject.customactions = modelConfig.customActions; + modelObject.customactions = modelConfig.customActions.map(ca => ({ label: ca.label, code: ca.code })); } // Add segments if present diff --git a/src/controllers/customactions.js b/src/controllers/customactions.js index 575b93c..f0ce2d1 100644 --- a/src/controllers/customactions.js +++ b/src/controllers/customactions.js @@ -17,17 +17,18 @@ module.exports.getMatching = api => { // Get model options const currentModelOptions = fnHelper.getModelOptions(modelName); - const cannotDelete = currentModelOptions && currentModelOptions.canDelete === false; + const authorizedToDeleteRegardingOptions = currentModelOptions && currentModelOptions.canDelete === true; + const authorizedToDelete = req.modelPermData && req.modelPermData.can_delete === true; const actionsList = []; - if (!cannotDelete) { + if (authorizedToDeleteRegardingOptions && authorizedToDelete) { actionsList.push({ - label: target === 'item' ? 'Delete item' : 'Delete items', + label: target === 'item' ? 'Delete' : 'Delete selected', code: 'delete' }); } - const currentModelCustomActions = fnHelper.getModelCustomActions(modelName); + const currentModelCustomActions = fnHelper.getModelCustomActions(modelName, req.modelPermData.can_use_custom_actions); if (!currentModelCustomActions || currentModelCustomActions.length === 0) { return res.json({ list: actionsList }); } diff --git a/src/helpers/functions.js b/src/helpers/functions.js index 79acf2b..567dba2 100644 --- a/src/helpers/functions.js +++ b/src/helpers/functions.js @@ -9,13 +9,21 @@ const getModel = modelCode => { return currentModel; }; -module.exports.getModelCustomActions = modelCode => { - const currentModel = getModel(modelCode); - if (!currentModel) { - return null; - } - - return currentModel.customActions; +module.exports.initModels = models => { + return models.map(m => { + const defaultModelConfig = { + slug: m.slug, + model: m.model, + segments: m.segments || [], + customActions: m.customActions || [], + options: { + canCreate: m.options && typeof m.options.canCreate === 'boolean' ? m.options.canCreate : true, + canUpdate: m.options && typeof m.options.canUpdate === 'boolean' ? m.options.canUpdate : true, + canDelete: m.options && typeof m.options.canDelete === 'boolean' ? m.options.canDelete : true + } + }; + return defaultModelConfig; + }); }; module.exports.getModelObject = modelCode => { @@ -36,12 +44,16 @@ module.exports.getModelOptions = modelCode => { return currentModel.options; }; -module.exports.getModelCustomActions = modelCode => { +module.exports.getModelCustomActions = (modelCode, onlyIn) => { const currentModel = getModel(modelCode); if (!currentModel) { return null; } + if (onlyIn && Array.isArray(onlyIn) && !onlyIn.includes('*')) { + return currentModel.customActions.filter(ca => onlyIn.includes(ca.code)); + } + return currentModel.customActions; };