Skip to content
Snippets Groups Projects
Select Git revision
  • 4c468456c2027305e98d59e8b8ed2a6a26fd9a32
  • main default protected
  • register-logging-channel
  • expr-lang
  • ci-82
  • attr-events
  • locale-wip
  • custom-routes
  • v0.1.85
  • v0.1.84
  • v0.1.83
  • v0.1.82
  • v0.1.81
  • v0.1.80
  • v0.1.79
  • v0.1.78
  • v0.1.77
  • v0.1.76
  • v0.1.75
  • v0.1.74
  • v0.1.73
  • v0.1.72
  • v0.1.71
  • v0.1.70
  • v0.1.69
  • v0.1.68
  • v0.1.67
  • v0.1.65
28 results

AbstractAuthorizationService.php

Blame
  • AbstractAuthorizationService.php 2.39 KiB
    <?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
    {
        /** @var AuthorizationExpressionChecker */
        private $userAuthorizationChecker;
    
        /** @var AuthorizationUser|null */
        private $currentAuthorizationUser;
    
        public function __construct(UserSessionInterface $userSession, AuthorizationDataProviderProvider $authorizationDataProviderProvider)
        {
            $muxer = new AuthorizationDataMuxer($authorizationDataProviderProvider->getAuthorizationDataProviders());
            $this->userAuthorizationChecker = new AuthorizationExpressionChecker($muxer);
            $this->currentAuthorizationUser = new AuthorizationUser($userSession->getUserIdentifier(), $this->userAuthorizationChecker);
        }
    
        public function setConfig(array $config)
        {
            $this->userAuthorizationChecker->setConfig($config);
        }
    
        /**
         * @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);
        }
    }