| 
									
										
										
										
											2017-11-06 11:14:43 +01:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-16 17:59:41 +01:00
										 |  |  | const _ = require('lodash'); | 
					
						
							| 
									
										
										
										
											2018-01-05 16:19:53 +01:00
										 |  |  | const request = require('request'); | 
					
						
							| 
									
										
										
										
											2017-12-07 18:16:18 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-20 12:19:44 +02:00
										 |  |  | const DEFAULT_PERMISSIONS = [ | 
					
						
							| 
									
										
										
										
											2020-04-15 19:47:55 +02:00
										 |  |  |   { action: 'admincallback', controller: 'auth', type: 'users-permissions', roleType: 'public' }, | 
					
						
							|  |  |  |   { action: 'adminregister', controller: 'auth', type: 'users-permissions', roleType: 'public' }, | 
					
						
							|  |  |  |   { action: 'callback', controller: 'auth', type: 'users-permissions', roleType: 'public' }, | 
					
						
							|  |  |  |   { action: 'connect', controller: 'auth', type: 'users-permissions', roleType: null }, | 
					
						
							|  |  |  |   { action: 'forgotpassword', controller: 'auth', type: 'users-permissions', roleType: 'public' }, | 
					
						
							|  |  |  |   { action: 'register', controller: 'auth', type: 'users-permissions', roleType: 'public' }, | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     action: 'emailconfirmation', | 
					
						
							|  |  |  |     controller: 'auth', | 
					
						
							|  |  |  |     type: 'users-permissions', | 
					
						
							|  |  |  |     roleType: 'public', | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  |   { action: 'changepassword', controller: 'auth', type: 'users-permissions', roleType: 'public' }, | 
					
						
							|  |  |  |   { action: 'init', controller: 'userspermissions', type: null, roleType: null }, | 
					
						
							|  |  |  |   { action: 'me', controller: 'user', type: 'users-permissions', roleType: null }, | 
					
						
							|  |  |  |   { action: 'autoreload', controller: null, type: null, roleType: null }, | 
					
						
							|  |  |  | ]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-20 12:19:44 +02:00
										 |  |  | const isPermissionEnabled = (permission, role) => | 
					
						
							|  |  |  |   DEFAULT_PERMISSIONS.some( | 
					
						
							|  |  |  |     defaultPerm => | 
					
						
							|  |  |  |       (defaultPerm.action === null || permission.action === defaultPerm.action) && | 
					
						
							|  |  |  |       (defaultPerm.controller === null || permission.controller === defaultPerm.controller) && | 
					
						
							|  |  |  |       (defaultPerm.type === null || permission.type === defaultPerm.type) && | 
					
						
							|  |  |  |       (defaultPerm.roleType === null || role.type === defaultPerm.roleType) | 
					
						
							| 
									
										
										
										
											2020-04-15 19:47:55 +02:00
										 |  |  |   ); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-06 11:14:43 +01:00
										 |  |  | module.exports = { | 
					
						
							| 
									
										
										
										
											2019-07-15 23:16:50 +02:00
										 |  |  |   async createRole(params) { | 
					
						
							| 
									
										
										
										
											2018-02-06 14:43:26 +01:00
										 |  |  |     if (!params.type) { | 
					
						
							|  |  |  |       params.type = _.snakeCase(_.deburr(_.toLower(params.name))); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-15 23:16:50 +02:00
										 |  |  |     const role = await strapi | 
					
						
							|  |  |  |       .query('role', 'users-permissions') | 
					
						
							| 
									
										
										
										
											2019-04-09 12:09:03 +02:00
										 |  |  |       .create(_.omit(params, ['users', 'permissions'])); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-02 15:18:08 +01:00
										 |  |  |     const arrayOfPromises = Object.keys(params.permissions || {}).reduce((acc, type) => { | 
					
						
							|  |  |  |       Object.keys(params.permissions[type].controllers).forEach(controller => { | 
					
						
							|  |  |  |         Object.keys(params.permissions[type].controllers[controller]).forEach(action => { | 
					
						
							|  |  |  |           acc.push( | 
					
						
							|  |  |  |             strapi.query('permission', 'users-permissions').create({ | 
					
						
							|  |  |  |               role: role.id, | 
					
						
							|  |  |  |               type, | 
					
						
							|  |  |  |               controller, | 
					
						
							|  |  |  |               action: action.toLowerCase(), | 
					
						
							|  |  |  |               ...params.permissions[type].controllers[controller][action], | 
					
						
							|  |  |  |             }) | 
					
						
							|  |  |  |           ); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2017-11-27 16:49:56 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-02 15:18:08 +01:00
										 |  |  |       return acc; | 
					
						
							|  |  |  |     }, []); | 
					
						
							| 
									
										
										
										
											2018-01-23 15:38:43 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // Use Content Manager business logic to handle relation.
 | 
					
						
							| 
									
										
										
										
											2019-07-16 20:52:31 +02:00
										 |  |  |     if (params.users && params.users.length > 0) | 
					
						
							|  |  |  |       arrayOfPromises.push( | 
					
						
							|  |  |  |         strapi.query('role', 'users-permissions').update( | 
					
						
							|  |  |  |           { | 
					
						
							|  |  |  |             id: role.id, | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |           { users: params.users } | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |       ); | 
					
						
							| 
									
										
										
										
											2017-12-01 16:06:16 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-23 15:38:43 +01:00
										 |  |  |     return await Promise.all(arrayOfPromises); | 
					
						
							| 
									
										
										
										
											2017-11-27 17:02:45 +01:00
										 |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-15 23:16:50 +02:00
										 |  |  |   async deleteRole(roleID, publicRoleID) { | 
					
						
							|  |  |  |     const role = await strapi | 
					
						
							|  |  |  |       .query('role', 'users-permissions') | 
					
						
							| 
									
										
										
										
											2019-04-09 12:09:03 +02:00
										 |  |  |       .findOne({ id: roleID }, ['users', 'permissions']); | 
					
						
							| 
									
										
										
										
											2018-01-23 15:38:43 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (!role) { | 
					
						
							|  |  |  |       throw new Error('Cannot found this role'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Move users to guest role.
 | 
					
						
							|  |  |  |     const arrayOfPromises = role.users.reduce((acc, user) => { | 
					
						
							| 
									
										
										
										
											2019-04-09 12:09:03 +02:00
										 |  |  |       acc.push( | 
					
						
							| 
									
										
										
										
											2019-07-15 23:16:50 +02:00
										 |  |  |         strapi.query('user', 'users-permissions').update( | 
					
						
							|  |  |  |           { | 
					
						
							| 
									
										
										
										
											2019-08-13 16:31:29 +02:00
										 |  |  |             id: user.id, | 
					
						
							| 
									
										
										
										
											2019-07-15 23:16:50 +02:00
										 |  |  |           }, | 
					
						
							|  |  |  |           { | 
					
						
							|  |  |  |             role: publicRoleID, | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2019-04-09 12:09:03 +02:00
										 |  |  |       ); | 
					
						
							| 
									
										
										
										
											2018-01-23 15:38:43 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |       return acc; | 
					
						
							|  |  |  |     }, []); | 
					
						
							| 
									
										
										
										
											2017-11-27 17:50:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-23 15:38:43 +01:00
										 |  |  |     // Remove permissions related to this role.
 | 
					
						
							|  |  |  |     role.permissions.forEach(permission => { | 
					
						
							| 
									
										
										
										
											2019-04-09 12:09:03 +02:00
										 |  |  |       arrayOfPromises.push( | 
					
						
							| 
									
										
										
										
											2019-07-15 23:16:50 +02:00
										 |  |  |         strapi.query('permission', 'users-permissions').delete({ | 
					
						
							| 
									
										
										
										
											2019-08-13 16:31:29 +02:00
										 |  |  |           id: permission.id, | 
					
						
							| 
									
										
										
										
											2019-07-15 23:16:50 +02:00
										 |  |  |         }) | 
					
						
							| 
									
										
										
										
											2019-04-09 12:09:03 +02:00
										 |  |  |       ); | 
					
						
							| 
									
										
										
										
											2018-04-30 18:26:56 +02:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-12-01 16:06:16 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-23 15:38:43 +01:00
										 |  |  |     // Delete the role.
 | 
					
						
							| 
									
										
										
										
											2020-03-02 15:18:08 +01:00
										 |  |  |     arrayOfPromises.push(strapi.query('role', 'users-permissions').delete({ id: roleID })); | 
					
						
							| 
									
										
										
										
											2017-12-01 16:06:16 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-23 15:38:43 +01:00
										 |  |  |     return await Promise.all(arrayOfPromises); | 
					
						
							| 
									
										
										
										
											2017-11-27 16:49:56 +01:00
										 |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-02 14:58:57 +08:00
										 |  |  |   getPlugins(lang = 'en') { | 
					
						
							| 
									
										
										
										
											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
										 |  |  |     }); | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-15 23:16:50 +02:00
										 |  |  |   getActions(plugins = [], withInfo = true) { | 
					
						
							| 
									
										
										
										
											2019-04-09 12:09:03 +02:00
										 |  |  |     const generateActions = data => | 
					
						
							| 
									
										
										
										
											2017-11-17 14:22:59 +01:00
										 |  |  |       Object.keys(data).reduce((acc, key) => { | 
					
						
							| 
									
										
										
										
											2018-01-25 15:46:15 +01:00
										 |  |  |         if (_.isFunction(data[key])) { | 
					
						
							|  |  |  |           acc[key] = { enabled: false, policy: '' }; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-11-06 11:14:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-17 14:22:59 +01:00
										 |  |  |         return acc; | 
					
						
							| 
									
										
										
										
											2019-04-09 12:09:03 +02:00
										 |  |  |       }, {}); | 
					
						
							| 
									
										
										
										
											2017-11-17 14:22:59 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-05 13:07:39 +02:00
										 |  |  |     const appControllers = Object.keys(strapi.api || {}) | 
					
						
							|  |  |  |       .filter(key => !!strapi.api[key].controllers) | 
					
						
							| 
									
										
										
										
											2019-04-09 12:09:03 +02:00
										 |  |  |       .reduce( | 
					
						
							|  |  |  |         (acc, key) => { | 
					
						
							|  |  |  |           Object.keys(strapi.api[key].controllers).forEach(controller => { | 
					
						
							| 
									
										
										
										
											2020-03-02 15:18:08 +01:00
										 |  |  |             acc.controllers[controller] = generateActions(strapi.api[key].controllers[controller]); | 
					
						
							| 
									
										
										
										
											2019-04-09 12:09:03 +02:00
										 |  |  |           }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           return acc; | 
					
						
							|  |  |  |         }, | 
					
						
							| 
									
										
										
										
											2019-06-08 16:23:52 +02:00
										 |  |  |         { controllers: {} } | 
					
						
							| 
									
										
										
										
											2019-04-09 12:09:03 +02:00
										 |  |  |       ); | 
					
						
							| 
									
										
										
										
											2017-11-16 17:59:41 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-02 15:18:08 +01:00
										 |  |  |     const pluginsPermissions = Object.keys(strapi.plugins).reduce((acc, key) => { | 
					
						
							|  |  |  |       const initialState = { | 
					
						
							|  |  |  |         controllers: {}, | 
					
						
							|  |  |  |       }; | 
					
						
							| 
									
										
										
										
											2017-11-17 12:14:12 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-02 15:18:08 +01:00
										 |  |  |       if (withInfo) { | 
					
						
							|  |  |  |         initialState.information = plugins.find(plugin => plugin.id === key) || {}; | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2017-11-17 12:14:12 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-02 15:18:08 +01:00
										 |  |  |       acc[key] = Object.keys(strapi.plugins[key].controllers).reduce((obj, k) => { | 
					
						
							|  |  |  |         obj.controllers[k] = generateActions(strapi.plugins[key].controllers[k]); | 
					
						
							| 
									
										
										
										
											2017-11-17 12:14:12 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-02 15:18:08 +01:00
										 |  |  |         return obj; | 
					
						
							|  |  |  |       }, initialState); | 
					
						
							| 
									
										
										
										
											2017-11-17 12:14:12 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-02 15:18:08 +01:00
										 |  |  |       return acc; | 
					
						
							|  |  |  |     }, {}); | 
					
						
							| 
									
										
										
										
											2017-11-16 17:59:41 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     const permissions = { | 
					
						
							|  |  |  |       application: { | 
					
						
							|  |  |  |         controllers: appControllers.controllers, | 
					
						
							| 
									
										
										
										
											2017-11-17 12:14:12 +01:00
										 |  |  |       }, | 
					
						
							| 
									
										
										
										
											2017-11-16 17:59:41 +01:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-30 18:26:56 +02:00
										 |  |  |     return _.merge(permissions, pluginsPermissions); | 
					
						
							| 
									
										
										
										
											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 getRole(roleID, plugins) { | 
					
						
							|  |  |  |     const role = await strapi | 
					
						
							|  |  |  |       .query('role', 'users-permissions') | 
					
						
							| 
									
										
										
										
											2019-04-09 12:09:03 +02:00
										 |  |  |       .findOne({ id: roleID }, ['users', 'permissions']); | 
					
						
							| 
									
										
										
										
											2018-01-05 16:19:53 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-19 17:57:10 +01:00
										 |  |  |     if (!role) { | 
					
						
							| 
									
										
										
										
											2019-07-25 16:43:17 +02:00
										 |  |  |       throw new Error('Cannot find this role'); | 
					
						
							| 
									
										
										
										
											2018-01-19 17:57:10 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-11-27 17:50:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-22 18:19:44 +01:00
										 |  |  |     // Group by `type`.
 | 
					
						
							| 
									
										
										
										
											2019-07-18 15:49:24 +02:00
										 |  |  |     const permissions = role.permissions.reduce((acc, permission) => { | 
					
						
							| 
									
										
										
										
											2020-03-02 15:18:08 +01:00
										 |  |  |       _.set(acc, `${permission.type}.controllers.${permission.controller}.${permission.action}`, { | 
					
						
							|  |  |  |         enabled: _.toNumber(permission.enabled) == true, | 
					
						
							|  |  |  |         policy: permission.policy, | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2018-01-05 16:19:53 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-02 15:18:08 +01:00
										 |  |  |       if (permission.type !== 'application' && !acc[permission.type].information) { | 
					
						
							| 
									
										
										
										
											2019-04-09 12:09:03 +02:00
										 |  |  |         acc[permission.type].information = | 
					
						
							|  |  |  |           plugins.find(plugin => plugin.id === permission.type) || {}; | 
					
						
							| 
									
										
										
										
											2018-01-22 18:19:44 +01:00
										 |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       return acc; | 
					
						
							|  |  |  |     }, {}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-18 15:49:24 +02:00
										 |  |  |     return { | 
					
						
							|  |  |  |       ...role, | 
					
						
							|  |  |  |       permissions, | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2017-11-27 17:50:51 +01:00
										 |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-15 23:16:50 +02:00
										 |  |  |   async getRoles() { | 
					
						
							| 
									
										
										
										
											2020-03-02 15:18:08 +01:00
										 |  |  |     const roles = await strapi.query('role', 'users-permissions').find({ _sort: 'name' }, []); | 
					
						
							| 
									
										
										
										
											2017-11-27 16:04:57 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-17 18:50:12 +01:00
										 |  |  |     for (let i = 0; i < roles.length; ++i) { | 
					
						
							| 
									
										
										
										
											2019-07-15 23:16:50 +02:00
										 |  |  |       roles[i].nb_users = await strapi | 
					
						
							|  |  |  |         .query('user', 'users-permissions') | 
					
						
							| 
									
										
										
										
											2019-04-09 12:09:03 +02:00
										 |  |  |         .count({ role: roles[i].id }); | 
					
						
							| 
									
										
										
										
											2018-01-17 18:50:12 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-11-27 16:04:57 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-17 18:50:12 +01:00
										 |  |  |     return roles; | 
					
						
							| 
									
										
										
										
											2017-11-27 16:04:57 +01:00
										 |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-15 23:16:50 +02:00
										 |  |  |   async getRoutes() { | 
					
						
							| 
									
										
										
										
											2018-01-17 18:50:12 +01:00
										 |  |  |     const routes = Object.keys(strapi.api || {}).reduce((acc, current) => { | 
					
						
							| 
									
										
										
										
											2018-04-23 14:39:27 +02:00
										 |  |  |       return acc.concat(_.get(strapi.api[current].config, 'routes', [])); | 
					
						
							| 
									
										
										
										
											2018-01-17 18:50:12 +01:00
										 |  |  |     }, []); | 
					
						
							| 
									
										
										
										
											2018-10-01 14:29:11 +02:00
										 |  |  |     const clonedPlugins = _.cloneDeep(strapi.plugins); | 
					
						
							| 
									
										
										
										
											2020-03-02 15:18:08 +01:00
										 |  |  |     const pluginsRoutes = Object.keys(clonedPlugins || {}).reduce((acc, current) => { | 
					
						
							|  |  |  |       const routes = _.get(clonedPlugins, [current, 'config', 'routes'], []).reduce((acc, curr) => { | 
					
						
							|  |  |  |         const prefix = curr.config.prefix; | 
					
						
							|  |  |  |         const path = prefix !== undefined ? `${prefix}${curr.path}` : `/${current}${curr.path}`; | 
					
						
							|  |  |  |         _.set(curr, 'path', path); | 
					
						
							| 
									
										
										
										
											2017-11-30 16:34:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-02 15:18:08 +01:00
										 |  |  |         return acc.concat(curr); | 
					
						
							|  |  |  |       }, []); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       acc[current] = routes; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       return acc; | 
					
						
							|  |  |  |     }, {}); | 
					
						
							| 
									
										
										
										
											2017-12-07 18:16:18 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-17 18:50:12 +01:00
										 |  |  |     return _.merge({ application: routes }, pluginsRoutes); | 
					
						
							| 
									
										
										
										
											2017-11-30 16:34:43 +01:00
										 |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-10 18:11:55 +02:00
										 |  |  |   async updatePermissions() { | 
					
						
							| 
									
										
										
										
											2020-04-15 19:47:55 +02:00
										 |  |  |     const { primaryKey } = strapi.query('permission', 'users-permissions'); | 
					
						
							|  |  |  |     const roles = await strapi.query('role', 'users-permissions').find({}, []); | 
					
						
							|  |  |  |     const rolesMap = roles.reduce((map, role) => ({ ...map, [role[primaryKey]]: role }), {}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const dbPermissions = await strapi | 
					
						
							| 
									
										
										
										
											2019-07-15 23:16:50 +02:00
										 |  |  |       .query('permission', 'users-permissions') | 
					
						
							| 
									
										
										
										
											2019-04-09 16:01:01 +02:00
										 |  |  |       .find({ _limit: -1 }); | 
					
						
							| 
									
										
										
										
											2020-04-15 19:47:55 +02:00
										 |  |  |     let permissionsFoundInDB = dbPermissions.map( | 
					
						
							|  |  |  |       p => `${p.type}.${p.controller}.${p.action}.${p.role[primaryKey]}` | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |     permissionsFoundInDB = _.uniq(permissionsFoundInDB); | 
					
						
							| 
									
										
										
										
											2018-01-17 18:50:12 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // Aggregate first level actions.
 | 
					
						
							|  |  |  |     const appActions = Object.keys(strapi.api || {}).reduce((acc, api) => { | 
					
						
							| 
									
										
										
										
											2020-03-02 15:18:08 +01:00
										 |  |  |       Object.keys(_.get(strapi.api[api], 'controllers', {})).forEach(controller => { | 
					
						
							|  |  |  |         const actions = Object.keys(strapi.api[api].controllers[controller]) | 
					
						
							|  |  |  |           .filter(action => _.isFunction(strapi.api[api].controllers[controller][action])) | 
					
						
							|  |  |  |           .map(action => `application.${controller}.${action.toLowerCase()}`); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         acc = acc.concat(actions); | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2017-11-17 16:36:57 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-17 18:50:12 +01:00
										 |  |  |       return acc; | 
					
						
							|  |  |  |     }, []); | 
					
						
							| 
									
										
										
										
											2017-11-17 16:36:57 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-17 18:50:12 +01:00
										 |  |  |     // Aggregate plugins' actions.
 | 
					
						
							|  |  |  |     const pluginsActions = Object.keys(strapi.plugins).reduce((acc, plugin) => { | 
					
						
							| 
									
										
										
										
											2019-04-09 12:09:03 +02:00
										 |  |  |       Object.keys(strapi.plugins[plugin].controllers).forEach(controller => { | 
					
						
							| 
									
										
										
										
											2020-03-02 15:18:08 +01:00
										 |  |  |         const actions = Object.keys(strapi.plugins[plugin].controllers[controller]) | 
					
						
							|  |  |  |           .filter(action => _.isFunction(strapi.plugins[plugin].controllers[controller][action])) | 
					
						
							| 
									
										
										
										
											2019-04-09 12:09:03 +02:00
										 |  |  |           .map(action => `${plugin}.${controller}.${action.toLowerCase()}`); | 
					
						
							| 
									
										
										
										
											2018-01-17 18:50:12 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-09 12:09:03 +02:00
										 |  |  |         acc = acc.concat(actions); | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2017-12-07 10:16:36 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-17 18:50:12 +01:00
										 |  |  |       return acc; | 
					
						
							|  |  |  |     }, []); | 
					
						
							| 
									
										
										
										
											2017-11-17 16:36:57 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-15 19:47:55 +02:00
										 |  |  |     const actionsFoundInFiles = appActions.concat(pluginsActions); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // create permissions for each role
 | 
					
						
							|  |  |  |     let permissionsFoundInFiles = actionsFoundInFiles.reduce( | 
					
						
							|  |  |  |       (acc, action) => [...acc, ...roles.map(role => `${action}.${role[primaryKey]}`)], | 
					
						
							|  |  |  |       [] | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |     permissionsFoundInFiles = _.uniq(permissionsFoundInFiles); | 
					
						
							| 
									
										
										
										
											2018-01-17 18:50:12 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // Compare to know if actions have been added or removed from controllers.
 | 
					
						
							| 
									
										
										
										
											2020-04-15 19:47:55 +02:00
										 |  |  |     if (!_.isEqual(permissionsFoundInDB.sort(), permissionsFoundInFiles.sort())) { | 
					
						
							| 
									
										
										
										
											2019-04-09 12:09:03 +02:00
										 |  |  |       const splitted = str => { | 
					
						
							| 
									
										
										
										
											2020-04-15 19:47:55 +02:00
										 |  |  |         const [type, controller, action, roleId] = str.split('.'); | 
					
						
							| 
									
										
										
										
											2018-01-17 18:50:12 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-15 19:47:55 +02:00
										 |  |  |         return { type, controller, action, roleId }; | 
					
						
							| 
									
										
										
										
											2018-01-17 18:50:12 +01:00
										 |  |  |       }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-15 19:47:55 +02:00
										 |  |  |       // We have to know the difference to add or remove the permissions entries in the database.
 | 
					
						
							|  |  |  |       const toRemove = _.difference(permissionsFoundInDB, permissionsFoundInFiles).map(splitted); | 
					
						
							|  |  |  |       const toAdd = _.difference(permissionsFoundInFiles, permissionsFoundInDB).map(splitted); | 
					
						
							| 
									
										
										
										
											2018-01-17 18:50:12 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-15 23:16:50 +02:00
										 |  |  |       const query = strapi.query('permission', 'users-permissions'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-17 18:50:12 +01:00
										 |  |  |       // Execute request to update entries in database for each role.
 | 
					
						
							| 
									
										
										
										
											2019-07-15 23:16:50 +02:00
										 |  |  |       await Promise.all([ | 
					
						
							| 
									
										
										
										
											2020-04-15 19:47:55 +02:00
										 |  |  |         Promise.all( | 
					
						
							|  |  |  |           toAdd.map(permission => | 
					
						
							|  |  |  |             query.create({ | 
					
						
							|  |  |  |               type: permission.type, | 
					
						
							|  |  |  |               controller: permission.controller, | 
					
						
							|  |  |  |               action: permission.action, | 
					
						
							| 
									
										
										
										
											2020-04-20 12:19:44 +02:00
										 |  |  |               enabled: isPermissionEnabled(permission, rolesMap[permission.roleId]), | 
					
						
							| 
									
										
										
										
											2020-04-15 19:47:55 +02:00
										 |  |  |               policy: '', | 
					
						
							|  |  |  |               role: permission.roleId, | 
					
						
							|  |  |  |             }) | 
					
						
							|  |  |  |           ) | 
					
						
							|  |  |  |         ), | 
					
						
							|  |  |  |         Promise.all( | 
					
						
							|  |  |  |           toRemove.map(permission => { | 
					
						
							|  |  |  |             const { type, controller, action, roleId: role } = permission; | 
					
						
							|  |  |  |             return query.delete({ type, controller, action, role }); | 
					
						
							|  |  |  |           }) | 
					
						
							|  |  |  |         ), | 
					
						
							| 
									
										
										
										
											2019-07-15 23:16:50 +02:00
										 |  |  |       ]); | 
					
						
							| 
									
										
										
										
											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() { | 
					
						
							| 
									
										
										
										
											2019-07-15 23:16:50 +02:00
										 |  |  |     const roleCount = await strapi.query('role', 'users-permissions').count(); | 
					
						
							| 
									
										
										
										
											2018-01-17 18:50:12 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-15 19:47:55 +02:00
										 |  |  |     if (roleCount === 0) { | 
					
						
							|  |  |  |       await strapi.query('role', 'users-permissions').create({ | 
					
						
							|  |  |  |         name: 'Authenticated', | 
					
						
							|  |  |  |         description: 'Default role given to authenticated user.', | 
					
						
							|  |  |  |         type: 'authenticated', | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2020-01-16 11:41:07 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-15 19:47:55 +02:00
										 |  |  |       await strapi.query('role', 'users-permissions').create({ | 
					
						
							|  |  |  |         name: 'Public', | 
					
						
							|  |  |  |         description: 'Default role given to unauthenticated user.', | 
					
						
							|  |  |  |         type: 'public', | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-01-17 18:50:12 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 15:35:40 +02:00
										 |  |  |     return this.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, []), | 
					
						
							| 
									
										
										
										
											2020-03-02 15:18:08 +01:00
										 |  |  |       strapi.query('role', 'users-permissions').findOne({ type: 'authenticated' }, []), | 
					
						
							| 
									
										
										
										
											2018-01-22 18:19:44 +01:00
										 |  |  |     ]); | 
					
						
							| 
									
										
										
										
											2017-11-27 17:50:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-16 20:52:31 +02:00
										 |  |  |     await strapi | 
					
						
							|  |  |  |       .query('role', 'users-permissions') | 
					
						
							|  |  |  |       .update({ id: roleID }, _.pick(body, ['name', 'description'])); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     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( | 
					
						
							|  |  |  |                 strapi.query('permission', 'users-permissions').update( | 
					
						
							|  |  |  |                   { | 
					
						
							|  |  |  |                     role: roleID, | 
					
						
							|  |  |  |                     type, | 
					
						
							|  |  |  |                     controller, | 
					
						
							|  |  |  |                     action: action.toLowerCase(), | 
					
						
							|  |  |  |                   }, | 
					
						
							|  |  |  |                   bodyAction | 
					
						
							|  |  |  |                 ) | 
					
						
							|  |  |  |               ); | 
					
						
							| 
									
										
										
										
											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) { | 
					
						
							| 
									
										
										
										
											2020-03-02 15:18:08 +01:00
										 |  |  |     return strapi.query('user', 'users-permissions').update({ id: user.id }, { 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
										 |  |  |   }, | 
					
						
							| 
									
										
										
										
											2017-11-06 11:14:43 +01:00
										 |  |  | }; |