APIv4 - Add checkAccess action
[civicrm-core.git] / Civi / Api4 / Generic / CheckAccessAction.php
CommitLineData
929a9585
CW
1<?php
2
3/*
4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
6 | |
7 | This work is published under the GNU AGPLv3 license with some |
8 | permitted exceptions and without any warranty. For full license |
9 | and copyright information, see https://civicrm.org/licensing |
10 +--------------------------------------------------------------------+
11 */
12
13/**
14 *
15 * @package CRM
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 */
18
19
20namespace Civi\Api4\Generic;
21
22use Civi\Api4\Utils\CoreUtil;
23
24/**
25 * Check if current user is authorized to perform specified action on a given $ENTITY.
26 *
27 * @method $this setAction(string $action)
28 * @method string getAction()
29 * @method $this setValues(array $values)
30 * @method array getValues()
31 */
32class CheckAccessAction extends AbstractAction {
33
34 /**
35 * @var string
36 * @required
37 */
38 protected $action;
39
40 /**
41 * @var array
42 * @required
43 */
44 protected $values = [];
45
46 /**
47 * @param \Civi\Api4\Generic\Result $result
48 */
49 public function _run(Result $result) {
50 // Prevent circular checks
51 if ($this->action === 'checkAccess') {
52 $granted = TRUE;
53 }
54 else {
55 $granted = CoreUtil::checkAccess($this->getEntityName(), $this->action, $this->values);
56 }
57 $result->exchangeArray([['access' => $granted]]);
58 }
59
60 /**
61 * This action is always allowed
62 *
63 * @return bool
64 */
65 public function isAuthorized() {
66 return TRUE;
67 }
68
69 /**
70 * Add an item to the values array
71 * @param string $fieldName
72 * @param mixed $value
73 * @return $this
74 */
75 public function addValue(string $fieldName, $value) {
76 $this->values[$fieldName] = $value;
77 return $this;
78 }
79
80}