Skip to content
Snippets Groups Projects
Commit 1d43096e authored by Groß-Vogt, Tobias's avatar Groß-Vogt, Tobias
Browse files

#39832 renamed 'rights' to 'roles'; optional alias for autorization 'object'

parent 49ded510
No related branches found
No related tags found
No related merge requests found
Pipeline #231021 passed
...@@ -47,23 +47,23 @@ abstract class AbstractAuthorizationService ...@@ -47,23 +47,23 @@ abstract class AbstractAuthorizationService
} }
/** /**
* @param mixed $subject * @param mixed $object
* *
* @throws ApiError * @throws ApiError
*/ */
public function denyAccessUnlessIsGranted(string $rightName, $subject = null): void public function denyAccessUnlessIsGranted(string $rightName, $object = null, string $objectAlias = null): void
{ {
if ($this->isGrantedInternal($rightName, $subject) === false) { if ($this->isGrantedInternal($rightName, $object, $objectAlias) === false) {
throw new ApiError(Response::HTTP_FORBIDDEN, 'access denied. missing right '.$rightName); throw new ApiError(Response::HTTP_FORBIDDEN, 'access denied. missing right '.$rightName);
} }
} }
/** /**
* @param mixed $subject * @param mixed $object
*/ */
public function isGranted(string $expressionName, $subject = null): bool public function isGranted(string $expressionName, $object = null, string $objectAlias = null): bool
{ {
return $this->isGrantedInternal($expressionName, $subject); return $this->isGrantedInternal($expressionName, $object, $objectAlias);
} }
/** /**
...@@ -84,9 +84,9 @@ abstract class AbstractAuthorizationService ...@@ -84,9 +84,9 @@ abstract class AbstractAuthorizationService
/** /**
* @throws AuthorizationException * @throws AuthorizationException
*/ */
private function isGrantedInternal(string $rightName, $subject = null): bool private function isGrantedInternal(string $rightName, $object, string $objectAlias = null): bool
{ {
return $this->userAuthorizationChecker->isGranted($this->currentAuthorizationUser, $rightName, $subject); return $this->userAuthorizationChecker->isGranted($this->currentAuthorizationUser, $rightName, $object, $objectAlias);
} }
/** /**
...@@ -101,7 +101,7 @@ abstract class AbstractAuthorizationService ...@@ -101,7 +101,7 @@ abstract class AbstractAuthorizationService
{ {
$treeBuilder = new TreeBuilder(self::AUTHORIZATION_ROOT_CONFIG_NODE); $treeBuilder = new TreeBuilder(self::AUTHORIZATION_ROOT_CONFIG_NODE);
$rightsNodeChildBuilder = $treeBuilder->getRootNode()->children()->arrayNode(AuthorizationExpressionChecker::RIGHTS_CONFIG_NODE) $rightsNodeChildBuilder = $treeBuilder->getRootNode()->children()->arrayNode(AuthorizationExpressionChecker::ROLES_CONFIG_NODE)
->addDefaultsIfNotSet() ->addDefaultsIfNotSet()
->children(); ->children();
foreach ($rights as $right) { foreach ($rights as $right) {
...@@ -128,7 +128,7 @@ abstract class AbstractAuthorizationService ...@@ -128,7 +128,7 @@ abstract class AbstractAuthorizationService
{ {
return [ return [
AbstractAuthorizationService::AUTHORIZATION_ROOT_CONFIG_NODE => [ AbstractAuthorizationService::AUTHORIZATION_ROOT_CONFIG_NODE => [
AuthorizationExpressionChecker::RIGHTS_CONFIG_NODE => $rightExpressions, AuthorizationExpressionChecker::ROLES_CONFIG_NODE => $rightExpressions,
AuthorizationExpressionChecker::ATTRIBUTES_CONFIG_NODE => $attributeExpressions, AuthorizationExpressionChecker::ATTRIBUTES_CONFIG_NODE => $attributeExpressions,
], ],
]; ];
......
...@@ -11,10 +11,11 @@ use Dbp\Relay\CoreBundle\ExpressionLanguage\ExpressionLanguage; ...@@ -11,10 +11,11 @@ use Dbp\Relay\CoreBundle\ExpressionLanguage\ExpressionLanguage;
*/ */
class AuthorizationExpressionChecker class AuthorizationExpressionChecker
{ {
public const RIGHTS_CONFIG_NODE = 'rights'; public const ROLES_CONFIG_NODE = 'roles';
public const ATTRIBUTES_CONFIG_NODE = 'attributes'; public const ATTRIBUTES_CONFIG_NODE = 'attributes';
private const MAX_NUM_CALLS = 16; private const USER_VARIBLE_NAME = 'user';
private const DEFAULT_OBJECT_VARIBLE_NAME = 'object';
/** @var ExpressionLanguage */ /** @var ExpressionLanguage */
private $expressionLanguage; private $expressionLanguage;
...@@ -46,7 +47,7 @@ class AuthorizationExpressionChecker ...@@ -46,7 +47,7 @@ class AuthorizationExpressionChecker
public function setConfig(array $config) public function setConfig(array $config)
{ {
$this->loadExpressions($config[self::RIGHTS_CONFIG_NODE] ?? [], $this->rightExpressions); $this->loadExpressions($config[self::ROLES_CONFIG_NODE] ?? [], $this->rightExpressions);
$this->loadExpressions($config[self::ATTRIBUTES_CONFIG_NODE] ?? [], $this->attributeExpressions); $this->loadExpressions($config[self::ATTRIBUTES_CONFIG_NODE] ?? [], $this->attributeExpressions);
} }
...@@ -95,7 +96,7 @@ class AuthorizationExpressionChecker ...@@ -95,7 +96,7 @@ class AuthorizationExpressionChecker
* *
* @throws AuthorizationException * @throws AuthorizationException
*/ */
public function isGranted(AuthorizationUser $currentAuthorizationUser, string $rightName, $subject): bool public function isGranted(AuthorizationUser $currentAuthorizationUser, string $rightName, $object, string $objectAlias = null): bool
{ {
if (in_array($rightName, $this->rightExpressionStack, true)) { if (in_array($rightName, $this->rightExpressionStack, true)) {
throw new AuthorizationException(sprintf('infinite loop caused by authorization right expression %s detected', $rightName), AuthorizationException::INFINITE_EXRPESSION_LOOP_DETECTED); throw new AuthorizationException(sprintf('infinite loop caused by authorization right expression %s detected', $rightName), AuthorizationException::INFINITE_EXRPESSION_LOOP_DETECTED);
...@@ -109,8 +110,8 @@ class AuthorizationExpressionChecker ...@@ -109,8 +110,8 @@ class AuthorizationExpressionChecker
} }
return $this->expressionLanguage->evaluate($rightExpression, [ return $this->expressionLanguage->evaluate($rightExpression, [
'user' => $currentAuthorizationUser, self::USER_VARIBLE_NAME => $currentAuthorizationUser,
'subject' => $subject, $objectAlias ?? self::DEFAULT_OBJECT_VARIBLE_NAME => $object,
]); ]);
} finally { } finally {
array_pop($this->rightExpressionStack); array_pop($this->rightExpressionStack);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment