<?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\User\UserInterface;
use App\Entity\User\User;
use App\Entity\User\Veto;
use App\Entity\User\AdminPays;
use App\Entity\User\DirectElevage;
use App\Entity\User\DirectRegional;
use App\Entity\User\DirectPrefectoral;
use App\Entity\User\DirectAbattoire;
use App\Security\Rights\UserRights;
class UserManagmentVoter extends Voter
{
protected function supports($attribute, $subject)
{
// replace with your own logic
// https://symfony.com/doc/current/security/voters.html
return in_array($attribute, [
UserRights::SHOW_COLLABORATEUR,
//UserRights::ADD_INSTITUTION,
UserRights::ADD_COLLABORATEUR
]);
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
{
$user = $token->getUser();
// if the user is anonymous, do not grant access
if (!$user instanceof UserInterface) {
return false;
}
switch ($attribute) {
case UserRights::SHOW_COLLABORATEUR:
return $this->canViewCollaborateur($user);
case UserRights::ADD_COLLABORATEUR:
return $this->canCreateCollaborateur($user);
}
return false;
}
private function canCreateCollaborateur(User $user)
{
if (($user instanceof AdminPays) or ( $user instanceof DirectAbattoire)) {
return true;
}
return false;
}
private function canViewCollaborateur(User $user)
{
if (($user instanceof AdminPays) or ( $user instanceof DirectAbattoire)) {
return true;
}
return false;
}
}