<?php
namespace App\Security\Voter;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\User\UserInterface;
class UserVoter extends Voter
{
public function __construct(Private Security $security){}
protected function supports($attribute, $subject): bool
{
return in_array($attribute, ['USER_VERIFIED', 'USER_APPROUVED', 'PROF_INDEPENDANT', 'PROF_NOT_SUSPENDED']);
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
// if the user is anonymous, do not grant access
if (!$user instanceof UserInterface) {
return false;
}
// dd($user);
// check isVerified()
if ($attribute=='USER_VERIFIED' and $user->isVerified()) {
return true;
}
// check isApprouved()
if ($attribute=='USER_APPROUVED' and $user->isApprouved()) {
return true;
}
// check isProfIndependant()
if ($attribute=='PROF_INDEPENDANT' and $user->isProf() and $user->isIndependant()) {
return true;
}
// check isProfNotSuspended()
if ($attribute=='PROF_NOT_SUSPENDED' and $user->isProf() and !$user->isSuspend()) {
return true;
}
return false;
}
}