rename voter helepers

This commit is contained in:
Julien Fastré 2021-09-17 10:08:33 +02:00
parent ebb2f5d243
commit cf40f38463
10 changed files with 54 additions and 65 deletions

View File

@ -5,7 +5,7 @@ namespace Chill\MainBundle\Security\Authorization;
use Chill\MainBundle\Entity\User; use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Security\Resolver\CenterResolverDispatcher; use Chill\MainBundle\Security\Resolver\CenterResolverDispatcher;
class DefaultVoter implements VoterInterface class DefaultVoterHelper implements VoterHelperInterface
{ {
protected AuthorizationHelper $authorizationHelper; protected AuthorizationHelper $authorizationHelper;
@ -50,14 +50,7 @@ class DefaultVoter implements VoterInterface
} }
if (NULL === $subject) { if (NULL === $subject) {
if (NULL === $center = $this->centerResolverDispatcher return 0 < count($this->authorizationHelper->getReachableCenters($token->getUser(), $attribute, null));
->resolveCenter($subject)) {
return false;
}
return $this->authorizationHelper->userCanReachCenter(
$token->getUser(),
$center
);
} }
return $this->authorizationHelper->userHasAccess( return $this->authorizationHelper->userHasAccess(

View File

@ -4,7 +4,7 @@ namespace Chill\MainBundle\Security\Authorization;
use Chill\MainBundle\Security\Resolver\CenterResolverDispatcher; use Chill\MainBundle\Security\Resolver\CenterResolverDispatcher;
class DefaultVoterFactory implements VoterFactoryInterface class DefaultVoterHelperFactory implements VoterHelperFactoryInterface
{ {
protected AuthorizationHelper $authorizationHelper; protected AuthorizationHelper $authorizationHelper;
protected CenterResolverDispatcher $centerResolverDispatcher; protected CenterResolverDispatcher $centerResolverDispatcher;
@ -19,7 +19,7 @@ class DefaultVoterFactory implements VoterFactoryInterface
public function generate($context): VoterGeneratorInterface public function generate($context): VoterGeneratorInterface
{ {
return new VoterGenerator( return new DefaultVoterHelperGenerator(
$this->authorizationHelper, $this->authorizationHelper,
$this->centerResolverDispatcher $this->centerResolverDispatcher
); );

View File

@ -4,7 +4,7 @@ namespace Chill\MainBundle\Security\Authorization;
use Chill\MainBundle\Security\Resolver\CenterResolverDispatcher; use Chill\MainBundle\Security\Resolver\CenterResolverDispatcher;
final class VoterGenerator implements VoterGeneratorInterface final class DefaultVoterHelperGenerator implements VoterGeneratorInterface
{ {
protected AuthorizationHelper $authorizationHelper; protected AuthorizationHelper $authorizationHelper;
protected CenterResolverDispatcher $centerResolverDispatcher; protected CenterResolverDispatcher $centerResolverDispatcher;
@ -25,9 +25,9 @@ final class VoterGenerator implements VoterGeneratorInterface
return $this; return $this;
} }
public function build(): VoterInterface public function build(): VoterHelperInterface
{ {
return new DefaultVoter( return new DefaultVoterHelper(
$this->authorizationHelper, $this->authorizationHelper,
$this->centerResolverDispatcher, $this->centerResolverDispatcher,
$this->configuration $this->configuration

View File

@ -1,7 +0,0 @@
<?php
namespace Chill\MainBundle\Security\Authorization;
interface VoterFactoryInterface
{
}

View File

@ -4,4 +4,12 @@ namespace Chill\MainBundle\Security\Authorization;
interface VoterGeneratorInterface interface VoterGeneratorInterface
{ {
/**
* @param string $class The FQDN of a class
* @param array $attributes an array of attributes
* @return $this
*/
public function addCheckFor(string $class, array $attributes): self;
public function build(): VoterHelperInterface;
} }

View File

@ -0,0 +1,8 @@
<?php
namespace Chill\MainBundle\Security\Authorization;
interface VoterHelperFactoryInterface
{
public function generate($context): VoterGeneratorInterface;
}

View File

@ -0,0 +1,12 @@
<?php
namespace Chill\MainBundle\Security\Authorization;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
interface VoterHelperInterface
{
public function supports($attribute, $subject): bool;
public function voteOnAttribute($attribute, $subject, TokenInterface $token);
}

View File

@ -1,7 +0,0 @@
<?php
namespace Chill\MainBundle\Security\Authorization;
interface VoterInterface
{
}

View File

@ -22,6 +22,8 @@ namespace Chill\PersonBundle\Security\Authorization;
use Chill\MainBundle\Security\Authorization\AbstractChillVoter; use Chill\MainBundle\Security\Authorization\AbstractChillVoter;
use Chill\MainBundle\Entity\User; use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Security\Authorization\AuthorizationHelper; use Chill\MainBundle\Security\Authorization\AuthorizationHelper;
use Chill\MainBundle\Security\Authorization\VoterHelperFactoryInterface;
use Chill\MainBundle\Security\Authorization\VoterHelperInterface;
use Chill\MainBundle\Security\ProvideRoleHierarchyInterface; use Chill\MainBundle\Security\ProvideRoleHierarchyInterface;
use Chill\MainBundle\Security\Resolver\CenterResolverDispatcher; use Chill\MainBundle\Security\Resolver\CenterResolverDispatcher;
use Chill\PersonBundle\Entity\Person; use Chill\PersonBundle\Entity\Person;
@ -42,52 +44,32 @@ class PersonVoter extends AbstractChillVoter implements ProvideRoleHierarchyInte
protected CenterResolverDispatcher $centerResolverDispatcher; protected CenterResolverDispatcher $centerResolverDispatcher;
protected VoterHelperInterface $voter;
public function __construct( public function __construct(
AuthorizationHelper $helper, AuthorizationHelper $helper,
CenterResolverDispatcher $centerResolverDispatcher CenterResolverDispatcher $centerResolverDispatcher,
VoterHelperFactoryInterface $voterFactory
) { ) {
$this->helper = $helper; $this->helper = $helper;
$this->centerResolverDispatcher = $centerResolverDispatcher; $this->centerResolverDispatcher = $centerResolverDispatcher;
$this->voter = $voterFactory
->generate(self::class)
->addCheckFor(Center::class, [self::STATS, self::LISTS, self::DUPLICATE])
->addCheckFor(Person::class, [self::CREATE, self::UPDATE, self::SEE, self::DUPLICATE])
->addCheckFor(null, [self::CREATE] )
->build()
;
} }
protected function supports($attribute, $subject) protected function supports($attribute, $subject)
{ {
if ($subject instanceof Person) { return $this->voter->supports($attribute, $subject);
return \in_array($attribute, [
self::CREATE, self::UPDATE, self::SEE, self::DUPLICATE
]);
} elseif ($subject instanceof Center) {
return \in_array($attribute, [
self::STATS, self::LISTS, self::DUPLICATE
]);
} elseif ($subject === null) {
return $attribute === self::CREATE;
} else {
return false;
}
} }
protected function voteOnAttribute($attribute, $subject, TokenInterface $token) protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
{ {
if (!$token->getUser() instanceof User) { return $this->voter->voteOnAttribute($attribute, $subject, $token);
return false;
}
if ($subject === null) {
$centers = $this->helper->getReachableCenters($token->getUser(),
new Role($attribute));
return count($centers) > 0;
}
$center = $this->centerResolverDispatcher->resolveCenter($subject);
if (NULL === $center && $subject instanceof Person) {
// person without any center are seen by everybody
return true;
}
return $this->helper->userHasAccess($token->getUser(), $subject, $attribute);
} }
private function getAttributes() private function getAttributes()

View File

@ -21,8 +21,8 @@ namespace Chill\TaskBundle\Security\Authorization;
use Chill\EventBundle\Entity\Event; use Chill\EventBundle\Entity\Event;
use Chill\MainBundle\Entity\Center; use Chill\MainBundle\Entity\Center;
use Chill\MainBundle\Security\Authorization\AbstractChillVoter; use Chill\MainBundle\Security\Authorization\AbstractChillVoter;
use Chill\MainBundle\Security\Authorization\VoterFactoryInterface; use Chill\MainBundle\Security\Authorization\VoterHelperFactoryInterface;
use Chill\MainBundle\Security\Authorization\VoterInterface; use Chill\MainBundle\Security\Authorization\VoterHelperInterface;
use Chill\MainBundle\Security\Resolver\CenterResolverDispatcher; use Chill\MainBundle\Security\Resolver\CenterResolverDispatcher;
use Chill\TaskBundle\Entity\AbstractTask; use Chill\TaskBundle\Entity\AbstractTask;
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface; use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
@ -61,7 +61,7 @@ final class TaskVoter extends AbstractChillVoter implements ProvideRoleHierarchy
protected CenterResolverDispatcher $centerResolverDispatcher; protected CenterResolverDispatcher $centerResolverDispatcher;
protected VoterInterface $voter; protected VoterHelperInterface $voter;
public function __construct( public function __construct(
AccessDecisionManagerInterface $accessDecisionManager, AccessDecisionManagerInterface $accessDecisionManager,
@ -69,7 +69,7 @@ final class TaskVoter extends AbstractChillVoter implements ProvideRoleHierarchy
EventDispatcherInterface $eventDispatcher, EventDispatcherInterface $eventDispatcher,
LoggerInterface $logger, LoggerInterface $logger,
CenterResolverDispatcher $centerResolverDispatcher, CenterResolverDispatcher $centerResolverDispatcher,
VoterFactoryInterface $voterFactory VoterHelperFactoryInterface $voterFactory
) { ) {
$this->accessDecisionManager = $accessDecisionManager; $this->accessDecisionManager = $accessDecisionManager;
$this->authorizationHelper = $authorizationHelper; $this->authorizationHelper = $authorizationHelper;