Files
myep-team/src/Security/Voter/UploadVoter.php
T

51 lines
1.5 KiB
PHP

<?php
namespace App\Security\Voter;
use App\Entity\Upload;
use App\Entity\User;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class UploadVoter extends Voter
{
public const VIEW = 'VIEW';
public const DELETE = 'DELETE';
public const DOWNLOAD = 'DOWNLOAD';
public const CHECK = 'CHECK';
public function __construct(private readonly Security $security)
{
}
protected function supports(string $attribute, mixed $subject): bool
{
if (! $subject instanceof Upload) {
return false;
}
return in_array($attribute, [static::VIEW, static::DELETE, static::DOWNLOAD, static::CHECK]);
}
protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool
{
/** @var Upload $upload */
$upload = $subject;
// Administrative users have full access
if (true === $this->security->isGranted('ROLE_ADMINISTRATIVE')) {
return true;
}
// Teamers may only access their own documents
if (true === in_array($attribute, [static::VIEW, static::DELETE, static::DOWNLOAD])) {
/** @var User $user */
$user = $token->getUser();
return Upload::TYPE_DOCUMENT === $upload->getType() || $user === $upload->getOwner();
}
return false;
}
}