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\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';
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 === in_array('ROLE_ADMINISTRATIVE', $token->getRoleNames())) {
// Only new documents may be checked
if (static::CHECK === $attribute) {
return in_array($upload->getStatus(), [Upload::STATUS_NEW, Upload::STATUS_PENDING]);
}
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->getOwner() === $user;
}
return false;
}
}