src/Security/Voter/ServicesVoter.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\User;
  4. use App\Entity\Organisation;
  5. use App\Entity\Service;
  6. use App\Entity\Subscription;
  7. use App\Repository\PlanRepository;
  8. use App\Service\Misc;
  9. use Symfony\Component\Security\Core\User\UserInterface;
  10. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  11. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  12. class ServicesVoter extends Voter
  13. {
  14.     private $planRepository;
  15.     private $misc;
  16.     const LEAKED_PASSWORDS 'leaked-passwords';
  17.     const EXPOSED_SUB_DOMAINS 'exposed-sub-domains';
  18.     const POTENTIAL_VULNERABILITIES 'potential-vulnerabilities';
  19.     const EXPOSED_IPS_PORTS   'exposed-ips-and-ports';
  20.     const INTERNAL_ARCHITECTURE 'internal-architecture';
  21.     const DARK_WEB_EXPOSURE 'dark-web-exposure';
  22.     /**
  23.      * FormVoter constructor.
  24.      * @param PlanRepository $planRepository
  25.      * @param Misc $misc
  26.      */
  27.     public function __construct(PlanRepository $planRepositoryMisc $misc)
  28.     {
  29.         $this->misc $misc;
  30.         $this->planRepository $planRepository;
  31.     }
  32.     protected function supports($attribute$subject)
  33.     {
  34.         if (!in_array($attribute, [
  35.             self::LEAKED_PASSWORDS,
  36.             self::EXPOSED_SUB_DOMAINS,
  37.             self::POTENTIAL_VULNERABILITIES,
  38.             self::EXPOSED_IPS_PORTS,
  39.             self::INTERNAL_ARCHITECTURE,
  40.             self::DARK_WEB_EXPOSURE,
  41.         ])) {
  42.             return false;
  43.         }
  44.         return true;
  45.     }
  46.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  47.     {
  48.         if (!$subject instanceof Organisation) {
  49.             /**
  50.              * @var User $user
  51.              */
  52.             $user $token->getUser();
  53.             // if the user is anonymous, do not grant access
  54.             if (!$user instanceof UserInterface || $user->isIsDeleted()) {
  55.                 return false;
  56.             }
  57.             /**
  58.              * @var Organisation $organisation
  59.              */
  60.             $organisation $this->misc->getUserOrganisation($user);
  61.             if (!$organisation) {
  62.                 return false;
  63.             }
  64.         } else {
  65.             $organisation $subject;
  66.         }
  67.         /**
  68.          * @var Subscription $sub
  69.          */
  70.         $sub $this->misc->getActifSubsription($organisation);
  71.         
  72.         if ($sub) {
  73.             /**
  74.              * @var Service $sevice
  75.              */
  76.             foreach ($sub->getPlan()->getServices() as $service) {
  77.                 if ($service->getSlug() === $attribute) {
  78.                     return true;
  79.                 }
  80.             }
  81.         }
  82.         return false;
  83.     }
  84. }