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