2017-11-06 11:14:43 +01:00
|
|
|
'use strict';
|
|
|
|
|
2017-11-17 14:22:59 +01:00
|
|
|
const fs = require('fs')
|
|
|
|
const path = require('path');
|
|
|
|
const stringify = JSON.stringify;
|
2017-11-16 17:59:41 +01:00
|
|
|
const _ = require('lodash');
|
2017-11-06 11:14:43 +01:00
|
|
|
/**
|
|
|
|
* UsersPermissions.js service
|
|
|
|
*
|
|
|
|
* @description: A set of functions similar to controller's actions to avoid code duplication.
|
|
|
|
*/
|
|
|
|
|
|
|
|
module.exports = {
|
2017-11-16 17:59:41 +01:00
|
|
|
getActions: () => {
|
2017-11-17 14:22:59 +01:00
|
|
|
// Plugin user-permissions path
|
|
|
|
const roleConfigPath = path.join(
|
|
|
|
strapi.config.appPath,
|
|
|
|
'plugins',
|
|
|
|
'users-permissions',
|
|
|
|
'config',
|
|
|
|
'roles.json',
|
|
|
|
);
|
2017-11-17 12:14:12 +01:00
|
|
|
|
2017-11-17 14:22:59 +01:00
|
|
|
const generateActions = (data) => (
|
|
|
|
Object.keys(data).reduce((acc, key) => {
|
|
|
|
acc[key] = { enabled: false, policy: '' };
|
2017-11-06 11:14:43 +01:00
|
|
|
|
2017-11-17 14:22:59 +01:00
|
|
|
return acc;
|
|
|
|
}, {}));
|
|
|
|
|
|
|
|
const appControllers = Object.keys(strapi.api).reduce((acc, key) => {
|
|
|
|
acc.controllers[key] = generateActions(strapi.api[key].controllers[key]);
|
2017-11-16 17:59:41 +01:00
|
|
|
|
|
|
|
return acc;
|
2017-11-17 12:14:12 +01:00
|
|
|
}, { controllers: {} });
|
|
|
|
|
|
|
|
const pluginsPermissions = Object.keys(strapi.plugins).reduce((acc, key) => {
|
|
|
|
const pluginControllers = Object.keys(strapi.plugins[key].controllers).reduce((obj, k) => {
|
|
|
|
obj.icon = strapi.plugins[key].package.strapi.icon;
|
2017-11-17 14:22:59 +01:00
|
|
|
obj.controllers[k] = generateActions(strapi.plugins[key].controllers[k]);
|
2017-11-17 12:14:12 +01:00
|
|
|
|
|
|
|
return obj;
|
|
|
|
|
|
|
|
}, { icon: '', controllers: {} });
|
|
|
|
|
|
|
|
acc[key] = pluginControllers;
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
}, {});
|
2017-11-16 17:59:41 +01:00
|
|
|
|
|
|
|
const permissions = {
|
|
|
|
application: {
|
|
|
|
icon: '',
|
|
|
|
controllers: appControllers.controllers,
|
2017-11-17 12:14:12 +01:00
|
|
|
},
|
2017-11-16 17:59:41 +01:00
|
|
|
};
|
|
|
|
|
2017-11-17 14:22:59 +01:00
|
|
|
|
2017-11-17 12:14:12 +01:00
|
|
|
const allPermissions = _.merge(permissions, pluginsPermissions);
|
|
|
|
|
2017-11-17 14:22:59 +01:00
|
|
|
try {
|
|
|
|
const permissionsJSON = require(roleConfigPath);
|
|
|
|
|
|
|
|
if (_.isEmpty(_.get(permissionsJSON, ['0', 'permissions']))) {
|
|
|
|
_.set(permissionsJSON, ['0', 'permissions'], allPermissions);
|
|
|
|
fs.writeFileSync(roleConfigPath, stringify(permissionsJSON, null, 2), 'utf8');
|
|
|
|
}
|
|
|
|
} catch(err) {
|
|
|
|
console.log(err);
|
|
|
|
}
|
|
|
|
|
2017-11-17 12:14:12 +01:00
|
|
|
return allPermissions;
|
|
|
|
}
|
2017-11-06 11:14:43 +01:00
|
|
|
};
|