|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Workflow\EventListener; |
| 13 | + |
| 14 | +use Symfony\Component\ExpressionLanguage\Expression; |
| 15 | +use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolverInterface; |
| 16 | +use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
| 17 | +use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; |
| 18 | +use Symfony\Component\Security\Core\Role\RoleHierarchyInterface; |
| 19 | +use Symfony\Component\Workflow\Event\GuardEvent; |
| 20 | + |
| 21 | +/** |
| 22 | + * @author Grégoire Pineau <lyrixx@lyrixx.info> |
| 23 | + */ |
| 24 | +class GuardListener |
| 25 | +{ |
| 26 | + private $configuration; |
| 27 | + private $expressionLanguage; |
| 28 | + private $tokenStorage; |
| 29 | + private $authenticationChecker; |
| 30 | + private $trustResolver; |
| 31 | + private $roleHierarchy; |
| 32 | + |
| 33 | + public function __construct($configuration, ExpressionLanguage $expressionLanguage, TokenStorageInterface $tokenStorage, AuthorizationCheckerInterface $authenticationChecker, AuthenticationTrustResolverInterface $trustResolver, RoleHierarchyInterface $roleHierarchy = null) |
| 34 | + { |
| 35 | + $this->configuration = $configuration; |
| 36 | + $this->expressionLanguage = $expressionLanguage; |
| 37 | + $this->tokenStorage = $tokenStorage; |
| 38 | + $this->authenticationChecker = $authenticationChecker; |
| 39 | + $this->trustResolver = $trustResolver; |
| 40 | + $this->roleHierarchy = $roleHierarchy; |
| 41 | + } |
| 42 | + |
| 43 | + public function onTransition(GuardEvent $event, $eventName) |
| 44 | + { |
| 45 | + if (!isset($this->configuration[$eventName])) { |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + if (!$this->expressionLanguage->evaluate($this->configuration[$eventName], $this->getVariables($event))) { |
| 50 | + $event->setBlocked(true); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + // code should be sync with Symfony\Component\Security\Core\Authorization\Voter\ExpressionVoter |
| 55 | + private function getVariables(GuardEvent $event) |
| 56 | + { |
| 57 | + $token = $this->tokenStorage->getToken(); |
| 58 | + |
| 59 | + if (null !== $this->roleHierarchy) { |
| 60 | + $roles = $this->roleHierarchy->getReachableRoles($token->getRoles()); |
| 61 | + } else { |
| 62 | + $roles = $token->getRoles(); |
| 63 | + } |
| 64 | + |
| 65 | + $variables = array( |
| 66 | + 'token' => $token, |
| 67 | + 'user' => $token->getUser(), |
| 68 | + 'subject' => $event->getSubject(), |
| 69 | + 'roles' => array_map(function ($role) { |
| 70 | + return $role->getRole(); |
| 71 | + }, $roles), |
| 72 | + // needed for the is_granted expression function |
| 73 | + 'auth_checker' => $this->authenticationChecker, |
| 74 | + // needed for the is_* expression function |
| 75 | + 'trust_resolver' => $this->trustResolver, |
| 76 | + ); |
| 77 | + |
| 78 | + return $variables; |
| 79 | + } |
| 80 | +} |
0 commit comments