<?php
declare(strict_types=1);
namespace App\Api\Security\Voter;
use App\Api\Entity\User;
use App\Api\Entity\UserAbstract;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\User\UserInterface;
class UserBelongToCompanyVoter extends Voter
{
const VIEW = 'view';
protected function supports($attribute, $subject)
{
if (!in_array($attribute, [self::VIEW])) {
return false;
}
if (!$subject instanceof UserAbstract) {
return false;
}
return true;
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
{
$authenticatedUser = $token->getUser();
if (!$authenticatedUser instanceof UserAbstract) {
return false;
}
/** @var User $userFromRequest */
$userFromRequest = $subject;
switch ($attribute) {
case self::VIEW:
return $this->canView($userFromRequest, $authenticatedUser);
}
}
private function canView(UserInterface $userFromRequest, UserInterface $authenticatedUser): bool
{
return $userFromRequest->getTdc1Company() === $authenticatedUser;
}
}