| 
									
										
										
										
											2020-05-28 11:29:59 +02:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-03 19:31:57 +01:00
										 |  |  | const { yup, validateYupSchema } = require('@strapi/utils'); | 
					
						
							| 
									
										
										
										
											2021-03-25 14:59:44 +01:00
										 |  |  | const { getService } = require('../utils'); | 
					
						
							| 
									
										
										
										
											2020-10-27 11:27:17 +01:00
										 |  |  | const validators = require('./common-validators'); | 
					
						
							| 
									
										
										
										
											2020-05-28 11:29:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-10 18:04:47 +02:00
										 |  |  | const checkPermissionsSchema = yup.object().shape({ | 
					
						
							|  |  |  |   permissions: yup.array().of( | 
					
						
							|  |  |  |     yup | 
					
						
							|  |  |  |       .object() | 
					
						
							|  |  |  |       .shape({ | 
					
						
							|  |  |  |         action: yup.string().required(), | 
					
						
							|  |  |  |         subject: yup.string(), | 
					
						
							|  |  |  |         field: yup.string(), | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |       .noUnknown() | 
					
						
							|  |  |  |   ), | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-10 15:42:32 +02:00
										 |  |  | // validatePermissionsExist
 | 
					
						
							| 
									
										
										
										
											2020-06-09 17:45:53 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-08 23:33:39 +02:00
										 |  |  | const checkPermissionsExist = function (permissions) { | 
					
						
							| 
									
										
										
										
											2021-03-25 14:59:44 +01:00
										 |  |  |   const existingActions = getService('permission').actionProvider.values(); | 
					
						
							| 
									
										
										
										
											2020-06-09 17:45:53 +02:00
										 |  |  |   const failIndex = permissions.findIndex( | 
					
						
							| 
									
										
										
										
											2022-08-08 23:33:39 +02:00
										 |  |  |     (permission) => | 
					
						
							| 
									
										
										
										
											2020-06-15 19:11:36 +02:00
										 |  |  |       !existingActions.some( | 
					
						
							| 
									
										
										
										
											2022-08-08 23:33:39 +02:00
										 |  |  |         (action) => | 
					
						
							| 
									
										
										
										
											2020-08-11 18:16:39 +02:00
										 |  |  |           action.actionId === permission.action && | 
					
						
							|  |  |  |           (action.section !== 'contentTypes' || action.subjects.includes(permission.subject)) | 
					
						
							| 
									
										
										
										
											2020-06-09 17:45:53 +02:00
										 |  |  |       ) | 
					
						
							|  |  |  |   ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return failIndex === -1 | 
					
						
							|  |  |  |     ? true | 
					
						
							|  |  |  |     : this.createError({ | 
					
						
							|  |  |  |         path: 'permissions', | 
					
						
							|  |  |  |         message: `[${failIndex}] is not an existing permission action`, | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const actionsExistSchema = yup | 
					
						
							|  |  |  |   .array() | 
					
						
							| 
									
										
										
										
											2020-06-15 19:11:36 +02:00
										 |  |  |   .of( | 
					
						
							|  |  |  |     yup.object().shape({ | 
					
						
							| 
									
										
										
										
											2020-07-20 17:40:01 +02:00
										 |  |  |       conditions: yup.array().of(yup.string()), | 
					
						
							| 
									
										
										
										
											2020-06-15 19:11:36 +02:00
										 |  |  |     }) | 
					
						
							|  |  |  |   ) | 
					
						
							| 
									
										
										
										
											2020-06-09 17:45:53 +02:00
										 |  |  |   .test('actions-exist', '', checkPermissionsExist); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // exports
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-28 11:29:59 +02:00
										 |  |  | module.exports = { | 
					
						
							| 
									
										
										
										
											2023-03-06 21:46:45 +01:00
										 |  |  |   validatedUpdatePermissionsInput: validateYupSchema(validators.updatePermissions), | 
					
						
							| 
									
										
										
										
											2021-11-03 19:31:57 +01:00
										 |  |  |   validatePermissionsExist: validateYupSchema(actionsExistSchema), | 
					
						
							|  |  |  |   validateCheckPermissionsInput: validateYupSchema(checkPermissionsSchema), | 
					
						
							| 
									
										
										
										
											2020-05-28 11:29:59 +02:00
										 |  |  | }; |