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-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
|
|
|
});
|
|
|
|
},
|
|
|
|
|
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
|
|
|
|
2019-07-15 23:16:50 +02:00
|
|
|
async getRoutes() {
|
2021-09-03 23:45:53 +02:00
|
|
|
// TODO: remove or refactor
|
|
|
|
|
|
|
|
const applicationRoutes = [];
|
|
|
|
|
|
|
|
_.forEach(strapi.api, api => {
|
|
|
|
_.forEach(api.routes, route => {
|
|
|
|
if (_.has(route, 'routes')) {
|
|
|
|
applicationRoutes.push(...route.routes);
|
|
|
|
} else {
|
|
|
|
applicationRoutes.push(route);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2017-11-30 16:34:43 +01:00
|
|
|
|
2021-09-03 23:45:53 +02:00
|
|
|
const pluginsRoutes = {};
|
2020-03-02 15:18:08 +01:00
|
|
|
|
2021-09-03 23:45:53 +02:00
|
|
|
_.forEach(strapi.plugins, (plugin, pluginName) => {
|
|
|
|
const pluginRoutes = [];
|
2020-03-02 15:18:08 +01:00
|
|
|
|
2021-09-03 23:45:53 +02:00
|
|
|
_.forEach(plugin.routes, route => {
|
|
|
|
if (_.has(route, 'routes')) {
|
|
|
|
pluginRoutes.push(
|
|
|
|
...route.routes.map(route => {
|
|
|
|
const prefix = route.config && route.config.prefix;
|
|
|
|
const path =
|
|
|
|
prefix !== undefined ? `${prefix}${route.path}` : `/${pluginName}${route.path}`;
|
|
|
|
|
|
|
|
return {
|
|
|
|
...route,
|
|
|
|
path,
|
|
|
|
};
|
|
|
|
})
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
const prefix = route.config && route.config.prefix;
|
|
|
|
const path =
|
|
|
|
prefix !== undefined ? `${prefix}${route.path}` : `/${pluginName}${route.path}`;
|
|
|
|
|
|
|
|
pluginRoutes.push({
|
|
|
|
...route,
|
|
|
|
path,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
pluginsRoutes[pluginName] = pluginRoutes;
|
|
|
|
});
|
2017-12-07 18:16:18 +01:00
|
|
|
|
2021-09-03 23:45:53 +02:00
|
|
|
return _.merge({ application: applicationRoutes }, pluginsRoutes);
|
2017-11-30 16:34:43 +01:00
|
|
|
},
|
|
|
|
|
2019-04-10 18:11:55 +02:00
|
|
|
async updatePermissions() {
|
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-07-08 21:53:30 +02:00
|
|
|
return getService('users-permissions').updatePermissions();
|
2018-01-17 18:50:12 +01:00
|
|
|
},
|
|
|
|
|
2019-07-15 23:16:50 +02:00
|
|
|
async updateRole(roleID, body) {
|
2019-04-09 12:09:03 +02:00
|
|
|
const [role, authenticated] = await Promise.all([
|
2018-01-22 18:19:44 +01:00
|
|
|
this.getRole(roleID, []),
|
2021-08-06 18:09:49 +02:00
|
|
|
strapi.query('plugin::users-permissions.role').findOne({ where: { type: 'authenticated' } }),
|
2018-01-22 18:19:44 +01:00
|
|
|
]);
|
2017-11-27 17:50:51 +01:00
|
|
|
|
2021-08-06 18:09:49 +02:00
|
|
|
await strapi.query('plugin::users-permissions.role').update({
|
2021-07-08 18:15:32 +02:00
|
|
|
where: { id: roleID },
|
|
|
|
data: _.pick(body, ['name', 'description']),
|
|
|
|
});
|
2019-07-16 20:52:31 +02:00
|
|
|
|
|
|
|
await Promise.all(
|
2019-07-17 10:15:22 +02:00
|
|
|
Object.keys(body.permissions || {}).reduce((acc, type) => {
|
2019-04-09 12:09:03 +02:00
|
|
|
Object.keys(body.permissions[type].controllers).forEach(controller => {
|
2020-03-02 15:18:08 +01:00
|
|
|
Object.keys(body.permissions[type].controllers[controller]).forEach(action => {
|
|
|
|
const bodyAction = body.permissions[type].controllers[controller][action];
|
|
|
|
const currentAction = _.get(
|
|
|
|
role.permissions,
|
|
|
|
`${type}.controllers.${controller}.${action}`,
|
|
|
|
{}
|
|
|
|
);
|
2019-04-09 12:09:03 +02:00
|
|
|
|
2020-03-02 15:18:08 +01:00
|
|
|
if (!_.isEqual(bodyAction, currentAction)) {
|
|
|
|
acc.push(
|
2021-08-06 18:09:49 +02:00
|
|
|
strapi.query('plugin::users-permissions.permission').update({
|
2021-07-08 18:15:32 +02:00
|
|
|
where: {
|
2020-03-02 15:18:08 +01:00
|
|
|
role: roleID,
|
|
|
|
type,
|
|
|
|
controller,
|
|
|
|
action: action.toLowerCase(),
|
|
|
|
},
|
2021-07-08 18:15:32 +02:00
|
|
|
data: bodyAction,
|
|
|
|
})
|
2020-03-02 15:18:08 +01:00
|
|
|
);
|
2019-06-08 16:23:52 +02:00
|
|
|
}
|
2020-03-02 15:18:08 +01:00
|
|
|
});
|
2018-01-22 18:19:44 +01:00
|
|
|
});
|
2018-01-17 18:50:12 +01:00
|
|
|
|
2019-04-09 12:09:03 +02:00
|
|
|
return acc;
|
2019-07-16 20:52:31 +02:00
|
|
|
}, [])
|
2019-04-09 12:09:03 +02:00
|
|
|
);
|
2018-03-12 14:04:43 +01:00
|
|
|
|
2018-01-22 18:19:44 +01:00
|
|
|
// Add user to this role.
|
2019-07-16 20:52:31 +02:00
|
|
|
const newUsers = _.differenceBy(body.users, role.users, 'id');
|
|
|
|
await Promise.all(newUsers.map(user => this.updateUserRole(user, roleID)));
|
2017-12-05 16:44:54 +01:00
|
|
|
|
2019-07-16 20:52:31 +02:00
|
|
|
const oldUsers = _.differenceBy(role.users, body.users, 'id');
|
2020-03-02 15:18:08 +01:00
|
|
|
await Promise.all(oldUsers.map(user => this.updateUserRole(user, authenticated.id)));
|
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
|
|
|
});
|