vendor/appaydin/pd-user/Security/SwitchUserVoter.php line 24

Open in your IDE?
  1. <?php
  2. /**
  3.  * This file is part of the pd-admin pd-user package.
  4.  *
  5.  * @package     pd-user
  6.  * @license     LICENSE
  7.  * @author      Ramazan APAYDIN <apaydin541@gmail.com>
  8.  * @link        https://github.com/appaydin/pd-user
  9.  */
  10. namespace Pd\UserBundle\Security;
  11. use Pd\UserBundle\Model\User;
  12. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  13. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  14. use Symfony\Component\Security\Core\User\UserInterface;
  15. /**
  16.  * User Switcher.
  17.  *
  18.  * @author Ramazan APAYDIN <apaydin541@gmail.com>
  19.  */
  20. class SwitchUserVoter extends Voter
  21. {
  22.     protected function supports($attribute$subject): bool
  23.     {
  24.         return 'CAN_SWITCH_USER' === $attribute && $subject instanceof UserInterface;
  25.     }
  26.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  27.     {
  28.         $user $token->getUser();
  29.         if (!$user instanceof UserInterface || !$subject instanceof UserInterface) {
  30.             return false;
  31.         }
  32.         // All Access
  33.         if (\in_array(User::ROLE_ALL_ACCESS$token->getRoleNames(), true)) {
  34.             return true;
  35.         }
  36.         // Check Account Switcher
  37.         if (\in_array('ROLE_ALLOWED_TO_SWITCH'$user->getRoles(), true)) {
  38.             return true;
  39.         }
  40.         return false;
  41.     }
  42. }