src/Security/Voter/TimeTracking/TimeTrackingVoter.php line 13
<?php
namespace App\Security\Voter\TimeTracking;
use App\Entity\TimeTracking\Summary;
use App\Entity\TimeTracking\TimeEntry;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Bundle\SecurityBundle\Security;
class TimeTrackingVoter extends Voter
{
final public const EDIT = 'TT_ENTITY_EDIT';
final public const VIEW = 'TT_ENTITY_VIEW';
final public const DELETE = 'TT_ENTITY_DELETE';
public function __construct(
private readonly Security $security)
{
}
protected function supports(string $attribute, mixed $subject): bool
{
if(!in_array($attribute, [self::EDIT, self::VIEW, self::DELETE])){
return false;
}
if (!$subject instanceof TimeEntry) {
return false;
}
return true;
}
protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool
{
$user = $token->getUser();
// if the user is anonymous, do not grant access
if (!$user instanceof UserInterface) {
return false;
}
if (!$this->security->isGranted('ROLE_STAFF')){
return false;
}
if($user->getChronoActiv() != 1){
return false;
}
$flag = false;
$mapping = $user->getMapping();
foreach($mapping->getPlugin() as $plugin){
if($plugin->getPlugin()->getId() == 2 and $plugin->getState() == 1){
$flag = true;
}
}
if($flag == false){
return false;
}
// ... (check conditions and return true to grant permission) ...
return match($attribute) {
self::VIEW => $this->canView($subject, $user),
self::EDIT => $this->canEdit($subject, $user),
self::DELETE => $this->canDelete(),
default => throw new \LogicException('This code should not be reached!')
};
}
private function canView(TimeEntry $entity, CustomerUser $user): bool
{
if ($entity->getSummary()->getStaff()->getId() == $user->getStaff()) {
return true;
}
return false;
}
private function canEdit(?TimeEntry $entity, CustomerUser $user): bool
{
if($entity->getStaff() == null AND $user->getChronoActivApp() != 1){
return false;
}
if ($entity->getStaff()->getId() == $user->getStaff()) {
return true;
}
return $this->security->isGranted('ROLE_ADMIN');
}
private function canDelete(): bool
{
return $this->security->isGranted('ROLE_ADMIN');
}
}