| @@ -1,22 +1,22 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace IvyForms\Routes\Notification; |
| 4 | 4 | |
| 5 | -// phpcs:disable PSR1.Files.SideEffects | |
| 6 | -if (!defined('ABSPATH')) { | |
| 7 | - exit; // Exit if accessed directly | |
| 8 | -} | |
| 9 | - | |
| 10 | -use DI\Container; | |
| 11 | -use DI\DependencyException; | |
| 12 | -use DI\NotFoundException; | |
| 5 | +use IvyForms\Vendor\DI\Container; | |
| 6 | +use IvyForms\Vendor\DI\DependencyException; | |
| 7 | +use IvyForms\Vendor\DI\NotFoundException; | |
| 13 | 8 | use IvyForms\Controllers\Notification\AddNotificationController; |
| 14 | 9 | use IvyForms\Controllers\Notification\DeleteNotificationController; |
| 10 | +use IvyForms\Controllers\Notification\DeleteNotificationsController; | |
| 15 | 11 | use IvyForms\Controllers\Notification\DuplicateNotificationController; |
| 16 | 12 | use IvyForms\Controllers\Notification\GetNotificationController; |
| 17 | 13 | use IvyForms\Controllers\Notification\GetNotificationsController; |
| 14 | +use IvyForms\Controllers\Notification\SearchNotificationsController; | |
| 18 | 15 | use IvyForms\Controllers\Notification\UpdateNotificationController; |
| 16 | +use IvyForms\Infrastructure\WP\REST\RestRouteParamReader; | |
| 17 | +use IvyForms\Services\Permissions\PermissionResourceResolver; | |
| 18 | +use IvyForms\Services\Permissions\RoutePermissionService; | |
| 19 | 19 | use WP_REST_Server; |
| 20 | 20 | |
| 21 | 21 | class Notification |
| 22 | 22 | { |
| @@ -25,8 +25,21 @@ | ||
| 25 | 25 | * @throws NotFoundException |
| 26 | 26 | */ |
| 27 | 27 | public static function registerRoutes(Container $container, string $routeNamespace): void |
| 28 | 28 | { |
| 29 | + self::registerNotificationReadRoutes($container, $routeNamespace); | |
| 30 | + self::registerNotificationWriteRoutes($container, $routeNamespace); | |
| 31 | + } | |
| 32 | + | |
| 33 | + /** | |
| 34 | + * @throws DependencyException | |
| 35 | + * @throws NotFoundException | |
| 36 | + */ | |
| 37 | + private static function registerNotificationReadRoutes(Container $container, string $routeNamespace): void | |
| 38 | + { | |
| 39 | + $routePermissionService = $container->get(RoutePermissionService::class); | |
| 40 | + $permissionResourceResolver = $container->get(PermissionResourceResolver::class); | |
| 41 | + | |
| 29 | 42 | register_rest_route( |
| 30 | 43 | $routeNamespace, |
| 31 | 44 | '/notification/(?P<id>\d+)', |
| 32 | 45 | [ |
| @@ -31,11 +44,12 @@ | ||
| 31 | 44 | '/notification/(?P<id>\d+)', |
| 32 | 45 | [ |
| 33 | 46 | 'methods' => WP_REST_Server::READABLE, |
| 34 | 47 | 'callback' => $container->get(GetNotificationController::class), |
| 35 | - 'permission_callback' => function () { | |
| 36 | - return current_user_can('manage_options'); | |
| 37 | - }, | |
| 48 | + 'permission_callback' => self::permissionForNotificationForm( | |
| 49 | + $permissionResourceResolver, | |
| 50 | + $routePermissionService | |
| 51 | + ), | |
| 38 | 52 | ] |
| 39 | 53 | ); |
| 40 | 54 | |
| 41 | 55 | register_rest_route( |
| @@ -43,23 +57,47 @@ | ||
| 43 | 57 | '/notifications/(?P<id>\d+)', |
| 44 | 58 | [ |
| 45 | 59 | 'methods' => WP_REST_Server::READABLE, |
| 46 | 60 | 'callback' => $container->get(GetNotificationsController::class), |
| 47 | - 'permission_callback' => function () { | |
| 48 | - return current_user_can('manage_options'); | |
| 49 | - } | |
| 61 | + 'permission_callback' => static function ($request) use ($routePermissionService) { | |
| 62 | + $formId = RestRouteParamReader::getPositiveIntParam($request); | |
| 63 | + return $routePermissionService->canEditForms($formId); | |
| 64 | + }, | |
| 50 | 65 | ] |
| 51 | 66 | ); |
| 52 | 67 | |
| 53 | 68 | register_rest_route( |
| 54 | 69 | $routeNamespace, |
| 70 | + '/notifications/search', | |
| 71 | + [ | |
| 72 | + 'methods' => WP_REST_Server::READABLE, | |
| 73 | + 'callback' => $container->get(SearchNotificationsController::class), | |
| 74 | + 'permission_callback' => static function () use ($routePermissionService) { | |
| 75 | + return $routePermissionService->canEditForms(); | |
| 76 | + }, | |
| 77 | + ] | |
| 78 | + ); | |
| 79 | + } | |
| 80 | + | |
| 81 | + /** | |
| 82 | + * @throws DependencyException | |
| 83 | + * @throws NotFoundException | |
| 84 | + */ | |
| 85 | + private static function registerNotificationWriteRoutes(Container $container, string $routeNamespace): void | |
| 86 | + { | |
| 87 | + $routePermissionService = $container->get(RoutePermissionService::class); | |
| 88 | + $permissionResourceResolver = $container->get(PermissionResourceResolver::class); | |
| 89 | + | |
| 90 | + register_rest_route( | |
| 91 | + $routeNamespace, | |
| 55 | 92 | '/notification/add/', |
| 56 | 93 | [ |
| 57 | 94 | 'methods' => WP_REST_Server::CREATABLE, |
| 58 | 95 | 'callback' => $container->get(AddNotificationController::class), |
| 59 | - 'permission_callback' => function () { | |
| 60 | - return current_user_can('manage_options'); | |
| 61 | - } | |
| 96 | + 'permission_callback' => static function ($request) use ($routePermissionService) { | |
| 97 | + $formId = RestRouteParamReader::getPositiveIntParam($request, 'formId'); | |
| 98 | + return $routePermissionService->canEditForms($formId); | |
| 99 | + }, | |
| 62 | 100 | ] |
| 63 | 101 | ); |
| 64 | 102 | |
| 65 | 103 | register_rest_route( |
| @@ -67,11 +105,12 @@ | ||
| 67 | 105 | '/notification/update/(?P<id>\d+)', |
| 68 | 106 | [ |
| 69 | 107 | 'methods' => WP_REST_Server::CREATABLE, |
| 70 | 108 | 'callback' => $container->get(UpdateNotificationController::class), |
| 71 | - 'permission_callback' => function () { | |
| 72 | - return current_user_can('manage_options'); | |
| 73 | - } | |
| 109 | + 'permission_callback' => self::permissionForNotificationForm( | |
| 110 | + $permissionResourceResolver, | |
| 111 | + $routePermissionService | |
| 112 | + ), | |
| 74 | 113 | ] |
| 75 | 114 | ); |
| 76 | 115 | |
| 77 | 116 | register_rest_route( |
| @@ -79,13 +118,15 @@ | ||
| 79 | 118 | '/notification/delete/(?P<id>\d+)', |
| 80 | 119 | [ |
| 81 | 120 | 'methods' => WP_REST_Server::CREATABLE, |
| 82 | 121 | 'callback' => $container->get(DeleteNotificationController::class), |
| 83 | - 'permission_callback' => function () { | |
| 84 | - return current_user_can('manage_options'); | |
| 85 | - } | |
| 122 | + 'permission_callback' => self::permissionForNotificationForm( | |
| 123 | + $permissionResourceResolver, | |
| 124 | + $routePermissionService | |
| 125 | + ), | |
| 86 | 126 | ] |
| 87 | 127 | ); |
| 128 | + | |
| 88 | 129 | register_rest_route( |
| 89 | 130 | $routeNamespace, |
| 90 | 131 | '/notification/duplicate/(?P<id>\d+)', |
| 91 | 132 | [ |
| @@ -90,12 +131,56 @@ | ||
| 90 | 131 | '/notification/duplicate/(?P<id>\d+)', |
| 91 | 132 | [ |
| 92 | 133 | 'methods' => WP_REST_Server::CREATABLE, |
| 93 | 134 | 'callback' => $container->get(DuplicateNotificationController::class), |
| 94 | - // TODO - Add IvyForms capability - Admin only for now | |
| 95 | - 'permission_callback' => function () { | |
| 96 | - return current_user_can('manage_options'); | |
| 97 | - } | |
| 135 | + 'permission_callback' => self::permissionForNotificationForm( | |
| 136 | + $permissionResourceResolver, | |
| 137 | + $routePermissionService | |
| 138 | + ), | |
| 98 | 139 | ] |
| 99 | 140 | ); |
| 141 | + | |
| 142 | + register_rest_route( | |
| 143 | + $routeNamespace, | |
| 144 | + '/notifications/delete', | |
| 145 | + [ | |
| 146 | + 'methods' => WP_REST_Server::CREATABLE, | |
| 147 | + 'callback' => $container->get(DeleteNotificationsController::class), | |
| 148 | + 'permission_callback' => static function () use ($routePermissionService) { | |
| 149 | + return $routePermissionService->canEditForms(); | |
| 150 | + }, | |
| 151 | + 'args' => [ | |
| 152 | + 'ids' => [ | |
| 153 | + 'required' => true, | |
| 154 | + 'type' => 'array', | |
| 155 | + 'items' => ['type' => 'integer'], | |
| 156 | + ], | |
| 157 | + ], | |
| 158 | + ] | |
| 159 | + ); | |
| 160 | + } | |
| 161 | + | |
| 162 | + /** | |
| 163 | + * @return callable | |
| 164 | + */ | |
| 165 | + private static function permissionForNotificationForm( | |
| 166 | + PermissionResourceResolver $permissionResourceResolver, | |
| 167 | + RoutePermissionService $routePermissionService | |
| 168 | + ): callable { | |
| 169 | + return static function ($request) use ( | |
| 170 | + $permissionResourceResolver, | |
| 171 | + $routePermissionService | |
| 172 | + ): bool { | |
| 173 | + $notificationId = RestRouteParamReader::getPositiveIntParam($request); | |
| 174 | + if ($notificationId === null) { | |
| 175 | + return false; | |
| 176 | + } | |
| 177 | + | |
| 178 | + $formId = $permissionResourceResolver->formIdFromNotificationId($notificationId); | |
| 179 | + if ($formId === null) { | |
| 180 | + return false; | |
| 181 | + } | |
| 182 | + | |
| 183 | + return $routePermissionService->canEditForms($formId); | |
| 184 | + }; | |
| 100 | 185 | } |
| 101 | 186 | } |