Added authorize-*@: support for Flex blueprints

This commit is contained in:
Matias Griese 2022-09-02 13:11:48 +03:00
parent 5d2dc6c329
commit 6882037b85
2 changed files with 37 additions and 0 deletions

View File

@ -1,3 +1,9 @@
# v1.7.36
## mm/dd/2022
1. [](#new)
* Added `authorize-*@:` support for Flex blueprints, e.g. `authorize-disabled@: not delete` disables the field if user does not have access to delete objec
# v1.7.35
## 08/04/2022

View File

@ -836,6 +836,9 @@ class FlexDirectory implements FlexDirectoryInterface
$blueprint->addDynamicHandler('flex', function (array &$field, $property, array &$call) {
$this->dynamicFlexField($field, $property, $call);
});
$blueprint->addDynamicHandler('authorize', function (array &$field, $property, array &$call) {
$this->dynamicAuthorizeField($field, $property, $call);
});
if ($context) {
$blueprint->setContext($context);
@ -921,6 +924,34 @@ class FlexDirectory implements FlexDirectoryInterface
}
}
/**
* @param array $field
* @param string $property
* @param array $call
* @return void
*/
protected function dynamicAuthorizeField(array &$field, $property, array $call): void
{
$params = (array)$call['params'];
$object = $call['object'] ?? null;
$permission = array_shift($params);
$not = false;
if (str_starts_with($permission, '!')) {
$permission = substr($permission, 1);
$not = true;
} elseif (str_starts_with($permission, 'not ')) {
$permission = substr($permission, 4);
$not = true;
}
$permission = trim($permission);
if ($object) {
$value = $object->isAuthorized($permission) ?? false;
$field[$property] = $not ? !$value : $value;
}
}
/**
* @param array $array1
* @param array $array2