| 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 | namespace Civi\Api4\Generic; |
| 14 | |
| 15 | use Civi\Api4\Utils\CoreUtil; |
| 16 | |
| 17 | /** |
| 18 | * Check if current user is authorized to perform specified action on a given $ENTITY. |
| 19 | * |
| 20 | * @method $this setAction(string $action) |
| 21 | * @method string getAction() |
| 22 | * @method $this setValues(array $values) |
| 23 | * @method array getValues() |
| 24 | */ |
| 25 | class CheckAccessAction extends AbstractAction { |
| 26 | |
| 27 | /** |
| 28 | * @var string |
| 29 | * @required |
| 30 | */ |
| 31 | protected $action; |
| 32 | |
| 33 | /** |
| 34 | * @var array |
| 35 | * @required |
| 36 | */ |
| 37 | protected $values = []; |
| 38 | |
| 39 | /** |
| 40 | * @param \Civi\Api4\Generic\Result $result |
| 41 | */ |
| 42 | public function _run(Result $result) { |
| 43 | // Prevent circular checks |
| 44 | if ($this->action === 'checkAccess') { |
| 45 | $granted = TRUE; |
| 46 | } |
| 47 | else { |
| 48 | $granted = CoreUtil::checkAccessDelegated($this->getEntityName(), $this->action, $this->values, \CRM_Core_Session::getLoggedInContactID() ?: 0); |
| 49 | } |
| 50 | $result->exchangeArray([['access' => $granted]]); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * This action is always allowed |
| 55 | * |
| 56 | * @return bool |
| 57 | */ |
| 58 | public function isAuthorized(): bool { |
| 59 | return TRUE; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Add an item to the values array |
| 64 | * @param string $fieldName |
| 65 | * @param mixed $value |
| 66 | * @return $this |
| 67 | */ |
| 68 | public function addValue(string $fieldName, $value) { |
| 69 | $this->values[$fieldName] = $value; |
| 70 | return $this; |
| 71 | } |
| 72 | |
| 73 | } |