← All changes
|
backend/src/Controllers/Entry/DeleteEntriesController.php
+34
-3
1.0
→
1.5
View file →
| @@ -12,8 +12,10 @@ | ||
| 12 | 12 | use IvyForms\Common\Exceptions\NotFoundException; |
| 13 | 13 | use IvyForms\Common\Sanitizer\Sanitizer; |
| 14 | 14 | use IvyForms\Controllers\Controller; |
| 15 | 15 | use IvyForms\Services\Entry\EntryService; |
| 16 | +use IvyForms\Services\Permissions\PermissionResourceResolver; | |
| 17 | +use IvyForms\Services\Permissions\RoutePermissionService; | |
| 16 | 18 | use IvyForms\Services\Translations\BackendStrings; |
| 17 | 19 | use WP_REST_Request; |
| 18 | 20 | use WP_REST_Response; |
| 19 | 21 | |
| @@ -20,12 +22,20 @@ | ||
| 20 | 22 | class DeleteEntriesController extends Controller |
| 21 | 23 | { |
| 22 | 24 | private EntryService $entryService; |
| 23 | 25 | |
| 26 | + private RoutePermissionService $routePermissionService; | |
| 27 | + | |
| 28 | + private PermissionResourceResolver $permissionResourceResolver; | |
| 29 | + | |
| 24 | 30 | public function __construct( |
| 25 | - EntryService $entryService | |
| 31 | + EntryService $entryService, | |
| 32 | + RoutePermissionService $routePermissionService, | |
| 33 | + PermissionResourceResolver $permissionResourceResolver | |
| 26 | 34 | ) { |
| 27 | - $this->entryService = $entryService; | |
| 35 | + $this->entryService = $entryService; | |
| 36 | + $this->routePermissionService = $routePermissionService; | |
| 37 | + $this->permissionResourceResolver = $permissionResourceResolver; | |
| 28 | 38 | } |
| 29 | 39 | |
| 30 | 40 | /** |
| 31 | 41 | * @param WP_REST_Request $data |
| @@ -46,9 +56,30 @@ | ||
| 46 | 56 | BackendStrings::getExceptionStrings()['invalid_request_data'] |
| 47 | 57 | ); |
| 48 | 58 | } |
| 49 | 59 | |
| 50 | - $ids = Sanitizer::sanitizeIds($data->get_param('ids')) ; | |
| 60 | + $ids = Sanitizer::sanitizeIds($data->get_param('ids')); | |
| 61 | + if ($ids === []) { | |
| 62 | + return new WP_REST_Response([ | |
| 63 | + 'success' => false, | |
| 64 | + 'message' => BackendStrings::getExceptionStrings()['invalid_or_missing_ids'], | |
| 65 | + ], 400); | |
| 66 | + } | |
| 67 | + | |
| 68 | + $formIdsByEntryId = $this->permissionResourceResolver->formIdsFromEntryIds($ids); | |
| 69 | + | |
| 70 | + foreach ($ids as $entryId) { | |
| 71 | + $id = (int) $entryId; | |
| 72 | + if (!isset($formIdsByEntryId[$id])) { | |
| 73 | + throw new ForbiddenException('forbidden'); | |
| 74 | + } | |
| 75 | + } | |
| 76 | + | |
| 77 | + foreach (array_unique(array_values($formIdsByEntryId)) as $formId) { | |
| 78 | + if (!$this->routePermissionService->canDeleteEntries($formId)) { | |
| 79 | + throw new ForbiddenException('forbidden'); | |
| 80 | + } | |
| 81 | + } | |
| 51 | 82 | |
| 52 | 83 | // Bulk delete all entry fields for these entries |
| 53 | 84 | $this->entryService->getDeletionManager()->deleteEntryFieldsByEntryIds($ids); |
| 54 | 85 | // Bulk delete all entries |