PluginProbe
The Innovative Form Builder – IvyForms / 1.5
The Innovative Form Builder – IvyForms v1.5
1.5.1 1.5 1.4.1 1.4 trunk 0.1.2 0.2 0.2.1 0.3 0.3.1 0.4 0.5 0.6 0.6.1 0.6.1-backup 0.6.1.1 0.7 0.8 0.8.1 0.8.2 0.9 0.9.1 1.0 1.1 1.1.1 All 27 releases
← All changes | backend/src/Routes/Notification/Notification.php +102 -30 0.9.1 → 1.5 View file →
@@ -1,23 +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 5 use IvyForms\Vendor\DI\Container;
11 6 use IvyForms\Vendor\DI\DependencyException;
12 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;
18 14 use IvyForms\Controllers\Notification\SearchNotificationsController;
19 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;
20 19 use WP_REST_Server;
21 20
22 21 class Notification
23 22 {
@@ -26,8 +25,21 @@
26 25 * @throws NotFoundException
27 26 */
28 27 public static function registerRoutes(Container $container, string $routeNamespace): void
29 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 +
30 42 register_rest_route(
31 43 $routeNamespace,
32 44 '/notification/(?P<id>\d+)',
33 45 [
@@ -32,11 +44,12 @@
32 44 '/notification/(?P<id>\d+)',
33 45 [
34 46 'methods' => WP_REST_Server::READABLE,
35 47 'callback' => $container->get(GetNotificationController::class),
36 - 'permission_callback' => function () {
37 - return current_user_can('manage_options');
38 - },
48 + 'permission_callback' => self::permissionForNotificationForm(
49 + $permissionResourceResolver,
50 + $routePermissionService
51 + ),
39 52 ]
40 53 );
41 54
42 55 register_rest_route(
@@ -44,23 +57,47 @@
44 57 '/notifications/(?P<id>\d+)',
45 58 [
46 59 'methods' => WP_REST_Server::READABLE,
47 60 'callback' => $container->get(GetNotificationsController::class),
48 - 'permission_callback' => function () {
49 - return current_user_can('manage_options');
50 - }
61 + 'permission_callback' => static function ($request) use ($routePermissionService) {
62 + $formId = RestRouteParamReader::getPositiveIntParam($request);
63 + return $routePermissionService->canEditForms($formId);
64 + },
51 65 ]
52 66 );
53 67
54 68 register_rest_route(
55 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,
56 92 '/notification/add/',
57 93 [
58 94 'methods' => WP_REST_Server::CREATABLE,
59 95 'callback' => $container->get(AddNotificationController::class),
60 - 'permission_callback' => function () {
61 - return current_user_can('manage_options');
62 - }
96 + 'permission_callback' => static function ($request) use ($routePermissionService) {
97 + $formId = RestRouteParamReader::getPositiveIntParam($request, 'formId');
98 + return $routePermissionService->canEditForms($formId);
99 + },
63 100 ]
64 101 );
65 102
66 103 register_rest_route(
@@ -68,11 +105,12 @@
68 105 '/notification/update/(?P<id>\d+)',
69 106 [
70 107 'methods' => WP_REST_Server::CREATABLE,
71 108 'callback' => $container->get(UpdateNotificationController::class),
72 - 'permission_callback' => function () {
73 - return current_user_can('manage_options');
74 - }
109 + 'permission_callback' => self::permissionForNotificationForm(
110 + $permissionResourceResolver,
111 + $routePermissionService
112 + ),
75 113 ]
76 114 );
77 115
78 116 register_rest_route(
@@ -80,13 +118,15 @@
80 118 '/notification/delete/(?P<id>\d+)',
81 119 [
82 120 'methods' => WP_REST_Server::CREATABLE,
83 121 'callback' => $container->get(DeleteNotificationController::class),
84 - 'permission_callback' => function () {
85 - return current_user_can('manage_options');
86 - }
122 + 'permission_callback' => self::permissionForNotificationForm(
123 + $permissionResourceResolver,
124 + $routePermissionService
125 + ),
87 126 ]
88 127 );
128 +
89 129 register_rest_route(
90 130 $routeNamespace,
91 131 '/notification/duplicate/(?P<id>\d+)',
92 132 [
@@ -91,24 +131,56 @@
91 131 '/notification/duplicate/(?P<id>\d+)',
92 132 [
93 133 'methods' => WP_REST_Server::CREATABLE,
94 134 'callback' => $container->get(DuplicateNotificationController::class),
95 - // TODO - Add IvyForms capability - Admin only for now
96 - 'permission_callback' => function () {
97 - return current_user_can('manage_options');
98 - }
135 + 'permission_callback' => self::permissionForNotificationForm(
136 + $permissionResourceResolver,
137 + $routePermissionService
138 + ),
99 139 ]
100 140 );
101 141
102 142 register_rest_route(
103 143 $routeNamespace,
104 - '/notifications/search',
144 + '/notifications/delete',
105 145 [
106 - 'methods' => WP_REST_Server::READABLE,
107 - 'callback' => $container->get(SearchNotificationsController::class),
108 - 'permission_callback' => function () {
109 - return current_user_can('manage_options');
110 - }
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 + ],
111 158 ]
112 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 + };
113 185 }
114 186 }