<?php
namespace App\Security\Voters;
use App\Entity\Collab;
use App\Entity\CollabContract;
use App\Entity\Prime;
use App\Entity\User;
use App\Security\SecurityTrait;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Security;
class PrimeVoter extends Voter
{
use SecurityTrait;
const LIST = "list_prime";
const EDIT = "edit_prime";
const FIRST_LEVEL_VALIDATION = "first_level_validation_prime";
const SECOND_LEVEL_VALIDATION = "second_level_validation_prime";
private $security;
public function __construct(Security $security)
{
$this->security = $security;
}
protected function supports($attribute, $subject)
{
return in_array($attribute, [self::LIST, self::FIRST_LEVEL_VALIDATION, self::SECOND_LEVEL_VALIDATION]) ||
(in_array($attribute, [
self::EDIT
]) && $subject instanceof Prime);
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
{
$user = $token->getUser();
if (!$user instanceof User) {
return false;
}
switch ($attribute) {
case self::LIST:
return $this->isAdmin();
case self::EDIT:
return $this->isAdmin();
case self::FIRST_LEVEL_VALIDATION:
return $this->isAdmin() || $this->isSalesManager();
case self::SECOND_LEVEL_VALIDATION:
return $this->isAdmin() || $this->isGeneralManager();
}
throw new \LogicException('This code should not be reached!');
}
}