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[] $attributes
  31. *
  32. * @return self::ACCESS_ABSTAIN|self::ACCESS_DENIED
  33. */
  34. public function vote(TokenInterface $token, mixed $subject, array $attributes): int
  35. {
  36. if (!\is_object($subject) || !$this->supportsClass($subject::class)) {
  37. return self::ACCESS_ABSTAIN;
  38. }
  39. foreach ($attributes as $attribute) {
  40. $tokenUser = $token->getUser();
  41. if ($this->supportsAttribute($attribute) && $subject instanceof UserInterface && $tokenUser instanceof UserInterface) {
  42. if ($subject->isSuperAdmin() && !$tokenUser->isSuperAdmin()) {
  43. // deny a non super admin user to edit or delete a super admin user
  44. return self::ACCESS_DENIED;
  45. }
  46. }
  47. }
  48. // leave the permission voting to the AclVoter that is using the default permission map
  49. return self::ACCESS_ABSTAIN;
  50. }
  51. }