<?php
namespace App\Security\Voter;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\User\UserInterface;
class EmailAttachmentVoter extends Voter
{
public const OWNER_USER = 'OWNER_USER';
/** @var Security */
private $security;
public function __construct(Security $security)
{
$this->security = $security;
}
protected function supports(string $attribute, $subject): bool
{
return in_array($attribute, [self::OWNER_USER]);
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
if(is_null($subject)) {
return true;
}
$user = $token->getUser();
if (!$user instanceof UserInterface) {
return false;
}
if ($this->security->isGranted('ROLE_SUPERADMIN')) {
return true;
}
switch ($attribute) {
case self::OWNER_USER:
foreach ($user->getCompanies() as $company) {
if ($company->getEmailAttachments()->contains($subject)) {
return true;
}
}
return false;
break;
}
return false;
}
}