src/Voters/ConsultationNoteVoter.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Voters;
  3. use App\Entity\Patient\Note\ConsultationNote;
  4. use App\Entity\Person\User;
  5. use App\Exception\Api\ApiException;
  6. use App\Services\DeviceManager;
  7. use App\Services\Licence\CheckLicenceService;
  8. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  9. use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
  10. class ConsultationNoteVoter extends OrganisationVoter
  11. {
  12.     private $checkLicenceService;
  13.     private $deviceManager;
  14.     public function __construct(
  15.         AccessDecisionManagerInterface $decisionManager,
  16.         CheckLicenceService $checkLicenceService,
  17.         DeviceManager $deviceManager
  18.     ) {
  19.         parent::__construct($decisionManager);
  20.         $this->checkLicenceService $checkLicenceService;
  21.         $this->deviceManager $deviceManager;
  22.     }
  23.     protected function supports($attribute$subject)
  24.     {
  25.         return $subject instanceof ConsultationNote && in_array($attribute, [self::CREATEself::EDIT], true);
  26.     }
  27.     /**
  28.      * @param ConsultationNote $subject
  29.      */
  30.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  31.     {
  32.         /** @var User $user */
  33.         $user $token->getUser();
  34.         if (!$this->decisionManager->decide($token, [User::ROLE_PRACTITIONER])) {
  35.             return false;
  36.         }
  37.         if (self::EDIT === $attribute) {
  38.             if ($subject->getCreatedAt()->format('Ymd') !== (new \DateTime())->format('Ymd')) {
  39.                 return false;
  40.             }
  41.             if ($user->getClinic() !== $subject->getClinic()) {
  42.                 return false;
  43.             }
  44.         }
  45.         try {
  46.             $this->checkLicenceService->checkUser($this->deviceManager$user);
  47.         } catch (ApiException $apiException) {
  48.             return false;
  49.         }
  50.         return true;
  51.     }
  52. }