src/Security/Voter/KYCVoter.php line 12

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Security\Voter;
  4. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  5. use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
  6. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. final class KYCVoter extends Voter
  9. {
  10.     // Backward compatibility roles. Should be removed in the future and replaced with proper security checking.
  11.     private const ALLOWED_ROLES = ['ROLE_KYC''ROLE_TRANSFERS''ROLE_SUPPORT''ROLE_SUPPORT_MANAGER''ROLE_TRANSFERS_MANAGER''ROLE_TRANSACTION_MANAGER''ROLE_CARD_OPS''ROLE_DISPUTE'];
  12.     public function __construct(
  13.         private AccessDecisionManagerInterface $accessDecisionManager,
  14.         private AuthorizationCheckerInterface $authorizationChecker,
  15.     ) {}
  16.     protected function supports(string $attribute$subject): bool
  17.     {
  18.         return in_array($attribute, ['KYC_VIEW''KYC_EDIT''KYC_REGIX'], true);
  19.     }
  20.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  21.     {
  22.         $user $token->getUser();
  23.         if (!$user) {
  24.             return false;
  25.         }
  26.         $isKycManager $this->accessDecisionManager->decide($token, ['ROLE_KYC_MANAGER']);
  27.         if ($isKycManager) {
  28.             return true;
  29.         }
  30.         if ($attribute === 'KYC_VIEW') {
  31.             foreach (self::ALLOWED_ROLES as $role) {
  32.                 if ($this->authorizationChecker->isGranted($role)) {
  33.                     return true;
  34.                 }
  35.             }
  36.             return false;
  37.         }
  38.         // TODO: Extend for the rest of KYC related permissions
  39.         return false;
  40.     }
  41. }