250 lines
7.2 KiB
JavaScript
Raw Normal View History

'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');
const urlJoin = require('url-join');
const {
template: { createStrictInterpolationRegExp },
errors,
objects,
} = require('@strapi/utils');
2021-09-03 11:11:37 +02:00
2021-07-08 18:15:32 +02:00
const { getService } = require('../utils');
const DEFAULT_PERMISSIONS = [
2021-09-06 22:33:55 +02:00
{ action: 'plugin::users-permissions.auth.callback', roleType: 'public' },
2022-07-01 19:58:51 +02:00
{ action: 'plugin::users-permissions.auth.connect', roleType: 'public' },
{ action: 'plugin::users-permissions.auth.forgotPassword', roleType: 'public' },
{ action: 'plugin::users-permissions.auth.resetPassword', roleType: 'public' },
2021-09-06 22:33:55 +02:00
{ action: 'plugin::users-permissions.auth.register', roleType: 'public' },
2022-07-01 19:58:51 +02:00
{ action: 'plugin::users-permissions.auth.emailConfirmation', roleType: 'public' },
{ action: 'plugin::users-permissions.auth.sendEmailConfirmation', roleType: 'public' },
{ action: 'plugin::users-permissions.user.me', roleType: 'authenticated' },
2022-08-03 22:43:03 +02:00
{ action: 'plugin::users-permissions.auth.changePassword', roleType: 'authenticated' },
];
2022-08-08 23:33:39 +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 }) => ({
2021-09-07 21:03:30 +02:00
getActions({ defaultEnable = false } = {}) {
2021-09-06 22:33:55 +02:00
const actionMap = {};
2017-11-16 17:59:41 +01:00
2022-08-08 23:33:39 +02:00
const isContentApi = (action) => {
if (!_.has(action, Symbol.for('__type__'))) {
return false;
}
return action[Symbol.for('__type__')].includes('content-api');
};
2021-09-06 22:33:55 +02:00
_.forEach(strapi.api, (api, apiName) => {
const controllers = _.reduce(
api.controllers,
(acc, controller, controllerName) => {
const contentApiActions = _.pickBy(controller, isContentApi);
if (_.isEmpty(contentApiActions)) {
return acc;
}
acc[controllerName] = _.mapValues(contentApiActions, () => {
return {
enabled: defaultEnable,
policy: '',
};
});
return acc;
},
{}
);
if (!_.isEmpty(controllers)) {
actionMap[`api::${apiName}`] = { controllers };
}
2021-09-06 22:33:55 +02:00
});
2021-09-06 22:33:55 +02:00
_.forEach(strapi.plugins, (plugin, pluginName) => {
const controllers = _.reduce(
plugin.controllers,
(acc, controller, controllerName) => {
const contentApiActions = _.pickBy(controller, isContentApi);
if (_.isEmpty(contentApiActions)) {
return acc;
}
2017-11-16 17:59:41 +01:00
acc[controllerName] = _.mapValues(contentApiActions, () => {
return {
enabled: defaultEnable,
policy: '',
};
});
return acc;
},
{}
);
if (!_.isEmpty(controllers)) {
actionMap[`plugin::${pluginName}`] = { controllers };
}
2021-09-06 22:33:55 +02:00
});
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
async getRoutes() {
const routesMap = {};
2021-09-03 23:45:53 +02:00
_.forEach(strapi.api, (api, apiName) => {
2022-08-08 23:33:39 +02:00
const routes = _.flatMap(api.routes, (route) => {
2021-09-03 23:45:53 +02:00
if (_.has(route, 'routes')) {
return route.routes;
2021-09-03 23:45:53 +02:00
}
return route;
2022-08-08 23:33:39 +02:00
}).filter((route) => route.info.type === 'content-api');
2017-11-30 16:34:43 +01:00
if (routes.length === 0) {
return;
}
const apiPrefix = strapi.config.get('api.rest.prefix');
2022-08-08 23:33:39 +02:00
routesMap[`api::${apiName}`] = routes.map((route) => ({
2021-09-24 09:55:01 +02:00
...route,
path: urlJoin(apiPrefix, route.path),
2021-09-24 09:55:01 +02:00
}));
});
2021-09-03 23:45:53 +02:00
_.forEach(strapi.plugins, (plugin, pluginName) => {
const transformPrefix = transformRoutePrefixFor(pluginName);
2022-08-08 23:33:39 +02:00
const routes = _.flatMap(plugin.routes, (route) => {
2021-09-03 23:45:53 +02:00
if (_.has(route, 'routes')) {
return route.routes.map(transformPrefix);
2021-09-03 23:45:53 +02:00
}
return transformPrefix(route);
2022-08-08 23:33:39 +02:00
}).filter((route) => route.info.type === 'content-api');
2021-09-03 23:45:53 +02:00
if (routes.length === 0) {
return;
}
const apiPrefix = strapi.config.get('api.rest.prefix');
2022-08-08 23:33:39 +02:00
routesMap[`plugin::${pluginName}`] = routes.map((route) => ({
2021-09-24 09:55:01 +02:00
...route,
path: urlJoin(apiPrefix, route.path),
2021-09-24 09:55:01 +02:00
}));
2021-09-03 23:45:53 +02:00
});
2017-12-07 18:16:18 +01:00
return routesMap;
2017-11-30 16:34:43 +01: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'));
2021-09-06 22:33:55 +02:00
const appActions = _.flatMap(strapi.api, (api, apiName) => {
return _.flatMap(api.controllers, (controller, controllerName) => {
2022-08-08 23:33:39 +02:00
return _.keys(controller).map((actionName) => {
return `api::${apiName}.${controllerName}.${actionName}`;
2021-09-06 22:33:55 +02: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) => {
2022-08-08 23:33:39 +02:00
return _.keys(controller).map((actionName) => {
return `plugin::${pluginName}.${controllerName}.${actionName}`;
2021-09-06 22:33:55 +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);
2021-09-06 22:33:55 +02:00
await Promise.all(
2022-08-08 23:33:39 +02:00
toDelete.map((action) => {
2021-09-06 22:33:55 +02:00
return strapi.query('plugin::users-permissions.permission').delete({ where: { action } });
})
);
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(
2022-08-08 23:33:39 +02:00
toCreate.map((action) => {
2021-09-06 22:33:55 +02:00
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();
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',
},
});
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',
},
});
}
return getService('users-permissions').syncPermissions();
2017-11-27 17:50:51 +01: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
},
template(layout, data) {
const allowedTemplateVariables = objects.keysDeep(data);
// Create a strict interpolation RegExp based on possible variable names
const interpolate = createStrictInterpolationRegExp(allowedTemplateVariables, 'g');
try {
2023-01-09 18:28:30 +01:00
return _.template(layout, { interpolate, evaluate: false, escape: false })(data);
} catch (e) {
throw new errors.ApplicationError('Invalid email template');
}
},
2021-07-08 11:20:13 +02:00
});