<?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\RemovalRequest;
class RequestVoter extends Voter
{
protected function supports($attribute, $subject)
{
// replace with your own logic
// https://symfony.com/doc/current/security/voters.html
return in_array($attribute, ['create', 'update','delete','view'])
&& $subject instanceof \App\Entity\RemovalRequest;
}
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;
}
$enterprise = $user->getEnterprise();
if ($user->getRoles()[0] == "ROLE_ADMIN") { return true;
}
// ... (check conditions and return true to grant permission) ...
switch ($attribute) {
case 'create':
if($subject->getCustomer() == $enterprise) { return true;
}
break;
case 'update':
if($subject->getCustomer() == $enterprise ) { return true;
}
break;
case 'delete':
if($subject->getCustomer() == $enterprise && $subject->getStatus() == RemovalRequest::STATUT_DRAFT ) { return true;
}
break;
case 'view':
if($subject->getCustomer() == $enterprise )
{
return true;
}
break;
}
return false;
}
}