2017-10-05 22:01:34 +03:00
|
|
|
// Based heavily on the settings cache
|
2020-04-29 18:44:27 +03:00
|
|
|
const _ = require('lodash');
|
|
|
|
|
|
|
|
let actionsMap = {};
|
2017-10-05 22:01:34 +03:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
getAll: function getAll() {
|
|
|
|
return _.cloneDeep(actionsMap);
|
|
|
|
},
|
|
|
|
init: function init(perms) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const seenActions = {};
|
2017-10-05 22:01:34 +03:00
|
|
|
|
|
|
|
actionsMap = {};
|
|
|
|
|
|
|
|
// Build a hash map of the actions on objects, i.e
|
|
|
|
/*
|
|
|
|
{
|
|
|
|
'edit': ['post', 'tag', 'user', 'page'],
|
|
|
|
'delete': ['post', 'user'],
|
|
|
|
'create': ['post', 'user', 'page']
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
_.each(perms.models, function (perm) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const actionType = perm.get('action_type');
|
|
|
|
const objectType = perm.get('object_type');
|
2017-10-05 22:01:34 +03:00
|
|
|
|
|
|
|
actionsMap[actionType] = actionsMap[actionType] || [];
|
|
|
|
seenActions[actionType] = seenActions[actionType] || {};
|
|
|
|
|
|
|
|
// Check if we've already seen this action -> object combo
|
|
|
|
if (seenActions[actionType][objectType]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
actionsMap[actionType].push(objectType);
|
|
|
|
seenActions[actionType][objectType] = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
return actionsMap;
|
|
|
|
},
|
|
|
|
empty: function empty() {
|
|
|
|
return _.size(actionsMap) === 0;
|
|
|
|
}
|
|
|
|
};
|