src/Bundles/PatientBundle/Security/ViewPatientVoter.php line 18

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Bundles\PatientBundle\Security;
  4. use App\Bundles\AnalyticBundle\Provider\IndexNameProvider;
  5. use App\Bundles\PatientBundle\DataTable\Filters\AnalyticFilterableFieldsEnum;
  6. use App\Bundles\PatientBundle\Entity\Patient;
  7. use App\Bundles\PatientBundle\Repository\QueryBuilder\PatientElasticsearchQueryBuilder;
  8. use App\Bundles\UserBundle\Enum\SystemPermissionEnum;
  9. use App\Platform\Service\ElasticsearchProvider;
  10. use Elastica\Query;
  11. use Elastica\Query\BoolQuery;
  12. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  13. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  14. class ViewPatientVoter extends Voter
  15. {
  16.     public function __construct(
  17.         private readonly PatientElasticsearchQueryBuilder $queryBuilder,
  18.         private readonly ElasticsearchProvider $elasticsearchProvider,
  19.         private readonly IndexNameProvider $indexNameProvider,
  20.     ) {
  21.     }
  22.     protected function supports(string $attribute$subject): bool
  23.     {
  24.         return $attribute == SystemPermissionEnum::SPECIFIC_PATIENT_VIEW->value;
  25.     }
  26.     /** @param Patient|null $subject */
  27.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  28.     {
  29.         if (!$subject) {
  30.             return false;
  31.         }
  32.         $client $this->elasticsearchProvider->provideElastica();
  33.         $index $client->getIndex($this->indexNameProvider->patient());
  34.         $query = new Query();
  35.         $query->setSource([AnalyticFilterableFieldsEnum::ID->value]);
  36.         $boolQuery = new BoolQuery();
  37.         $boolQuery->addMust($this->queryBuilder->getAccessQuery());
  38.         $boolQuery->addMust(new Query\Term([AnalyticFilterableFieldsEnum::ID->value => $subject->getId()]));
  39.         $query->setQuery($boolQuery);
  40.         return $index->search($query)->count() > 0;
  41.     }
  42. }