| 
									
										
										
										
											2020-05-18 16:21:02 +02:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-20 17:30:05 +02:00
										 |  |  | const { ApplicationError } = require('@strapi/utils').errors; | 
					
						
							| 
									
										
										
										
											2020-05-19 16:11:19 +02:00
										 |  |  | const { validateRoleUpdateInput } = require('../validation/role'); | 
					
						
							| 
									
										
										
										
											2020-05-28 11:29:59 +02:00
										 |  |  | const { validatedUpdatePermissionsInput } = require('../validation/permission'); | 
					
						
							| 
									
										
										
										
											2020-06-23 16:31:16 +02:00
										 |  |  | const { EDITOR_CODE, AUTHOR_CODE, SUPER_ADMIN_CODE } = require('../services/constants'); | 
					
						
							| 
									
										
										
										
											2021-03-25 14:59:44 +01:00
										 |  |  | const { getService } = require('../utils'); | 
					
						
							| 
									
										
										
										
											2020-05-19 16:11:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-18 16:21:02 +02:00
										 |  |  | module.exports = { | 
					
						
							| 
									
										
										
										
											2020-05-27 17:15:58 +02:00
										 |  |  |   /** | 
					
						
							|  |  |  |    * Returns on role by id | 
					
						
							|  |  |  |    * @param {KoaContext} ctx - koa context | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2020-05-18 16:21:02 +02:00
										 |  |  |   async findOne(ctx) { | 
					
						
							|  |  |  |     const { id } = ctx.params; | 
					
						
							| 
									
										
										
										
											2021-03-25 14:59:44 +01:00
										 |  |  |     const role = await getService('role').findOneWithUsersCount({ id }); | 
					
						
							| 
									
										
										
										
											2020-05-18 16:21:02 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (!role) { | 
					
						
							|  |  |  |       return ctx.notFound('role.notFound'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ctx.body = { | 
					
						
							|  |  |  |       data: role, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |   }, | 
					
						
							| 
									
										
										
										
											2020-05-27 17:15:58 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * Returns every roles | 
					
						
							|  |  |  |    * @param {KoaContext} ctx - koa context | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2020-05-19 15:40:04 +02:00
										 |  |  |   async findAll(ctx) { | 
					
						
							| 
									
										
										
										
											2021-03-25 14:59:44 +01:00
										 |  |  |     const roles = await getService('role').findAllWithUsersCount(); | 
					
						
							| 
									
										
										
										
											2020-05-29 17:23:42 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-18 16:29:32 +02:00
										 |  |  |     ctx.body = { | 
					
						
							|  |  |  |       data: roles, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |   }, | 
					
						
							| 
									
										
										
										
											2020-05-27 17:15:58 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * Updates a role by id | 
					
						
							|  |  |  |    * @param {KoaContext} ctx - koa context | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2020-05-19 16:11:19 +02:00
										 |  |  |   async update(ctx) { | 
					
						
							|  |  |  |     const { id } = ctx.params; | 
					
						
							| 
									
										
										
										
											2021-03-25 14:59:44 +01:00
										 |  |  |     const { body } = ctx.request; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const roleService = getService('role'); | 
					
						
							| 
									
										
										
										
											2020-05-19 16:11:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-20 17:30:05 +02:00
										 |  |  |     await validateRoleUpdateInput(body); | 
					
						
							| 
									
										
										
										
											2020-05-19 16:11:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-25 14:59:44 +01:00
										 |  |  |     const role = await roleService.findOne({ id }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-19 16:11:19 +02:00
										 |  |  |     if (!role) { | 
					
						
							|  |  |  |       return ctx.notFound('role.notFound'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-20 11:39:26 +02:00
										 |  |  |     if (role.code === SUPER_ADMIN_CODE) { | 
					
						
							| 
									
										
										
										
											2021-10-20 17:30:05 +02:00
										 |  |  |       throw new ApplicationError("Super admin can't be edited."); | 
					
						
							| 
									
										
										
										
											2020-07-20 11:39:26 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-25 14:59:44 +01:00
										 |  |  |     const updatedRole = await roleService.update({ id }, body); | 
					
						
							|  |  |  |     const sanitizedRole = roleService.sanitizeRole(updatedRole); | 
					
						
							| 
									
										
										
										
											2020-05-29 11:09:17 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-19 16:11:19 +02:00
										 |  |  |     ctx.body = { | 
					
						
							| 
									
										
										
										
											2020-05-29 11:09:17 +02:00
										 |  |  |       data: sanitizedRole, | 
					
						
							| 
									
										
										
										
											2020-05-19 16:11:19 +02:00
										 |  |  |     }; | 
					
						
							|  |  |  |   }, | 
					
						
							| 
									
										
										
										
											2020-05-27 17:15:58 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * Returns the permissions assigned to a role | 
					
						
							|  |  |  |    * @param {KoaContext} ctx - koa context | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   async getPermissions(ctx) { | 
					
						
							|  |  |  |     const { id } = ctx.params; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-25 14:59:44 +01:00
										 |  |  |     const roleService = getService('role'); | 
					
						
							|  |  |  |     const permissionService = getService('permission'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const role = await roleService.findOne({ id }); | 
					
						
							| 
									
										
										
										
											2020-05-27 17:15:58 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (!role) { | 
					
						
							|  |  |  |       return ctx.notFound('role.notFound'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-29 16:27:35 +02:00
										 |  |  |     const permissions = await permissionService.findMany({ where: { role: { id: role.id } } }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-25 14:59:44 +01:00
										 |  |  |     const sanitizedPermissions = permissions.map(permissionService.sanitizePermission); | 
					
						
							| 
									
										
										
										
											2020-05-27 17:15:58 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     ctx.body = { | 
					
						
							| 
									
										
										
										
											2020-07-20 17:40:01 +02:00
										 |  |  |       data: sanitizedPermissions, | 
					
						
							| 
									
										
										
										
											2020-05-27 17:15:58 +02:00
										 |  |  |     }; | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * Updates the permissions assigned to a role | 
					
						
							|  |  |  |    * @param {KoaContext} ctx - koa context | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   async updatePermissions(ctx) { | 
					
						
							|  |  |  |     const { id } = ctx.params; | 
					
						
							| 
									
										
										
										
											2021-03-25 14:59:44 +01:00
										 |  |  |     const { body: input } = ctx.request; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const { findOne, assignPermissions } = getService('role'); | 
					
						
							|  |  |  |     const { sanitizePermission, actionProvider } = getService('permission'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const role = await findOne({ id }); | 
					
						
							| 
									
										
										
										
											2020-06-23 16:31:16 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (!role) { | 
					
						
							|  |  |  |       return ctx.notFound('role.notFound'); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-05-28 11:29:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-20 17:30:05 +02:00
										 |  |  |     if (role.code === SUPER_ADMIN_CODE) { | 
					
						
							|  |  |  |       throw new ApplicationError("Super admin permissions can't be edited."); | 
					
						
							| 
									
										
										
										
											2020-05-28 11:29:59 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-05-27 17:15:58 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-20 17:30:05 +02:00
										 |  |  |     await validatedUpdatePermissionsInput(input, role); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-23 16:31:16 +02:00
										 |  |  |     let permissionsToAssign; | 
					
						
							| 
									
										
										
										
											2021-03-25 14:59:44 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-18 11:40:50 +02:00
										 |  |  |     if ([EDITOR_CODE, AUTHOR_CODE].includes(role.code)) { | 
					
						
							| 
									
										
										
										
											2022-08-08 23:33:39 +02:00
										 |  |  |       permissionsToAssign = input.permissions.map((permission) => { | 
					
						
							| 
									
										
										
										
											2021-03-25 14:59:44 +01:00
										 |  |  |         const action = actionProvider.get(permission.action); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (action.section !== 'contentTypes') { | 
					
						
							|  |  |  |           return permission; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const conditions = role.code === AUTHOR_CODE ? ['admin::is-creator'] : []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return { ...permission, conditions }; | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2020-06-23 16:31:16 +02:00
										 |  |  |     } else { | 
					
						
							|  |  |  |       permissionsToAssign = input.permissions; | 
					
						
							| 
									
										
										
										
											2020-06-15 19:11:36 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-25 14:59:44 +01:00
										 |  |  |     const permissions = await assignPermissions(role.id, permissionsToAssign); | 
					
						
							| 
									
										
										
										
											2020-05-28 11:29:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-27 17:15:58 +02:00
										 |  |  |     ctx.body = { | 
					
						
							| 
									
										
										
										
											2021-03-25 14:59:44 +01:00
										 |  |  |       data: permissions.map(sanitizePermission), | 
					
						
							| 
									
										
										
										
											2020-05-27 17:15:58 +02:00
										 |  |  |     }; | 
					
						
							|  |  |  |   }, | 
					
						
							| 
									
										
										
										
											2020-05-18 16:21:02 +02:00
										 |  |  | }; |