<?php
namespace App\Security\Voter;
use App\Repository\ModuleRepository;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\User\UserInterface;
class ModuleVoter extends Voter {
public const MODULES = [
"hosting" => "hosting",
];
private ModuleRepository $moduleRepository;
public function __construct(ModuleRepository $moduleRepository) {
$this->moduleRepository = $moduleRepository;
}
/**
* Please pass a `null` subject.
* The attribute already refers to the module we want access to.
*/
protected function supports(string $attribute, $subject): bool {
return in_array($attribute, self::MODULES);
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool {
$user = $token->getUser();
if (!$user instanceof UserInterface) return false;
switch ($attribute) {
case self::MODULES["hosting"]:
$module = $this->moduleRepository->findOneBy([
"slug" => $attribute,
]);
return $module->getActive();
default:
return false;
}
}
}