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