vendor/sonata-project/user-bundle/src/Security/Authorization/Voter/UserAclVoter.php line 20

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of the Sonata Project package.
  5. *
  6. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Sonata\UserBundle\Security\Authorization\Voter;
  12. use Sonata\UserBundle\Model\UserInterface;
  13. use Symfony\Component\Security\Acl\Voter\AclVoter;
  14. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  15. final class UserAclVoter extends AclVoter
  16. {
  17. public function supportsClass($class): bool
  18. {
  19. // support the Object-Scope ACL
  20. return is_subclass_of($class, UserInterface::class);
  21. }
  22. /**
  23. * @param mixed $attribute
  24. */
  25. public function supportsAttribute($attribute): bool
  26. {
  27. return 'EDIT' === $attribute || 'DELETE' === $attribute;
  28. }
  29. /**
  30. * @param mixed $subject
  31. * @param mixed[] $attributes
  32. */
  33. public function vote(TokenInterface $token, $subject, array $attributes): int
  34. {
  35. if (!\is_object($subject) || !$this->supportsClass(\get_class($subject))) {
  36. return self::ACCESS_ABSTAIN;
  37. }
  38. foreach ($attributes as $attribute) {
  39. $tokenUser = $token->getUser();
  40. if ($this->supportsAttribute($attribute) && $subject instanceof UserInterface && $tokenUser instanceof UserInterface) {
  41. if ($subject->isSuperAdmin() && !$tokenUser->isSuperAdmin()) {
  42. // deny a non super admin user to edit or delete a super admin user
  43. return self::ACCESS_DENIED;
  44. }
  45. }
  46. }
  47. // leave the permission voting to the AclVoter that is using the default permission map
  48. return self::ACCESS_ABSTAIN;
  49. }
  50. }