2017-11-06 11:14:43 +01:00
|
|
|
'use strict';
|
|
|
|
|
2017-11-16 17:59:41 +01:00
|
|
|
const _ = require('lodash');
|
2021-09-06 22:33:55 +02:00
|
|
|
const { filter, map, pipe, prop } = require('lodash/fp');
|
2021-09-03 11:11:37 +02:00
|
|
|
|
2021-07-08 18:15:32 +02:00
|
|
|
const { getService } = require('../utils');
|
2017-11-06 11:14:43 +01:00
|
|
|
|
2020-04-20 12:19:44 +02:00
|
|
|
const DEFAULT_PERMISSIONS = [
|
2021-09-06 22:33:55 +02:00
|
|
|
{ action: 'plugin::users-permissions.auth.admincallback', roleType: 'public' },
|
|
|
|
{ action: 'plugin::users-permissions.auth.adminregister', roleType: 'public' },
|
|
|
|
{ action: 'plugin::users-permissions.auth.callback', roleType: 'public' },
|
|
|
|
{ action: 'plugin::users-permissions.auth.connect', roleType: null },
|
|
|
|
{ action: 'plugin::users-permissions.auth.forgotpassword', roleType: 'public' },
|
|
|
|
{ action: 'plugin::users-permissions.auth.resetpassword', roleType: 'public' },
|
|
|
|
{ action: 'plugin::users-permissions.auth.register', roleType: 'public' },
|
|
|
|
{ action: 'plugin::users-permissions.auth.emailconfirmation', roleType: 'public' },
|
|
|
|
{ action: 'plugin::users-permissions.user.me', roleType: null },
|
2020-04-15 19:47:55 +02:00
|
|
|
];
|
|
|
|
|
2021-09-07 12:26:01 +02:00
|
|
|
const transformRoutePrefixFor = pluginName => route => {
|
|
|
|
const prefix = route.config && route.config.prefix;
|
|
|
|
const path = prefix !== undefined ? `${prefix}${route.path}` : `/${pluginName}${route.path}`;
|
|
|
|
|
|
|
|
return {
|
|
|
|
...route,
|
|
|
|
path,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-07-08 11:20:13 +02:00
|
|
|
module.exports = ({ strapi }) => ({
|
2019-12-02 14:58:57 +08:00
|
|
|
getPlugins(lang = 'en') {
|
2021-09-03 11:11:37 +02:00
|
|
|
const request = require('request');
|
2019-04-09 12:09:03 +02:00
|
|
|
return new Promise(resolve => {
|
|
|
|
request(
|
|
|
|
{
|
|
|
|
uri: `https://marketplace.strapi.io/plugins?lang=${lang}`,
|
|
|
|
json: true,
|
2019-12-02 14:58:57 +08:00
|
|
|
timeout: 3000,
|
2019-04-09 12:09:03 +02:00
|
|
|
headers: {
|
|
|
|
'cache-control': 'max-age=3600',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
(err, response, body) => {
|
2019-06-17 00:02:27 +08:00
|
|
|
if (err || response.statusCode !== 200) {
|
2019-04-09 12:09:03 +02:00
|
|
|
return resolve([]);
|
|
|
|
}
|
2018-01-05 16:19:53 +01:00
|
|
|
|
2019-04-09 12:09:03 +02:00
|
|
|
resolve(body);
|
2019-06-08 16:23:52 +02:00
|
|
|
}
|
2019-04-09 12:09:03 +02:00
|
|
|
);
|
2018-01-05 16:19:53 +01:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2021-09-07 12:26:01 +02:00
|
|
|
// TODO: Filter on content-api only
|
2020-08-31 14:17:30 +02:00
|
|
|
getActions() {
|
2021-09-06 22:33:55 +02:00
|
|
|
const actionMap = {};
|
2017-11-16 17:59:41 +01:00
|
|
|
|
2021-09-06 22:33:55 +02:00
|
|
|
_.forEach(strapi.api, (api, apiName) => {
|
|
|
|
const controllers = _.mapValues(api.controllers, controller => {
|
|
|
|
return _.mapValues(controller, () => {
|
|
|
|
return { enabled: false, policy: '' };
|
|
|
|
});
|
|
|
|
});
|
2017-11-17 12:14:12 +01:00
|
|
|
|
2021-09-06 22:33:55 +02:00
|
|
|
actionMap[`api::${apiName}`] = { controllers };
|
|
|
|
});
|
2017-11-17 12:14:12 +01:00
|
|
|
|
2021-09-06 22:33:55 +02:00
|
|
|
_.forEach(strapi.plugins, (plugin, pluginName) => {
|
|
|
|
const controllers = _.mapValues(plugin.controllers, controller => {
|
|
|
|
return _.mapValues(controller, () => {
|
|
|
|
return { enabled: false, policy: '' };
|
|
|
|
});
|
|
|
|
});
|
2017-11-16 17:59:41 +01:00
|
|
|
|
2021-09-06 22:33:55 +02:00
|
|
|
actionMap[`plugin::${pluginName}`] = { controllers };
|
|
|
|
});
|
2017-11-16 17:59:41 +01:00
|
|
|
|
2021-09-06 22:33:55 +02:00
|
|
|
return actionMap;
|
2017-11-17 16:36:57 +01:00
|
|
|
},
|
2017-11-17 14:22:59 +01:00
|
|
|
|
2021-09-07 12:26:01 +02:00
|
|
|
// TODO: Filter on content-api only
|
2019-07-15 23:16:50 +02:00
|
|
|
async getRoutes() {
|
2021-09-07 12:26:01 +02:00
|
|
|
const routesMap = {};
|
2021-09-03 23:45:53 +02:00
|
|
|
|
2021-09-07 12:26:01 +02:00
|
|
|
_.forEach(strapi.api, (api, apiName) => {
|
|
|
|
const routes = _.flatMap(api.routes, route => {
|
2021-09-03 23:45:53 +02:00
|
|
|
if (_.has(route, 'routes')) {
|
2021-09-07 12:26:01 +02:00
|
|
|
return route.routes;
|
2021-09-03 23:45:53 +02:00
|
|
|
}
|
2021-09-07 12:26:01 +02:00
|
|
|
|
|
|
|
return route;
|
2021-09-03 23:45:53 +02:00
|
|
|
});
|
2017-11-30 16:34:43 +01:00
|
|
|
|
2021-09-07 12:26:01 +02:00
|
|
|
if (routes.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
routesMap[`api::${apiName}`] = routes;
|
|
|
|
});
|
2020-03-02 15:18:08 +01:00
|
|
|
|
2021-09-03 23:45:53 +02:00
|
|
|
_.forEach(strapi.plugins, (plugin, pluginName) => {
|
2021-09-07 12:26:01 +02:00
|
|
|
const transformPrefix = transformRoutePrefixFor(pluginName);
|
2020-03-02 15:18:08 +01:00
|
|
|
|
2021-09-07 12:26:01 +02:00
|
|
|
const routes = _.flatMap(plugin.routes, route => {
|
2021-09-03 23:45:53 +02:00
|
|
|
if (_.has(route, 'routes')) {
|
2021-09-07 12:26:01 +02:00
|
|
|
return route.routes.map(transformPrefix);
|
2021-09-03 23:45:53 +02:00
|
|
|
}
|
2021-09-07 12:26:01 +02:00
|
|
|
|
|
|
|
return transformPrefix(route);
|
2021-09-03 23:45:53 +02:00
|
|
|
});
|
|
|
|
|
2021-09-07 12:26:01 +02:00
|
|
|
if (routes.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
routesMap[`plugin::${pluginName}`] = routes;
|
2021-09-03 23:45:53 +02:00
|
|
|
});
|
2017-12-07 18:16:18 +01:00
|
|
|
|
2021-09-07 12:26:01 +02:00
|
|
|
return routesMap;
|
2017-11-30 16:34:43 +01:00
|
|
|
},
|
|
|
|
|
2021-09-07 12:26:01 +02:00
|
|
|
async syncPermissions() {
|
2021-08-06 18:09:49 +02:00
|
|
|
const roles = await strapi.query('plugin::users-permissions.role').findMany();
|
2021-09-06 22:33:55 +02:00
|
|
|
const dbPermissions = await strapi.query('plugin::users-permissions.permission').findMany();
|
2021-07-08 18:15:32 +02:00
|
|
|
|
2021-09-06 22:33:55 +02:00
|
|
|
const permissionsFoundInDB = _.uniq(_.map(dbPermissions, 'action'));
|
2020-03-02 15:18:08 +01:00
|
|
|
|
2021-09-06 22:33:55 +02:00
|
|
|
const appActions = _.flatMap(strapi.api, (api, apiName) => {
|
|
|
|
return _.flatMap(api.controllers, (controller, controllerName) => {
|
|
|
|
return _.keys(controller).map(actionName => {
|
|
|
|
return `api::${apiName}.${controllerName}.${_.toLower(actionName)}`;
|
|
|
|
});
|
2020-03-02 15:18:08 +01:00
|
|
|
});
|
2021-09-06 22:33:55 +02:00
|
|
|
});
|
2017-11-17 16:36:57 +01:00
|
|
|
|
2021-09-06 22:33:55 +02:00
|
|
|
const pluginsActions = _.flatMap(strapi.plugins, (plugin, pluginName) => {
|
|
|
|
return _.flatMap(plugin.controllers, (controller, controllerName) => {
|
|
|
|
return _.keys(controller).map(actionName => {
|
|
|
|
return `plugin::${pluginName}.${controllerName}.${_.toLower(actionName)}`;
|
|
|
|
});
|
2019-04-09 12:09:03 +02:00
|
|
|
});
|
2021-09-06 22:33:55 +02:00
|
|
|
});
|
2017-12-07 10:16:36 +01:00
|
|
|
|
2021-09-06 22:33:55 +02:00
|
|
|
const allActions = [...appActions, ...pluginsActions];
|
2021-07-08 18:15:32 +02:00
|
|
|
|
2021-09-06 22:33:55 +02:00
|
|
|
const toDelete = _.difference(permissionsFoundInDB, allActions);
|
2018-01-17 18:50:12 +01:00
|
|
|
|
2021-09-06 22:33:55 +02:00
|
|
|
await Promise.all(
|
|
|
|
toDelete.map(action => {
|
|
|
|
return strapi.query('plugin::users-permissions.permission').delete({ where: { action } });
|
|
|
|
})
|
|
|
|
);
|
2020-05-18 20:39:39 +02:00
|
|
|
|
2021-09-06 22:33:55 +02:00
|
|
|
if (permissionsFoundInDB.length === 0) {
|
|
|
|
// create default permissions
|
|
|
|
for (const role of roles) {
|
|
|
|
const toCreate = pipe(
|
|
|
|
filter(({ roleType }) => roleType === role.type || roleType === null),
|
|
|
|
map(prop('action'))
|
|
|
|
)(DEFAULT_PERMISSIONS);
|
|
|
|
|
|
|
|
await Promise.all(
|
|
|
|
toCreate.map(action => {
|
|
|
|
return strapi.query('plugin::users-permissions.permission').create({
|
|
|
|
data: {
|
|
|
|
action,
|
|
|
|
role: role.id,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
2017-11-17 14:22:59 +01:00
|
|
|
}
|
2017-11-17 16:36:57 +01:00
|
|
|
},
|
2017-11-17 14:22:59 +01:00
|
|
|
|
2019-08-12 15:35:40 +02:00
|
|
|
async initialize() {
|
2021-08-06 18:09:49 +02:00
|
|
|
const roleCount = await strapi.query('plugin::users-permissions.role').count();
|
2018-01-17 18:50:12 +01:00
|
|
|
|
2020-04-15 19:47:55 +02:00
|
|
|
if (roleCount === 0) {
|
2021-08-06 18:09:49 +02:00
|
|
|
await strapi.query('plugin::users-permissions.role').create({
|
2021-07-08 18:15:32 +02:00
|
|
|
data: {
|
|
|
|
name: 'Authenticated',
|
|
|
|
description: 'Default role given to authenticated user.',
|
|
|
|
type: 'authenticated',
|
|
|
|
},
|
2020-04-15 19:47:55 +02:00
|
|
|
});
|
2020-01-16 11:41:07 +01:00
|
|
|
|
2021-08-06 18:09:49 +02:00
|
|
|
await strapi.query('plugin::users-permissions.role').create({
|
2021-07-08 18:15:32 +02:00
|
|
|
data: {
|
|
|
|
name: 'Public',
|
|
|
|
description: 'Default role given to unauthenticated user.',
|
|
|
|
type: 'public',
|
|
|
|
},
|
2020-04-15 19:47:55 +02:00
|
|
|
});
|
|
|
|
}
|
2018-01-17 18:50:12 +01:00
|
|
|
|
2021-09-07 12:26:01 +02:00
|
|
|
return getService('users-permissions').syncPermissions();
|
2017-11-27 17:50:51 +01:00
|
|
|
},
|
|
|
|
|
2019-07-15 23:16:50 +02:00
|
|
|
async updateUserRole(user, role) {
|
2021-07-08 18:15:32 +02:00
|
|
|
return strapi
|
2021-08-06 18:09:49 +02:00
|
|
|
.query('plugin::users-permissions.user')
|
2021-07-08 18:15:32 +02:00
|
|
|
.update({ where: { id: user.id }, data: { role } });
|
2017-12-07 10:16:36 +01:00
|
|
|
},
|
|
|
|
|
2019-07-15 23:16:50 +02:00
|
|
|
template(layout, data) {
|
2018-01-25 08:38:46 +01:00
|
|
|
const compiledObject = _.template(layout);
|
|
|
|
return compiledObject(data);
|
2019-04-09 12:09:03 +02:00
|
|
|
},
|
2021-07-08 11:20:13 +02:00
|
|
|
});
|