<?php
declare(strict_types=1);
namespace App\Security\Voter;
use App\Entity\{Project\Project, User};
use Symfony\Component\Security\Core\{Authentication\Token\TokenInterface, Authorization\Voter\Voter, Security};
/**
* Does user can change the project?
*/
class ProjectChangeVoter extends Voter
{
private Security $security;
public function __construct(Security $security)
{
$this->security = $security;
}
protected function supports(string $attribute, $subject): bool
{
return $attribute === 'PROJECT_EDIT' && $subject instanceof Project;
}
/**
* @param string $attribute
* @param Project|mixed $subject
*/
protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
$manager = $user->getManager();
if ($this->security->isGranted('ROLE_MASTER_MANAGER')) {
return true;
}
if ($this->security->isGranted('ROLE_MANAGER') && $subject->getManagers()->contains($manager)) {
return true;
}
return false;
}
}