src/TSMS/AdminBundle/Security/Authorization/Voter/UserVoter.php line 18

Open in your IDE?
  1. <?php
  2. /**
  3. * Copyright (C) SUEZ Smart Solutions - All Rights Reserved
  4. * On’Connect Gateway Management, 2018
  5. * Unauthorized copying of this file, via any medium is strictly prohibited
  6. * Proprietary and confidential
  7. * For the full copyright and license information, please report to the LICENSE CONTRACT.
  8. */
  9. namespace TSMS\AdminBundle\Security\Authorization\Voter;
  10. use Symfony\Component\DependencyInjection\ContainerInterface;
  11. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  12. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  13. use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
  14. use TSMS\CoreBundle\Entity\User;
  15. class UserVoter implements VoterInterface
  16. {
  17.     const DELETE 'DELETE_USER';
  18.     /**
  19.      * @var ContainerInterface
  20.      */
  21.     private $container;
  22.     /**
  23.      * @var AuthorizationCheckerInterface
  24.      */
  25.     private $authorizationChecker;
  26.     /**
  27.      * @param ContainerInterface $container
  28.      */
  29.     public function __construct(ContainerInterface $container)
  30.     {
  31.         $this->container $container;
  32.     }
  33.     /**
  34.      * @param string $attribute
  35.      *
  36.      * @return bool
  37.      */
  38.     public function supportsAttribute($attribute)
  39.     {
  40.         return in_array($attribute, [self::DELETE]);
  41.     }
  42.     /**
  43.      * @param string $class
  44.      *
  45.      * @return bool
  46.      */
  47.     public function supportsClass($class)
  48.     {
  49.         $supportedClass 'TSMS\CoreBundle\Entity\User';
  50.         return $supportedClass === $class || is_subclass_of($class$supportedClass);
  51.     }
  52.     /**
  53.      * @param TokenInterface $token
  54.      * @param User           $user
  55.      * @param array          $attributes
  56.      */
  57.     public function vote(TokenInterface $token$user, array $attributes)
  58.     {
  59.         if (
  60.             !is_object($user) ||
  61.             (null !== $user && !$this->supportsClass(get_class($user)))
  62.         ) {
  63.             return VoterInterface::ACCESS_ABSTAIN;
  64.         }
  65.         if (!== count($attributes)) {
  66.             throw new \InvalidArgumentException(
  67.                 'Only one attribute is allowed for DELETE'
  68.             );
  69.         }
  70.         $attribute $attributes[0];
  71.         if (!$this->supportsAttribute($attribute)) {
  72.             return VoterInterface::ACCESS_ABSTAIN;
  73.         }
  74.         $this->authorizationChecker $this->container->get('security.authorization_checker');
  75.         $currentUser $token->getUser();
  76.         switch ($attribute) {
  77.             case self::DELETE:
  78.                 //Cannot delete himself.
  79.                 if ($currentUser->getId() == $user->getId() || null == $user->getId()) {
  80.                     return VoterInterface::ACCESS_DENIED;
  81.                 }
  82.                 //If not a user admin collectivity/ER/TSMS can not delete user.
  83.                 if (!$this->authorizationChecker->isGranted('ROLE_ADMIN_COLLECTIVITY')) {
  84.                     return VoterInterface::ACCESS_DENIED;
  85.                 }
  86.                 //if current user is admin collectivity, he can just delete external user.
  87.                 if (User::ADMIN_COLLECTIVITY_LEVEL == $currentUser->getAdminLevel()) {
  88.                     if (User::INTERNAL_USER_LEVEL == $user->getAdminLevel()) {
  89.                         return VoterInterface::ACCESS_DENIED;
  90.                     }
  91.                 }
  92.                 //If current user is admin collectivity/TSMS, can not delete admin TSMS.
  93.                 if (in_array($currentUser->getAdminLevel(), [User::ADMIN_PERIMETER_LEVELUser::ADMIN_COLLECTIVITY_LEVEL])) {
  94.                     if (User::ADMIN_TSMS_LEVEL == $user->getAdminLevel()) {
  95.                         return VoterInterface::ACCESS_DENIED;
  96.                     }
  97.                 }
  98.                 return VoterInterface::ACCESS_GRANTED;
  99.                 break;
  100.         }
  101.         return VoterInterface::ACCESS_DENIED;
  102.     }
  103. }