src/Security/Voters/ClusterVoter.php line 17

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Security\Voters;
  4. use App\Services\Clients\ClientSystemState\ClientSystemStateService;
  5. use App\Entity\Client;
  6. use App\Entity\Cluster;
  7. use App\Entity\CompanyPerson;
  8. use App\Entity\OA2User;
  9. use App\Repository\ClientRepository;
  10. use App\Repository\CompanyPersonRepository;
  11. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  12. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  13. class ClusterVoter extends Voter
  14. {
  15.     public const MODE_READ 'read';
  16.     public const CAN_MAKE_CLUSTERS 'can_make_clusters';
  17.     private $clientRepository;
  18.     private $clientSystemStateService;
  19.     private $companyPersonRepository;
  20.     public function __construct(
  21.         ClientRepository $clientRepository,
  22.         ClientSystemStateService $clientSystemStateService,
  23.         CompanyPersonRepository $companyPersonRepository
  24.     ) {
  25.         $this->clientRepository $clientRepository;
  26.         $this->clientSystemStateService $clientSystemStateService;
  27.         $this->companyPersonRepository $companyPersonRepository;
  28.     }
  29.     /**
  30.      * @param string $attribute
  31.      * @param mixed  $subject
  32.      *
  33.      * @return bool
  34.      */
  35.     protected function supports($attribute$subject)
  36.     {
  37.         return !(!$subject instanceof Cluster)
  38.          ;
  39.     }
  40.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  41.     {
  42.         if (!in_array($attribute$this->getSupportedPermissions())) {
  43.             return false;
  44.         }
  45.         /** @var Cluster $cluster */
  46.         $cluster $subject;
  47.         /** @var Client|null $currentClient */
  48.         $currentClient $this->getCurrentClient($token);
  49.         if ($currentClient === null) {
  50.             return false;
  51.         }
  52.         if ($attribute === self::MODE_READ) {
  53.             return $this->canRead($currentClient$cluster);
  54.         }
  55.         if ($attribute === self::CAN_MAKE_CLUSTERS) {
  56.             return $this->canMakeClusters($currentClient$cluster);
  57.         }
  58.         return false;
  59.     }
  60.     private function getSupportedPermissions(): array
  61.     {
  62.         return [
  63.             self::MODE_READ,
  64.             self::CAN_MAKE_CLUSTERS,
  65.         ];
  66.     }
  67.     private function getCurrentClient(TokenInterface $token): ?Client
  68.     {
  69.         /** @var OA2User $oa2User */
  70.         $oa2User $token->getUser();
  71.         if (!$oa2User instanceof OA2User || $oa2User->isBankUser()) {
  72.             return null// Client user only
  73.         }
  74.         return $this->clientRepository->findOneByOa2User($oa2User);
  75.     }
  76.     public function canRead(Client $currentClientCluster $cluster): bool
  77.     {
  78.         /** @var CompanyPerson $relationCompany */
  79.         $relationCompany $this->companyPersonRepository->findOneByOwnerClient($currentClient$cluster->getClient());
  80.         return (bool) $relationCompany;
  81.     }
  82.     public function canMakeClusters(Client $currentClientCluster $cluster): bool
  83.     {
  84.         /** @var CompanyPerson $relationCompany */
  85.         $relationCompany $this->companyPersonRepository->findOneByOwnerClient($currentClient$cluster->getClient());
  86.         return $relationCompany $relationCompany->getCanMakeClusters() : false;
  87.     }
  88. }