src/Security/TruVoter.php line 19

Open in your IDE?
  1. <?php
  2. /**
  3.  * Tru voter check access tru
  4.  *
  5.  * @package RMCS
  6.  * @author Vlad Shashkov  <vlad.s@zimalab.com>
  7.  * @copyright 2014 - 2019 The Zimalab
  8.  */
  9. declare(strict_types=1);
  10. namespace App\Security;
  11. use App\Entity\Main\{PermissionUser};
  12. use Doctrine\Persistence\{ManagerRegistryObjectManager};
  13. use Symfony\Component\HttpFoundation\{RequestRequestStack};
  14. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  15. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  16. class TruVoter extends Voter
  17. {
  18.     /**
  19.      * @var array const
  20.      */
  21.     private const FLAGS = [
  22.         'CheckReadTru' => 'ROLE_R',
  23.         'CheckReadWriteTru' => 'ROLE_RW',
  24.         'CheckAdminTru' => 'ROLE_ADMIN',
  25.         'CheckOwnerTRu' => 'ROLE_OWNER'
  26.     ];
  27.     /**
  28.      * @var array const
  29.      */
  30.     public const ROLES = [
  31.         'r' => 'ROLE_R',
  32.         'rw' => 'ROLE_RW',
  33.         'admin'  => 'ROLE_ADMIN',
  34.         'root' => 'ROLE_OWNER'
  35.     ];
  36.     /**
  37.      * @var array
  38.      */
  39.     private const WEIGHTS = [
  40.         'ROLE_R' => 1,
  41.         'ROLE_RW' => 2,
  42.         'ROLE_ADMIN' => 3,
  43.         'ROLE_OWNER' => 4
  44.     ];
  45.     /**
  46.      * @var ObjectManager
  47.      */
  48.     private $em;
  49.     /**
  50.      * @var Request|null
  51.      */
  52.     private $request;
  53.     /**
  54.      * TruVoter constructor.
  55.      *
  56.      * @param ManagerRegistry $registry
  57.      * @param RequestStack $requestStack
  58.      */
  59.     public function __construct(ManagerRegistry $registryRequestStack $requestStack)
  60.     {
  61.         $this->em $registry->getManager('default');
  62.         $this->request $requestStack->getCurrentRequest();
  63.     }
  64.     /**
  65.      * Determines if the attribute and subject are supported by this voter.
  66.      *
  67.      * @param string $attribute An attribute
  68.      * @param mixed $subject The subject to secure, e.g. an object the user wants to access or any other PHP type
  69.      *
  70.      * @return bool True if the attribute and subject are supported, false otherwise
  71.      */
  72.     protected function supports($attribute$subject):bool
  73.     {
  74.         return in_array($attributearray_keys(self::FLAGS));
  75.     }
  76.     /**
  77.      * Perform a single access check operation on a given attribute, subject and token.
  78.      * It is safe to assume that $attribute and $subject already passed the "supports()" method check.
  79.      *
  80.      * @param string $attribute
  81.      * @param mixed $subject
  82.      *
  83.      * @param TokenInterface $token
  84.      * @return bool
  85.      */
  86.     protected function voteOnAttribute($attribute$subjectTokenInterface $token):bool
  87.     {
  88.         $user $token->getUser();
  89.         if (!$user instanceof User) {
  90.             return false;
  91.         }
  92.         $userRoleWeight $this->getUserRoleWeight($user);
  93.         $accessWeight $this->getAccessWeight($attribute);
  94.         return $userRoleWeight >= $accessWeight;
  95.     }
  96.     /**
  97.      * @param User $user
  98.      * @return bool
  99.      */
  100.     private function getUserRoleWeight(User $user): bool
  101.     {
  102.         // Get user role weight
  103.         $roleUserWeight $this->getRoleWeight(current($user->getRoles()));
  104.         // Get site role weight
  105.         $client $user->getClient()->getId();
  106.         $idTru intval($this->request->get('id_tru'));
  107.         $siteRole $this->em->getRepository(Permission::class)
  108.             ->findPermissionByClient($client$idTru);
  109.         $siteRoleWeight $this->getRoleWeight($siteRole);
  110.         // Which role weight we should use for get access ?
  111.         return $roleUserWeight $siteRoleWeight ?
  112.             $siteRoleWeight :
  113.             $roleUserWeight;
  114.     }
  115.     /**
  116.      * @param $attribute
  117.      * @return false|int|string
  118.      */
  119.     private function getAccessWeight(string $attribute)
  120.     {
  121.         $accessRole self::FLAGS[$attribute];
  122.         return $this->getRoleWeight($accessRole);
  123.     }
  124.     /**
  125.      * @param $role
  126.      * @return false|int|string
  127.      */
  128.     private function getRoleWeight(string $role)
  129.     {
  130.         return array_search($roleself::WEIGHTS);
  131.     }
  132.     /**
  133.      * @return array|false
  134.      */
  135.     public static function getRoles()
  136.     {
  137.         return array_combine(range(1count(self::ROLES)), array_values(self::ROLES));
  138.     }
  139. }