<?php
namespace App\Http\Api\Voter;
use App\Domain\Project\Entity\Version;
use App\Domain\User\Entity\User;
use JetBrains\PhpStorm\Pure;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
class VersionVoter extends Voter
{
const DOWNLOAD = 'DOWNLOAD_PROJECT';
protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool
{
if(!($subject instanceof Version)) return false;
/** @var User $user */
$user = $token->getUser();
switch ($attribute) {
case self::DOWNLOAD:
return $this->canAccessVersion($subject, $user);
default: return false;
}
}
private function canAccessVersion(Version $version, User $user): bool
{
return $version->getProject()->getCompany() === $user->getCompany();
}
protected function supports(string $attribute, mixed $subject): bool
{
return in_array($attribute, [self::DOWNLOAD]);
}
}