<?php
namespace App\Security;
use App\Entity\AppActions;
use App\Entity\AppUser;
use App\Entity\Employee;
use App\Entity\Poste;
use App\Entity\RoleAction;
use App\Entity\UserRole;
use App\Utils\Consts;
use Doctrine\ORM\EntityManagerInterface;
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 PosteRouteVoter extends Voter
{
public const CREATE = 'CREATE';
public const VIEW = 'VIEW';
public const EDIT = 'EDIT';
protected function supports(string $attribute, $subject): bool
{
// replace with your own logic
// https://symfony.com/doc/current/security/voters.html
return in_array($attribute, [self::CREATE, self::VIEW, self::EDIT])
&& $subject instanceof \App\Entity\Poste;
}
private $entityManager;
private $security;
public function __construct(EntityManagerInterface $entityManager,Security $security)
{
$this->entityManager = $entityManager;
$this->security = $security;
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
if (!$user instanceof AppUser) {
// the user must be logged in; if not, deny access
return false;
}
// you know $subject is a Post object, thanks to `supports()`
/** @var Poste $poste */
$poste = $subject;
$isCentral = false;
$userPosteDirector = $this->entityManager->getRepository(Employee::class)->findOneBy(['partner' => $user->getPartner()])->getPosteDirector();
$subjectPosteDirector = $subject->getPosteDirector();
if ($this->entityManager->getRepository(RoleAction::class)->hasRoleAction($user,'isCentrale')) {
$isCentral = true;
}
switch ($attribute) {
case self::VIEW:
return $this->canView($poste, $user, $userPosteDirector, $subjectPosteDirector, $isCentral);
case self::EDIT:
return $this->canEdit($poste, $user, $userPosteDirector, $subjectPosteDirector, $isCentral);
case self::CREATE:
return $this->canCreate($poste, $user);
}
return false;
}
private function canView(Poste $poste, AppUser $user, $userPosteDirector, $subjectPosteDirector, $isCentral): bool
{
// if they can edit, they can view
if ($this->entityManager->getRepository(RoleAction::class)->hasRoleActionWithReadPermission($user,'Poste')) {
if (($userPosteDirector == $subjectPosteDirector) || ($isCentral)) {
return true;
}
else{ return false; }
}
else{ return false; }
}
private function canEdit(Poste $poste, AppUser $user, $userPosteDirector, $subjectPosteDirector, $isCentral): bool
{
// this assumes that the Poste object has a `getOwner()` method
if ($this->entityManager->getRepository(RoleAction::class)->hasRoleActionWithEditPermission($user,'Poste')) {
if (($userPosteDirector == $subjectPosteDirector) || ($isCentral)) {
return true;
}
else{ return false; }
}
else{ return false; }
}
private function canCreate(Poste $poste, AppUser $user): bool
{
// this assumes that the Poste object has a `getOwner()` method
return $this->entityManager->getRepository(RoleAction::class)->
hasRoleActionWithCreatePermission($user,'Poste');
}
}