<?php
namespace App\Security\Voter;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use App\Entity\Accessmeister\User;
use App\Entity\Taskmeister\SelectionProject as Project;
class ProjectVoter extends Voter
{
private const VIEW = 'view';
private 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::VIEW])
&& $subject instanceof Project;
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
/**
* @var Project $subject
*/
$user = $token->getUser();
// if the user is anonymous, do not grant access
if (!$user instanceof User) {
return false;
}
$project = $subject;
// ... (check conditions and return true to grant permission) ...
switch ($attribute) {
case self::VIEW:
return $this->canView($project, $user);
break;
}
return false;
}
private function canView(Project $project, User $user): bool
{
return $project->getCompany() === $user->getCompany();
}
}