src/Security/Voters/AccountVoter.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voters;
  3. use App\Entity\Account;
  4. use App\Entity\Client;
  5. use App\Entity\OA2User;
  6. use App\Repository\ClientAccountRepository;
  7. use App\Repository\ClientRepository;
  8. use App\Repository\Permissions\AccountPermissionSettingRepository;
  9. use App\Repository\Permissions\ClientAccountPermissionRepository;
  10. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  11. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  12. class AccountVoter extends Voter
  13. {
  14.     public const MODE_READ 'read';
  15.     public const MODE_UPDATE 'update';
  16.     public const MODE_DELETE 'delete';
  17.     public const PERMISSION_VIEW_ACCOUNT 'view_account';
  18.     public const PERMISSION_INTERNAL_PAYMENT 'create_internal_payment';
  19.     public const PERMISSION_SIGN_TRANSACTION 'sign_transactions';
  20.     public const PERMISSION_SEPA_PAYMENT 'create_sepa_payment';
  21.     public const PERMISSION_EXCHANGE_CURRENCY 'exchange_currency';
  22.     /**
  23.      * @var ClientRepository
  24.      */
  25.     private $clientRepository;
  26.     /**
  27.      * @var ClientAccountRepository
  28.      */
  29.     private $clientAccountRepository;
  30.     /**
  31.      * @var ClientAccountPermissionRepository
  32.      */
  33.     private $clientAccPermRepository;
  34.     /**
  35.      * @var AccountPermissionSettingRepository
  36.      */
  37.     private $accPermSettingRepository;
  38.     public function __construct(
  39.         ClientRepository $clientRepository,
  40.         AccountPermissionSettingRepository $permissionSettingRepository,
  41.         ClientAccountPermissionRepository $accountPermissionRepository,
  42.         ClientAccountRepository $clientAccountRepository
  43.     ) {
  44.         $this->clientRepository $clientRepository;
  45.         $this->accPermSettingRepository $permissionSettingRepository;
  46.         $this->clientAccPermRepository $accountPermissionRepository;
  47.         $this->clientAccountRepository $clientAccountRepository;
  48.     }
  49.     protected function supports($attribute$subject)
  50.     {
  51.         if (!in_array($attribute$this->getSupportedPermissions())) {
  52.             return false;
  53.         }
  54.         return !(!$subject instanceof Account)
  55.          ;
  56.     }
  57.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  58.     {
  59.         if (!in_array($attribute$this->getSupportedPermissions())) {
  60.             return false;
  61.         }
  62.         /** @var Account $account */
  63.         $account $subject;
  64.         $currentClient $this->getCurrentClient($token);
  65.         if (!$currentClient) {
  66.             return false;
  67.         }
  68.         if ($account->getClient()->getId() === $currentClient->getId()) {
  69.             return true;
  70.         }
  71.         switch ($attribute) {
  72.             case self::PERMISSION_VIEW_ACCOUNT:
  73.                 return $this->canViewAccount($account$currentClient);
  74.                 break;
  75.             case self::PERMISSION_INTERNAL_PAYMENT:
  76.                 return $this->canInternalPayment($account$currentClient);
  77.                 break;
  78.             case self::PERMISSION_SIGN_TRANSACTION:
  79.                 return $this->canSignTransactio($account$currentClient);
  80.                 break;
  81.             case self::PERMISSION_SEPA_PAYMENT:
  82.                 return $this->canSepaPayment($account$currentClient);
  83.                 break;
  84.             case self::PERMISSION_EXCHANGE_CURRENCY:
  85.                 return $this->canExchangeCurrency($account$currentClient);
  86.                 break;
  87.             default:
  88.                 break;
  89.         }
  90.         // TODO: check owner rules!
  91.         $relationAccount $this->clientAccountRepository->findOneByPersonalAccount($currentClient$account);
  92.         return (bool) ($relationAccount)
  93.          ;
  94.     }
  95.     private function canViewAccount(Account $accountClient $currentClient): bool
  96.     {
  97.         $permissionId $this->accPermSettingRepository->getIdByHardvalue(self::PERMISSION_VIEW_ACCOUNT);
  98.         $permission $this->clientAccPermRepository->findOneByPermission($account$currentClient$permissionId);
  99.         return $permission true false;
  100.     }
  101.     private function canInternalPayment(Account $accountClient $currentClient): bool
  102.     {
  103.         $permissionId $this->accPermSettingRepository->getIdByHardvalue(self::PERMISSION_INTERNAL_PAYMENT);
  104.         $permission $this->clientAccPermRepository->findOneByPermission($account$currentClient$permissionId);
  105.         return $permission true false;
  106.     }
  107.     private function canSignTransactio(Account $accountClient $currentClient): bool
  108.     {
  109.         $permissionId $this->accPermSettingRepository->getIdByHardvalue(self::PERMISSION_SIGN_TRANSACTION);
  110.         $permission $this->clientAccPermRepository->findOneByPermission($account$currentClient$permissionId);
  111.         return $permission true false;
  112.     }
  113.     private function canSepaPayment(Account $accountClient $currentClient): bool
  114.     {
  115.         $permissionId $this->accPermSettingRepository->getIdByHardvalue(self::PERMISSION_SEPA_PAYMENT);
  116.         $permission $this->clientAccPermRepository->findOneByPermission($account$currentClient$permissionId);
  117.         return $permission true false;
  118.     }
  119.     private function canExchangeCurrency(Account $accountClient $currentClient): bool
  120.     {
  121.         $permissionId $this->accPermSettingRepository->getIdByHardvalue(self::PERMISSION_EXCHANGE_CURRENCY);
  122.         $permission $this->clientAccPermRepository->findOneByPermission($account$currentClient$permissionId);
  123.         return $permission true false;
  124.     }
  125.     private function getSupportedPermissions(): array
  126.     {
  127.         return [
  128.             self::MODE_READself::MODE_UPDATEself::MODE_DELETE,
  129.             self::PERMISSION_VIEW_ACCOUNT,
  130.             self::PERMISSION_INTERNAL_PAYMENT,
  131.             self::PERMISSION_SIGN_TRANSACTION,
  132.             self::PERMISSION_SEPA_PAYMENT,
  133.             self::PERMISSION_EXCHANGE_CURRENCY,
  134.         ];
  135.     }
  136.     private function getCurrentClient(TokenInterface $token): ?Client
  137.     {
  138.         /** @var OA2User $oa2User */
  139.         $oa2User $token->getUser();
  140.         if (!$oa2User instanceof OA2User || $oa2User->isBankUser()) {
  141.             return null// Client user only
  142.         }
  143.         return $this->clientRepository->findOneByOa2User($oa2User);
  144.     }
  145. }