src/Security/Voters/Psd2ConsentVoter.php line 14

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Security\Voters;
  4. use App\Entity\Client;
  5. use App\Entity\OA2User;
  6. use App\Entity\Psd2Consent;
  7. use App\Repository\ClientRepository;
  8. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  9. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  10. class Psd2ConsentVoter extends Voter
  11. {
  12.     public const PERMISSION_VIEW 'view';
  13.     public const PERMISSION_APPROVE 'approve';
  14.     public const PERMISSION_REJECT 'reject';
  15.     private $clientRepository;
  16.     public function __construct(ClientRepository $clientRepository)
  17.     {
  18.         $this->clientRepository $clientRepository;
  19.     }
  20.     protected function supports($attribute$subject)
  21.     {
  22.         return !(!$subject instanceof Psd2Consent)
  23.          ;
  24.     }
  25.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  26.     {
  27.         /** @var Psd2Consent $psd2Consent */
  28.         $psd2Consent $subject;
  29.         $consentClient $psd2Consent->getAccessToken()->getUser()->getClientUser();
  30.         $currentClient $this->getCurrentClient($token);
  31.         if (!$currentClient || !$consentClient) {
  32.             return false;
  33.         }
  34.         $user $currentClient->getPerson()->getUser();
  35.         $consentUsername $consentClient->getUsername();
  36.         $username $user $user->getUsername() : '';
  37.         return $consentUsername === $username;
  38.     }
  39.     private function getCurrentClient(TokenInterface $token): ?Client
  40.     {
  41.         /** @var OA2User $oa2User */
  42.         $oa2User $token->getUser();
  43.         if (!$oa2User instanceof OA2User || $oa2User->isBankUser()) {
  44.             return null// Client user only
  45.         }
  46.         return $this->clientRepository->findOneByOa2User($oa2User);
  47.     }
  48. }