Skip to content

Commit

Permalink
Renaming custom actions to actions
Browse files Browse the repository at this point in the history
  • Loading branch information
marcdelalonde committed May 2, 2021
1 parent 284a7ea commit 1da66bb
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 27 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ We only manage **MySQL** & **MongoDB** databases for the moment. However, we are

Adminmate comes with all the features you need for your administration panel:
* **CRUD**: Create-Read-Update-Delete actions
* **Custom Actions**: Add your business custom logic
* **Actions**: Add your business custom logic
* **Segments**: Create unlimited data segments
* **Dashboard**: Create charts from your data, directly from the Adminmate Dashboard or via our extensible API
* **Team-based access**: Control who can create-read-update-delete the data and access to your dashboards & graphs
Expand Down
8 changes: 4 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const perm = require('./src/middlewares/perm');
const authController = require('./src/controllers/auth');
const installController = require('./src/controllers/install');
const configCtrl = require('./src/controllers/config');
const customActionsCtrl = require('./src/controllers/customactions');
const actionsCtrl = require('./src/controllers/actions');
const chartsCtrl = require('./src/controllers/charts');

// Helpers
Expand Down Expand Up @@ -50,9 +50,9 @@ const Adminmate = ({ projectId, secretKey, authKey, masterPassword, models, char
// Config
router.get(`${endpointPrefix}/config`, isAuthorizedIP, isAuthorized, configCtrl.getConfig(api));

// Custom Actions
router.get(`${endpointPrefix}/models/:model/customactions`, isAuthorizedIP, isAuthorized, customActionsCtrl.getMatching(api));
router.post(`${endpointPrefix}/models/:model/customactions/:ca`, isAuthorizedIP, isAuthorized, perm.canExecuteCA, customActionsCtrl.execute);
// Actions
router.get(`${endpointPrefix}/models/:model/actions`, isAuthorizedIP, isAuthorized, actionsCtrl.getMatching(api));
router.post(`${endpointPrefix}/models/:model/actions/:ca`, isAuthorizedIP, isAuthorized, perm.canExecuteCA, actionsCtrl.execute);

// CRUD endpoints
router.post(`${endpointPrefix}/models/:model`, isAuthorizedIP, isAuthorized, perm.canAccessModel, api.modelGetAll);
Expand Down
24 changes: 12 additions & 12 deletions src/controllers/customactions.js → src/controllers/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ module.exports.getMatching = api => {
});
}

const currentModelCustomActions = fnHelper.getModelCustomActions(modelName, req.modelPermData.can_use_custom_actions);
if (!currentModelCustomActions || currentModelCustomActions.length === 0) {
const currentModelActions = fnHelper.getModelActions(modelName, req.modelPermData.can_use_actions);
if (!currentModelActions || currentModelActions.length === 0) {
return res.json({ list: actionsList });
}

Expand All @@ -44,7 +44,7 @@ module.exports.getMatching = api => {
}

// Filter by target
const customActionsFilteredByTarget = currentModelCustomActions
const actionsFilteredByTarget = currentModelActions
.filter(sa => {
const isStringAndValid = typeof sa.target === 'string' && sa.target === target;
const isArrayAndValid = Array.isArray(sa.target) && sa.target.includes(target);
Expand All @@ -56,25 +56,25 @@ module.exports.getMatching = api => {
});

itemsDB.forEach(item => {
customActionsFilteredByTarget.forEach(sa => {
// If the filter do not pass, remove the custom actions from the list
actionsFilteredByTarget.forEach(sa => {
// If the filter do not pass, remove the actions from the list
if (typeof sa.filter === 'function' && sa.filter(item) === false) {
sa.passFilter = false;
}
});
});

// We only keep valid custom actions
const finalCustomActions = customActionsFilteredByTarget.filter(sa => sa.passFilter === true);
// We only keep valid actions
const finalActions = actionsFilteredByTarget.filter(sa => sa.passFilter === true);

// If there is both the native delete action and other custom actions, add a separator
if (actionsList.length && finalCustomActions.length) {
// If there is both the native delete action and other actions, add a separator
if (actionsList.length && finalActions.length) {
actionsList.unshift({ type: 'separator' });
}

res.json({
list: [
...finalCustomActions,
...finalActions,
...actionsList
]
});
Expand All @@ -86,11 +86,11 @@ module.exports.execute = (req, res) => {
const caCode = req.params.ca;

const matchingModel = global._amConfig.models.find(m => m.slug === modelSlug);
if (!matchingModel || !matchingModel.customActions || !Array.isArray(matchingModel.customActions)) {
if (!matchingModel || !matchingModel.actions || !Array.isArray(matchingModel.actions)) {
return res.status(403).json({ message: 'Invalid model' });
}

const matchingAction = matchingModel.customActions.find(ca => ca.code === caCode);
const matchingAction = matchingModel.actions.find(action => action.code === caCode);
if (!matchingAction || !matchingAction.handler || typeof matchingAction.handler !== 'function') {
return res.status(403).json({ message: 'Invalid model' });
}
Expand Down
8 changes: 4 additions & 4 deletions src/controllers/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ module.exports.getConfig = api => {
slug: modelConfig.slug,
realname: api.getModelRealname(modelConfig.model),
properties: api.getModelProperties(modelConfig.model),
customactions: [],
actions: [],
segments: [],
options: modelConfig.options
};

// Add custom actions if present
if (modelConfig.customActions) {
modelObject.customactions = modelConfig.customActions.map(ca => ({ label: ca.label, code: ca.code }));
// Add actions if present
if (modelConfig.actions) {
modelObject.actions = modelConfig.actions.map(action => ({ label: action.label, code: action.code }));
}

// Add segments if present
Expand Down
8 changes: 4 additions & 4 deletions src/helpers/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module.exports.initModels = models => {
slug: m.slug,
model: m.model,
segments: m.segments || [],
customActions: m.customActions || [],
actions: m.actions || [],
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,
Expand Down Expand Up @@ -44,17 +44,17 @@ module.exports.getModelOptions = modelCode => {
return currentModel.options;
};

module.exports.getModelCustomActions = (modelCode, onlyIn) => {
module.exports.getModelActions = (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.actions.filter(action => onlyIn.includes(action.code));
}

return currentModel.customActions;
return currentModel.actions;
};


Expand Down
4 changes: 2 additions & 2 deletions src/middlewares/perm.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ module.exports.canExecuteCA = (req, res, next) => {
const caCode = req.params.ca;

const authorizedCA = caCode && (
req.modelPermData.can_use_custom_actions.includes('*') ||
req.modelPermData.can_use_custom_actions.includes(caCode)
req.modelPermData.can_use_actions.includes('*') ||
req.modelPermData.can_use_actions.includes(caCode)
);

if (authorizedCA) {
Expand Down

0 comments on commit 1da66bb

Please sign in to comment.