<?php
namespace App\Security\Voter;
use App\Entity\User;
use App\Entity\Organisation;
use App\Entity\Service;
use App\Entity\Subscription;
use App\Repository\PlanRepository;
use App\Service\Misc;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
class ServicesVoter extends Voter
{
private $planRepository;
private $misc;
const LEAKED_PASSWORDS = 'leaked-passwords';
const EXPOSED_SUB_DOMAINS = 'exposed-sub-domains';
const POTENTIAL_VULNERABILITIES = 'potential-vulnerabilities';
const EXPOSED_IPS_PORTS = 'exposed-ips-and-ports';
const INTERNAL_ARCHITECTURE = 'internal-architecture';
const DARK_WEB_EXPOSURE = 'dark-web-exposure';
/**
* FormVoter constructor.
* @param PlanRepository $planRepository
* @param Misc $misc
*/
public function __construct(PlanRepository $planRepository, Misc $misc)
{
$this->misc = $misc;
$this->planRepository = $planRepository;
}
protected function supports($attribute, $subject)
{
if (!in_array($attribute, [
self::LEAKED_PASSWORDS,
self::EXPOSED_SUB_DOMAINS,
self::POTENTIAL_VULNERABILITIES,
self::EXPOSED_IPS_PORTS,
self::INTERNAL_ARCHITECTURE,
self::DARK_WEB_EXPOSURE,
])) {
return false;
}
return true;
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
{
if (!$subject instanceof Organisation) {
/**
* @var User $user
*/
$user = $token->getUser();
// if the user is anonymous, do not grant access
if (!$user instanceof UserInterface || $user->isIsDeleted()) {
return false;
}
/**
* @var Organisation $organisation
*/
$organisation = $this->misc->getUserOrganisation($user);
if (!$organisation) {
return false;
}
} else {
$organisation = $subject;
}
/**
* @var Subscription $sub
*/
$sub = $this->misc->getActifSubsription($organisation);
if ($sub) {
/**
* @var Service $sevice
*/
foreach ($sub->getPlan()->getServices() as $service) {
if ($service->getSlug() === $attribute) {
return true;
}
}
}
return false;
}
}