src/Security/Voters/Content/NewsVoter.php line 17
<?php
/**
* Created by PhpStorm.
* User: Михаил
* Date: 08.10.2018
* Time: 14:21
*/
namespace App\Security\Voters\Content;
use App\Entity\Content\News;
use App\Entity\User\User;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Security;
class NewsVoter extends Voter
{
public const VIEW = 'view';
/**
* @var Security
*/
private $security;
public function __construct(Security $security)
{
$this->security = $security;
}
/**
* Determines if the attribute and subject are supported by this voter.
*
* @param string $attribute An attribute
* @param mixed $subject The subject to secure, e.g. an object the user wants to access or any other PHP type
*
* @return bool True if the attribute and subject are supported, false otherwise
*/
protected function supports($attribute, $subject): bool
{
if (!in_array($attribute, [self::VIEW])) {
return false;
}
// only vote on Post objects inside this voter
if (!$subject instanceof News) {
return false;
}
return true;
}
/**
* Perform a single access check operation on a given attribute, subject and token.
* It is safe to assume that $attribute and $subject already passed the "supports()" method check.
*
* @param string $attribute
* @param News $subject
* @param TokenInterface $token
*
* @return bool
*/
protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
{
switch ($attribute) {
case self::VIEW:
return (!$subject->isDeleted() && $subject->isPublished())||$this->security->isGranted('ROLE_ADMIN');
}
throw new \LogicException('This code should not be reached!');
}
}