src/Security/Voter/DatasourceVoter.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use Oz\ApiNvl\Model\User;
  4. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  5. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  6. use Symfony\Component\Security\Core\User\UserInterface;
  7. /**
  8.  *
  9.  */
  10. class DatasourceVoter extends Voter
  11. {
  12.     /**
  13.      * Determines if the attribute and subject are supported by this voter.
  14.      *
  15.      * @param string $attribute An attribute
  16.      * @param mixed  $subject   The subject to secure, e.g. an object the user wants to access or any other PHP type
  17.      *
  18.      * @return bool
  19.      */
  20.     protected function supports(string $attribute$subject): bool
  21.     {
  22.         return in_array($attribute, ['CAN_READ''CAN_WRITE''CAN_VALIDATE']) && is_string($subject);
  23.     }
  24.     /**
  25.      * Perform a single access check operation on a given attribute, subject and token.
  26.      * It is safe to assume that $attribute and $subject already passed the "supports()" method check.
  27.      *
  28.      * @param string         $attribute
  29.      * @param mixed          $subject
  30.      * @param TokenInterface $token
  31.      *
  32.      * @return bool
  33.      */
  34.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  35.     {
  36.         $user $token->getUser();
  37.         if (!$user instanceof User) {
  38.             return false;
  39.         }
  40.         switch ($attribute) {
  41.             case 'CAN_READ':
  42.                 return $this->canRead($user$subject);
  43.             case 'CAN_WRITE':
  44.                 return $this->canWrite($user$subject);
  45.             case 'CAN_VALIDATE':
  46.                 return $this->canValidate($user$subject);
  47.         }
  48.         return false;
  49.     }
  50.     /**
  51.      * @param User   $user
  52.      * @param string $subject
  53.      *
  54.      * @return bool
  55.      */
  56.     private function canRead(User $userstring $subject): bool
  57.     {
  58. //        return false;
  59.         if (!$this->permissionExists($user$subject)) {
  60.             return false;
  61.         }
  62.         $permission $user->getPermissions()[$subject];
  63.         return $permission->isRead();
  64.     }
  65.     /**
  66.      * @param User   $user
  67.      * @param string $subject
  68.      *
  69.      * @return bool
  70.      */
  71.     private function canWrite(User $userstring $subject): bool
  72.     {
  73.         if (!$this->permissionExists($user$subject)) {
  74.             return false;
  75.         }
  76.         $permission $user->getPermissions()[$subject];
  77.         return $permission->isWrite();
  78.     }
  79.     /**
  80.      * @param User   $user
  81.      * @param string $subject
  82.      *
  83.      * @return bool
  84.      */
  85.     private function canValidate(User $userstring $subject): bool
  86.     {
  87.         if (!$this->permissionExists($user$subject)) {
  88.             return false;
  89.         }
  90.         $permission $user->getPermissions()[$subject];
  91.         return $permission->isValidate();
  92.     }
  93.     /**
  94.      * @param User   $user
  95.      * @param string $subject
  96.      *
  97.      * @return bool
  98.      */
  99.     private function permissionExists(User $userstring $subject): bool
  100.     {
  101.         return array_key_exists($subject$user->getPermissions());
  102.     }
  103. }