Skip to content

Commit

Permalink
Added authorize-*@: support for Flex blueprints
Browse files Browse the repository at this point in the history
  • Loading branch information
mahagr committed Sep 2, 2022
1 parent 5d2dc6c commit 6882037
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
31 changes: 31 additions & 0 deletions system/src/Grav/Framework/Flex/FlexDirectory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 6882037

Please sign in to comment.