diff --git a/CHANGELOG.md b/CHANGELOG.md index 0958fe451..56f93123b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/system/src/Grav/Framework/Flex/FlexDirectory.php b/system/src/Grav/Framework/Flex/FlexDirectory.php index dce8bf523..5368ebce0 100644 --- a/system/src/Grav/Framework/Flex/FlexDirectory.php +++ b/system/src/Grav/Framework/Flex/FlexDirectory.php @@ -836,6 +836,9 @@ protected function getBlueprintInternal(string $type_view = '', string $context $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 @@ protected function dynamicFlexField(array &$field, $property, array $call): void } } + /** + * @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