/src/Symfony/Component/Security/Core/Authorization/Voter/RoleVoter.php
http://github.com/symfony/symfony · PHP · 62 lines · 35 code · 11 blank · 16 comment · 4 complexity · 491d6312ce06d275c413cb61bf33c4de MD5 · raw file
- <?php
- /*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Symfony\Component\Security\Core\Authorization\Voter;
- use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
- /**
- * RoleVoter votes if any attribute starts with a given prefix.
- *
- * @author Fabien Potencier <fabien@symfony.com>
- */
- class RoleVoter implements VoterInterface
- {
- private $prefix;
- public function __construct(string $prefix = 'ROLE_')
- {
- $this->prefix = $prefix;
- }
- /**
- * {@inheritdoc}
- */
- public function vote(TokenInterface $token, $subject, array $attributes)
- {
- $result = VoterInterface::ACCESS_ABSTAIN;
- $roles = $this->extractRoles($token);
- foreach ($attributes as $attribute) {
- if (!\is_string($attribute) || 0 !== strpos($attribute, $this->prefix)) {
- continue;
- }
- if ('ROLE_PREVIOUS_ADMIN' === $attribute) {
- trigger_deprecation('symfony/security-core', '5.1', 'The ROLE_PREVIOUS_ADMIN role is deprecated and will be removed in version 6.0, use the IS_IMPERSONATOR attribute instead.');
- }
- $result = VoterInterface::ACCESS_DENIED;
- foreach ($roles as $role) {
- if ($attribute === $role) {
- return VoterInterface::ACCESS_GRANTED;
- }
- }
- }
- return $result;
- }
- protected function extractRoles(TokenInterface $token)
- {
- return $token->getRoleNames();
- }
- }