| 
									
										
										
										
											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'); | 
					
						
							| 
									
										
										
										
											2022-01-10 18:11:32 +01:00
										 |  |  | const urlJoin = require('url-join'); | 
					
						
							| 
									
										
										
										
											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.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' }, | 
					
						
							| 
									
										
										
										
											2020-04-15 19:47:55 +02:00
										 |  |  | ]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-08 23:33:39 +02:00
										 |  |  | const transformRoutePrefixFor = (pluginName) => (route) => { | 
					
						
							| 
									
										
										
										
											2021-09-07 12:26:01 +02:00
										 |  |  |   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) => { | 
					
						
							| 
									
										
										
										
											2021-10-08 09:00:33 +02:00
										 |  |  |       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) => { | 
					
						
							| 
									
										
										
										
											2021-10-08 09:00:33 +02:00
										 |  |  |       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: '', | 
					
						
							|  |  |  |             }; | 
					
						
							|  |  |  |           }); | 
					
						
							| 
									
										
										
										
											2017-11-17 12:14:12 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-08 09:00:33 +02:00
										 |  |  |           return acc; | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         {} | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if (!_.isEmpty(controllers)) { | 
					
						
							|  |  |  |         actionMap[`api::${apiName}`] = { controllers }; | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2021-09-06 22:33:55 +02:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-11-17 12:14:12 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-06 22:33:55 +02:00
										 |  |  |     _.forEach(strapi.plugins, (plugin, pluginName) => { | 
					
						
							| 
									
										
										
										
											2021-10-08 09:00:33 +02:00
										 |  |  |       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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-08 09:00:33 +02: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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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) => { | 
					
						
							| 
									
										
										
										
											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')) { | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							| 
									
										
										
										
											2022-08-08 23:33:39 +02:00
										 |  |  |       }).filter((route) => route.info.type === 'content-api'); | 
					
						
							| 
									
										
										
										
											2017-11-30 16:34:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-07 12:26:01 +02:00
										 |  |  |       if (routes.length === 0) { | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-10 18:11:32 +01:00
										 |  |  |       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, | 
					
						
							| 
									
										
										
										
											2022-01-10 18:11:32 +01:00
										 |  |  |         path: urlJoin(apiPrefix, route.path), | 
					
						
							| 
									
										
										
										
											2021-09-24 09:55:01 +02:00
										 |  |  |       })); | 
					
						
							| 
									
										
										
										
											2021-09-07 12:26:01 +02:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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')) { | 
					
						
							| 
									
										
										
										
											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); | 
					
						
							| 
									
										
										
										
											2022-08-08 23:33:39 +02:00
										 |  |  |       }).filter((route) => route.info.type === 'content-api'); | 
					
						
							| 
									
										
										
										
											2021-09-03 23:45:53 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-07 12:26:01 +02:00
										 |  |  |       if (routes.length === 0) { | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-10 18:11:32 +01:00
										 |  |  |       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, | 
					
						
							| 
									
										
										
										
											2022-01-10 18:11:32 +01:00
										 |  |  |         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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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) => { | 
					
						
							| 
									
										
										
										
											2022-08-08 23:33:39 +02:00
										 |  |  |         return _.keys(controller).map((actionName) => { | 
					
						
							| 
									
										
										
										
											2021-09-24 12:23:02 +02:00
										 |  |  |           return `api::${apiName}.${controllerName}.${actionName}`; | 
					
						
							| 
									
										
										
										
											2021-09-06 22:33:55 +02:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											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) => { | 
					
						
							| 
									
										
										
										
											2022-08-08 23:33:39 +02:00
										 |  |  |         return _.keys(controller).map((actionName) => { | 
					
						
							| 
									
										
										
										
											2021-09-24 12:23:02 +02:00
										 |  |  |           return `plugin::${pluginName}.${controllerName}.${actionName}`; | 
					
						
							| 
									
										
										
										
											2021-09-06 22:33:55 +02:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											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( | 
					
						
							| 
									
										
										
										
											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 } }); | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |     ); | 
					
						
							| 
									
										
										
										
											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( | 
					
						
							| 
									
										
										
										
											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(); | 
					
						
							| 
									
										
										
										
											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
										 |  |  | }); |