use event to override vote on task

Now, the bundle dispatch an event `chill_task.vote` to override the authorization
on voting on a task.

If a subscriber or listener emits a vote on the event, this vote override the
logic defined for the default task.
This commit is contained in:
2018-07-17 15:28:04 +02:00
parent 95f75e53eb
commit ebc0961fa8
3 changed files with 115 additions and 0 deletions

View File

@@ -29,6 +29,8 @@ use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Chill\MainBundle\Entity\User;
use Chill\PersonBundle\Entity\Person;
use Symfony\Component\Security\Core\Role\Role;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Chill\TaskBundle\Security\Authorization\AuthorizationEvent;
/**
*
@@ -66,14 +68,22 @@ class TaskVoter extends AbstractChillVoter implements ProvideRoleHierarchyInterf
* @var LoggerInterface
*/
protected $logger;
/**
*
* @var EventDispatcherInterface
*/
protected $eventDispatcher;
public function __construct(
AccessDecisionManagerInterface $accessDecisionManager,
AuthorizationHelper $authorizationHelper,
EventDispatcherInterface $eventDispatcher,
LoggerInterface $logger
) {
$this->accessDecisionManager = $accessDecisionManager;
$this->authorizationHelper = $authorizationHelper;
$this->eventDispatcher = $eventDispatcher;
$this->logger = $logger;
}
@@ -101,6 +111,21 @@ class TaskVoter extends AbstractChillVoter implements ProvideRoleHierarchyInterf
if (!$token->getUser() instanceof User) {
return false;
}
$event = new AuthorizationEvent($subject, $attribute, $token);
$this->eventDispatcher->dispatch(AuthorizationEvent::VOTE, $event);
if ($event->hasVote()) {
$this->logger->debug("The TaskVoter is overriding by "
.AuthorizationEvent::VOTE, [
'vote' => $event->getVote(),
'task_id' => $subject->getId()
]);
return $event->getVote();
}
if ($subject instanceof AbstractTask) {
if ($subject->getPerson() === null) {