Skip to content
Snippets Groups Projects
Commit 4ee2696e authored by Reiter, Christoph's avatar Reiter, Christoph :snake:
Browse files

auth: rename AuthorizationService -> AbstractAuthorizationService

To be in line with the naming schema of symfony.
Add a deprecated dummy class to make the transition easier
parent 674c84b6
No related branches found
No related tags found
No related merge requests found
Pipeline #198661 passed
<?php
declare(strict_types=1);
namespace Dbp\Relay\CoreBundle\Authorization;
use Dbp\Relay\CoreBundle\API\UserSessionInterface;
use Dbp\Relay\CoreBundle\Exception\ApiError;
use Symfony\Component\HttpFoundation\Response;
abstract class AbstractAuthorizationService
{
public const AUTHORIZATION_CONFIG_ATTRIBUTE = 'authorization';
public const RIGHTS_CONFIG_ATTRIBUTE = 'rights';
public const ATTRIBUTES_CONFIG_ATTRIBUTE = 'attributes';
public const NAME_CONFIG_ATTRIBUTE = 'name';
public const EXPRESSION_CONFIG_ATTRIBUTE = 'expression';
/** @var UserAuthorizationChecker */
private $userAuthorizationChecker;
/** @var AuthorizationUser|null */
private $currentAuthorizationUser;
public function __construct(UserSessionInterface $userSession, AuthorizationDataProviderProvider $authorizationDataProviderProvider)
{
$this->userAuthorizationChecker = new UserAuthorizationChecker($userSession, $authorizationDataProviderProvider);
$this->currentAuthorizationUser = new AuthorizationUser($this->userAuthorizationChecker);
}
public function setConfig(array $config)
{
$this->userAuthorizationChecker->setConfig($config[self::AUTHORIZATION_CONFIG_ATTRIBUTE]);
}
/**
* @param mixed $subject
*
* @throws ApiError
*/
public function denyAccessUnlessIsGranted(string $rightName, $subject = null): void
{
if ($this->isGrantedInternal($rightName, $subject) === false) {
throw new ApiError(Response::HTTP_FORBIDDEN, 'access denied. missing right '.$rightName);
}
}
/**
* @param mixed $subject
*/
public function isGranted(string $expressionName, $subject = null): bool
{
return $this->isGrantedInternal($expressionName, $subject);
}
/**
* @param mixed|null $defaultValue
*
* @return mixed|null
*/
public function getAttribute(string $attributeName, $defaultValue = null)
{
return $this->getAttributeInternal($attributeName, $defaultValue);
}
private function getAttributeInternal(string $attributeName, $defaultValue = null)
{
$this->userAuthorizationChecker->init();
return $this->userAuthorizationChecker->getAttribute($this->currentAuthorizationUser, $attributeName, $defaultValue);
}
/**
* @throws AuthorizationException
*/
private function isGrantedInternal(string $rightName, $subject = null): bool
{
$this->userAuthorizationChecker->init();
return $this->userAuthorizationChecker->isGranted($this->currentAuthorizationUser, $rightName, $subject);
}
}
......@@ -4,79 +4,9 @@ declare(strict_types=1);
namespace Dbp\Relay\CoreBundle\Authorization;
use Dbp\Relay\CoreBundle\API\UserSessionInterface;
use Dbp\Relay\CoreBundle\Exception\ApiError;
use Symfony\Component\HttpFoundation\Response;
abstract class AuthorizationService
{
public const AUTHORIZATION_CONFIG_ATTRIBUTE = 'authorization';
public const RIGHTS_CONFIG_ATTRIBUTE = 'rights';
public const ATTRIBUTES_CONFIG_ATTRIBUTE = 'attributes';
public const NAME_CONFIG_ATTRIBUTE = 'name';
public const EXPRESSION_CONFIG_ATTRIBUTE = 'expression';
/** @var UserAuthorizationChecker */
private $userAuthorizationChecker;
/** @var AuthorizationUser|null */
private $currentAuthorizationUser;
public function __construct(UserSessionInterface $userSession, AuthorizationDataProviderProvider $authorizationDataProviderProvider)
{
$this->userAuthorizationChecker = new UserAuthorizationChecker($userSession, $authorizationDataProviderProvider);
$this->currentAuthorizationUser = new AuthorizationUser($this->userAuthorizationChecker);
}
public function setConfig(array $config)
{
$this->userAuthorizationChecker->setConfig($config[self::AUTHORIZATION_CONFIG_ATTRIBUTE]);
}
/**
* @param mixed $subject
*
* @throws ApiError
*/
public function denyAccessUnlessIsGranted(string $rightName, $subject = null): void
{
if ($this->isGrantedInternal($rightName, $subject) === false) {
throw new ApiError(Response::HTTP_FORBIDDEN, 'access denied. missing right '.$rightName);
}
}
/**
* @param mixed $subject
* @deprecated use AbstractAuthorizationService instead
*/
public function isGranted(string $expressionName, $subject = null): bool
abstract class AuthorizationService extends AbstractAuthorizationService
{
return $this->isGrantedInternal($expressionName, $subject);
}
/**
* @param mixed|null $defaultValue
*
* @return mixed|null
*/
public function getAttribute(string $attributeName, $defaultValue = null)
{
return $this->getAttributeInternal($attributeName, $defaultValue);
}
private function getAttributeInternal(string $attributeName, $defaultValue = null)
{
$this->userAuthorizationChecker->init();
return $this->userAuthorizationChecker->getAttribute($this->currentAuthorizationUser, $attributeName, $defaultValue);
}
/**
* @throws AuthorizationException
*/
private function isGrantedInternal(string $rightName, $subject = null): bool
{
$this->userAuthorizationChecker->init();
return $this->userAuthorizationChecker->isGranted($this->currentAuthorizationUser, $rightName, $subject);
}
}
......@@ -47,8 +47,8 @@ class UserAuthorizationChecker
public function setConfig(array $config)
{
$this->loadExpressions($config[AuthorizationService::RIGHTS_CONFIG_ATTRIBUTE], $this->rightExpressions);
$this->loadExpressions($config[AuthorizationService::ATTRIBUTES_CONFIG_ATTRIBUTE], $this->attributeExpressions);
$this->loadExpressions($config[AbstractAuthorizationService::RIGHTS_CONFIG_ATTRIBUTE], $this->rightExpressions);
$this->loadExpressions($config[AbstractAuthorizationService::ATTRIBUTES_CONFIG_ATTRIBUTE], $this->attributeExpressions);
}
public function init()
......@@ -121,7 +121,7 @@ class UserAuthorizationChecker
private function loadExpressions(array $expressions, array &$target): void
{
foreach ($expressions as $expression) {
$target[$expression[AuthorizationService::NAME_CONFIG_ATTRIBUTE]] = $expression[AuthorizationService::EXPRESSION_CONFIG_ATTRIBUTE];
$target[$expression[AbstractAuthorizationService::NAME_CONFIG_ATTRIBUTE]] = $expression[AbstractAuthorizationService::EXPRESSION_CONFIG_ATTRIBUTE];
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment