<?php
namespace App\Security\Content;
use App\Entity\Content\BlogCategory;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\User\UserInterface;
class BlogCategoryVoter extends Voter
{
const VIEW = 'BLOG_CATEGORY_VIEW';
const VIEW_ANY = 'BLOG_CATEGORY_VIEW_ANY';
const CREATE = 'BLOG_CATEGORY_CREATE';
const EDIT = 'BLOG_CATEGORY_EDIT';
const EDIT_ANY = 'BLOG_CATEGORY_EDIT_ANY';
const DELETE = 'BLOG_CATEGORY_DELETE';
/**
* @var AccessDecisionManagerInterface
*/
private AccessDecisionManagerInterface $decisionManager;
/**
* UserVoter constructor.
*/
public function __construct(AccessDecisionManagerInterface $decisionManager)
{
$this->decisionManager = $decisionManager;
}
protected function supports($attribute, $subject): bool
{
// if the attribute isn't one we support, return false
if (!in_array($attribute, array(
self::VIEW,
self::VIEW_ANY,
self::CREATE,
self::EDIT,
self::EDIT_ANY,
self::DELETE,
), true)) {
return false;
}
// only vote on Property objects inside this voter
if ($subject && !$subject instanceof BlogCategory) {
return false;
}
return true;
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
{
if ($this->decisionManager->decide($token, array('ROLE_SUPER_ADMIN'))) {
return true;
}
$user = $token->getUser();
if (!$user instanceof UserInterface) {
// the user must be logged in; if not, deny access
return false;
}
switch ($attribute) {
case self::VIEW_ANY:
return $this->canViewAny($user);
case self::VIEW:
return $this->canView($subject, $user);
case self::CREATE:
return $this->canCreate($user);
case self::EDIT:
return $this->canEdit($subject, $user);
case self::EDIT_ANY:
return $this->canEditAny($subject, $user);
case self::DELETE:
return $this->canDelete($subject, $user);
}
throw new \LogicException('This code should not be reached!');
}
/**
* Check if logged in User can view Property
*/
private function canView(UserInterface $subject, UserInterface $user): bool
{
if ($this->canEdit($subject, $user)) {
return true;
}
if ($this->isOwner($subject, $user)) {
return true;
}
return false;
}
/**
* Check if logged in User can view Property
*/
private function canViewAny(UserInterface $user): bool
{
if ($user->hasRight(self::VIEW_ANY)) {
return true;
}
return false;
}
/**
* Check if logged in User can create Property
*/
private function canCreate(UserInterface $user): bool
{
if ($user->hasRight(self::CREATE)) {
return true;
}
return $user->hasRole('ROLE_ADMIN');
}
/**
* Check if logged in User can edit Property
*/
private function canEdit(UserInterface $user): bool
{
if ($user->hasRight(self::EDIT)) {
return true;
}
return false;
}
/**
* Check if logged in User can print Property
*/
private function canEditAny(UserInterface $subject, UserInterface $user): bool
{
if ($user->hasRight(self::EDIT_ANY)) {
return true;
}
if ($this->isOwner($subject, $user)) {
return true;
}
return false;
}
/**
* Check if logged in User can delete Property
*/
private function canDelete(UserInterface $subject, UserInterface $user): bool
{
if ($user->hasRight(self::DELETE)) {
return true;
}
if ($this->isOwner($subject, $user)) {
return true;
}
return false;
}
/**
* Check if User if Owner of Subject/Property
*/
private function isOwner(UserInterface $subject, UserInterface $user): bool
{
return $user->getId() === $subject->getId();
}
}