PluginProbe ʕ •ᴥ•ʔ
Advanced Access Manager – Access Governance for WordPress / trunk
Advanced Access Manager – Access Governance for WordPress vtrunk
6.8.4 6.8.5 6.9.0 6.9.1 6.9.10 6.9.11 6.9.12 6.9.13 6.9.14 6.9.15 6.9.16 6.9.17 6.9.18 6.9.19 6.9.2 6.9.20 6.9.21 6.9.22 6.9.23 6.9.24 6.9.25 6.9.26 6.9.27 6.9.28 6.9.29 6.9.3 6.9.30 6.9.31 6.9.32 6.9.33 6.9.34 6.9.35 6.9.36 6.9.37 6.9.38 6.9.39 6.9.4 6.9.41 6.9.42 6.9.43 6.9.44 6.9.45 6.9.46 6.9.47 6.9.48 6.9.49 6.9.5 6.9.51 6.9.6 6.9.7 6.9.8 6.9.9 7.0.0 7.0.0-alpha.6 7.0.0-alpha.7 7.0.0-beta.1 7.0.0-rc1 7.0.0-rc2 7.0.0-rc3 7.0.1 7.0.10 7.0.11 7.0.2 7.0.3 7.0.4 7.0.5 7.0.6 7.0.7 7.0.8 7.0.9 7.1.0 7.1.1 trunk 3.0 4.0 4.0.1 4.1 4.2 4.3 4.4 4.4.1 4.5 4.6 4.6.1 4.6.2 4.7 4.7.1 4.7.2 4.7.5 4.7.6 4.8 4.8.1 4.9 4.9.1 4.9.2 4.9.3 4.9.4 4.9.5 4.9.5.1 4.9.5.2 5.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1 5.1.1 5.10 5.11 5.2 5.2.1 5.2.5 5.2.6 5.2.7 5.3 5.3.1 5.3.2 5.3.3 5.3.4 5.3.5 5.4 5.4.1 5.4.2 5.4.3 5.4.3.1 5.4.3.2 5.5 5.5.1 5.5.2 5.6 5.6.1 5.6.1.1 5.7 5.7.1 5.7.2 5.7.3 5.8 5.8.1 5.8.2 5.8.3 5.9 5.9.1 5.9.1.1 5.9.2 5.9.2.1 5.9.3 5.9.4 5.9.5 5.9.6 5.9.6.1 5.9.6.2 5.9.6.3 5.9.7 5.9.7.1 5.9.7.2 5.9.7.3 5.9.8 5.9.8.1 5.9.9 5.9.9.1 6.0.0 6.0.1 6.0.2 6.0.3 6.0.4 6.0.5 6.1.0 6.1.1 6.2.0 6.2.1 6.2.2 6.3.0 6.3.1 6.3.2 6.3.3 6.4.0 6.4.1 6.4.2 6.4.3 6.5.0 6.5.1 6.5.2 6.5.3 6.5.4 6.6.0 6.6.1 6.6.2 6.6.3 6.6.4 6.7.0 6.7.1 6.7.2 6.7.3 6.7.4 6.7.5 6.7.6 6.7.7 6.7.8 6.7.9 6.8.0 6.8.1 6.8.2 6.8.3
advanced-access-manager / application / Restful / Policies.php
advanced-access-manager / application / Restful Last commit date
AccessDeniedRedirect.php 1 year ago AdminToolbar.php 1 year ago ApiRoute.php 1 year ago BackendMenu.php 1 year ago BackwardCompatibility.php 1 year ago Capability.php 1 year ago Configs.php 5 months ago Content.php 1 year ago Identity.php 1 year ago Jwt.php 5 months ago LoginRedirect.php 1 year ago LogoutRedirect.php 1 year ago Metabox.php 1 year ago Mu.php 1 year ago NotFoundRedirect.php 1 year ago Policies.php 1 year ago Roles.php 1 year ago SecureLogin.php 1 year ago SecurityAudit.php 1 year ago ServiceTrait.php 1 year ago Settings.php 1 year ago Urls.php 1 year ago Users.php 1 year ago Widgets.php 1 year ago
Policies.php
432 lines
1 <?php
2
3 /**
4 * ======================================================================
5 * LICENSE: This file is subject to the terms and conditions defined in *
6 * file 'license.txt', which is part of this source code package. *
7 * ======================================================================
8 */
9
10 /**
11 * RESTful API for the Access Policies service
12 *
13 * @package AAM
14 * @version 7.0.0
15 */
16 class AAM_Restful_Policies
17 {
18
19 use AAM_Restful_ServiceTrait;
20
21 /**
22 * Necessary permissions to access endpoint
23 *
24 * @version 7.0.0
25 */
26 const PERMISSIONS = [
27 'aam_manager',
28 'aam_manage_policies'
29 ];
30
31 /**
32 * Constructor
33 *
34 * @return void
35 * @access protected
36 *
37 * @version 7.0.0
38 */
39 protected function __construct()
40 {
41 // Register API endpoint
42 add_action('rest_api_init', function() {
43 // Get the list of all policies
44 $this->_register_route('/policies', [
45 'methods' => WP_REST_Server::READABLE,
46 'callback' => [ $this, 'get_policies' ],
47 'args' => array(
48 'fields' => array(
49 'description' => 'List of additional fields to return',
50 'type' => 'string',
51 'validate_callback' => function ($value) {
52 return $this->_validate_fields_input($value);
53 }
54 )
55 )
56 ], self::PERMISSIONS);
57
58 // Get a single policy
59 $this->_register_route('/policy/(?P<id>[\d]+)', [
60 'methods' => WP_REST_Server::READABLE,
61 'callback' => [ $this, 'get_policy' ],
62 'args' => [
63 'id' => [
64 'description' => 'Policy ID',
65 'type' => 'number',
66 'required' => true
67 ],
68 'fields' => array(
69 'description' => 'List of additional fields to return',
70 'type' => 'string',
71 'validate_callback' => function ($value) {
72 return $this->_validate_fields_input($value);
73 }
74 )
75 ]
76 ], self::PERMISSIONS);
77
78 // Create new policy
79 $this->_register_route('/policies', [
80 'methods' => WP_REST_Server::CREATABLE,
81 'callback' => [ $this, 'create_policy' ],
82 'args' => [
83 'policy' => [
84 'description' => 'JSON policy',
85 'type' => 'string',
86 'required' => true
87 ],
88 'title' => [
89 'description' => 'Policy title',
90 'type' => 'string',
91 'required' => false
92 ],
93 'excerpt' => [
94 'description' => 'Policy short description',
95 'type' => 'string',
96 'required' => false
97 ],
98 'effect' => [
99 'description' => 'Attach or detach policy',
100 'type' => 'string',
101 'required' => false,
102 'default' => 'attach',
103 'enum' => [ 'attach', 'detach' ]
104 ],
105 'status' => [
106 'description' => 'Policy status',
107 'type' => 'string',
108 'required' => false,
109 'default' => 'publish'
110 ],
111 'fields' => array(
112 'description' => 'List of additional fields to return',
113 'type' => 'string',
114 'validate_callback' => function ($value) {
115 return $this->_validate_fields_input($value);
116 }
117 )
118 ]
119 ], self::PERMISSIONS);
120
121 // Attach/detach policy
122 $this->_register_route('/policy/(?P<id>[\d]+)', [
123 'methods' => WP_REST_Server::EDITABLE,
124 'callback' => [ $this, 'toggle_policy' ],
125 'args' => [
126 'id' => [
127 'description' => 'Policy ID',
128 'type' => 'number',
129 'required' => true
130 ],
131 'effect' => [
132 'description' => 'Attach or detach policy',
133 'type' => 'string',
134 'required' => true,
135 'enum' => [ 'attach', 'detach' ]
136 ],
137 'fields' => array(
138 'description' => 'List of additional fields to return',
139 'type' => 'string',
140 'validate_callback' => function ($value) {
141 return $this->_validate_fields_input($value);
142 }
143 )
144 ]
145 ], self::PERMISSIONS);
146
147 // Delete policy
148 $this->_register_route('/policy/(?P<id>[\d]+)', [
149 'methods' => WP_REST_Server::DELETABLE,
150 'callback' => [ $this, 'delete_policy' ],
151 'args' => [
152 'id' => [
153 'description' => 'Policy ID',
154 'type' => 'number',
155 'required' => true
156 ]
157 ]
158 ], self::PERMISSIONS);
159
160 // Reset policy settings
161 $this->_register_route('/policies', [
162 'methods' => WP_REST_Server::DELETABLE,
163 'callback' => [ $this, 'reset_policies' ]
164 ], self::PERMISSIONS);
165 });
166 }
167
168 /**
169 * Get list of all registered policies
170 *
171 * @param WP_REST_Request $request
172 *
173 * @return WP_REST_Response
174 * @access public
175 *
176 * @version 7.0.0
177 */
178 public function get_policies(WP_REST_Request $request)
179 {
180 try {
181 $result = [];
182
183 foreach($this->_get_service($request)->policies() as $policy) {
184 array_push($result, $this->_prepare_policy_item($policy, $request));
185 }
186 } catch (Exception $e) {
187 $result = $this->_prepare_error_response($e);
188 }
189
190 return rest_ensure_response($result);
191 }
192
193 /**
194 * Toggle registered policy
195 *
196 * @param WP_REST_Request $request
197 *
198 * @return WP_REST_Response
199 * @access public
200 *
201 * @version 7.0.0
202 */
203 public function toggle_policy(WP_REST_Request $request)
204 {
205 try {
206 $policy_id = intval($request->get_param('id'));
207 $effect = $request->get_param('effect');
208
209 if ($effect === 'attach') {
210 $this->_get_service($request)->attach($policy_id);
211 } else {
212 $this->_get_service($request)->detach($policy_id);
213 }
214
215 $result = $this->_prepare_policy_item(
216 $this->_get_service($request)->policy($policy_id), $request
217 );
218 } catch (Exception $e) {
219 $result = $this->_prepare_error_response($e);
220 }
221
222 return rest_ensure_response($result);
223 }
224
225 /**
226 * Create new policy
227 *
228 * @param WP_REST_Request $request
229 *
230 * @return WP_REST_Response
231 * @access public
232 *
233 * @version 7.0.0
234 */
235 public function create_policy(WP_REST_Request $request)
236 {
237 try {
238 $service = $this->_get_service($request);
239
240 $policy_id = $service->create(
241 [
242 'json' => $request->get_param('policy'),
243 'title' => $request->get_param('title'),
244 'excerpt' => $request->get_param('excerpt')
245 ],
246 $request->get_param('status'),
247 $request->get_param('effect')
248 );
249
250 $result = $this->_prepare_policy_item(
251 $service->policy($policy_id), $request
252 );
253 } catch (Exception $e) {
254 $result = $this->_prepare_error_response($e);
255 }
256
257 return rest_ensure_response($result);
258 }
259
260 /**
261 * Get a single registered policy
262 *
263 * @param WP_REST_Request $request
264 *
265 * @return WP_REST_Response
266 * @access public
267 *
268 * @version 7.0.0
269 */
270 public function get_policy(WP_REST_Request $request)
271 {
272 try {
273 $policy = $this->_get_service($request)->policy(
274 intval($request->get_param('id'))
275 );
276
277 $result = $this->_prepare_policy_item($policy, $request);
278 } catch (Exception $e) {
279 $result = $this->_prepare_error_response($e);
280 }
281
282 return rest_ensure_response($result);
283 }
284
285 /**
286 * Delete a policy
287 *
288 * @param WP_REST_Request $request
289 *
290 * @return WP_REST_Response
291 * @access public
292 *
293 * @version 7.0.0
294 */
295 public function delete_policy(WP_REST_Request $request)
296 {
297 try {
298 $policy_id = intval($request->get_param('id'));
299
300 if (current_user_can('aam_delete_policy', $policy_id)) {
301 wp_delete_post($policy_id);
302 }
303
304 $result = [ 'success' => true ];
305 } catch (Exception $e) {
306 $result = $this->_prepare_error_response($e);
307 }
308
309 return rest_ensure_response($result);
310 }
311
312 /**
313 * Reset policy settings
314 *
315 * @param WP_REST_Request $request
316 *
317 * @return WP_REST_Response
318 * @access public
319 *
320 * @version 7.0.0
321 */
322 public function reset_policies(WP_REST_Request $request)
323 {
324 try {
325 $result = [ 'success' => $this->_get_service($request)->reset() ];
326 } catch (Exception $e) {
327 $result = $this->_prepare_error_response($e);
328 }
329
330 return rest_ensure_response($result);
331 }
332
333 /**
334 * Prepare policy item output
335 *
336 * @param array $policy
337 * @param WP_REST_Request $request
338 *
339 * @return array
340 * @access private
341 *
342 * @version 7.0.0
343 */
344 private function _prepare_policy_item($policy, $request)
345 {
346 // Determine list of fields to return
347 $fields = $request->get_param('fields');
348 $fields = empty($fields) ? [] : wp_parse_list($fields);
349 $fields = array_unique(array_merge(
350 [ 'id', 'title', 'status', 'is_attached' ], $fields
351 ));
352
353 // Determine the list of permissions
354 $permissions = [
355 'toggle_policy'
356 ];
357
358 if (current_user_can('aam_edit_policy', $policy['id'])) {
359 array_push($permissions, 'edit_policy');
360 }
361
362 if (current_user_can('aam_delete_policy', $policy['id'])) {
363 array_push($permissions, 'delete_policy');
364 }
365
366 return array_filter([
367 'id' => $policy['id'],
368 'title' => $policy['ref']->post_title,
369 'status' => $policy['status'],
370 'excerpt' => $policy['ref']->post_excerpt,
371 'json' => $policy['json'],
372 'is_attached' => $policy['is_attached'],
373 'permissions' => $permissions
374 ], function($k) use ($fields) {
375 return in_array($k, $fields, true);
376 }, ARRAY_FILTER_USE_KEY);
377 }
378
379 /**
380 * Get Policies framework service
381 *
382 * @param WP_REST_Request $request
383 *
384 * @return AAM_Framework_Service_Policies
385 * @access private
386 *
387 * @version 7.0.0
388 */
389 private function _get_service($request)
390 {
391 return AAM::api()->policies(
392 $this->_determine_access_level($request),
393 [ 'error_handling' => 'exception' ]
394 );
395 }
396
397 /**
398 * Validate the input field "fields"
399 *
400 * @param string|null $value Input value
401 *
402 * @return bool|WP_Error
403 * @access private
404 *
405 * @version 7.0.0
406 */
407 private function _validate_fields_input($value)
408 {
409 $response = true;
410
411 if (is_string($value) && strlen($value) > 0) {
412 $invalid_fields = [];
413
414 foreach(explode(',', $value) as $field) {
415 if (strlen(sanitize_key($field)) !== strlen($field)) {
416 $invalid_fields[] = $field;
417 }
418 }
419
420 if (count($invalid_fields) > 0) {
421 $response = new WP_Error(
422 'rest_invalid_param',
423 sprintf('Invalid fields: %s', implode(', ', $invalid_fields)),
424 array('status' => 400)
425 );
426 }
427 }
428
429 return $response;
430 }
431
432 }