<?php
namespace App\Security\Voter;
use App\Config;
use App\Entity\BackUser;
use App\Entity\Page;
use App\Utils\VoterHelper;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class PageVoter extends Voter
{
private const SEE_FRONT = 'see-front';
private const SEE = 'see';
private const EDIT = 'edit';
private const PAGE_COMPOSER = 'page-composer';
private const TRASH = 'trash';
private const DELETE = 'delete';
private const DRAFT = 'draft';
private const SUBMIT = 'submit';
private const PUBLISH = 'publish';
private const UN_PUBLISH = 'un-publish';
private const ARCHIVE = 'archive';
private const PREVIEW = 'preview';
/**
* PageVoter constructor.
*/
public function __construct()
{
}
protected function supports(string $attribute, mixed $subject): bool
{
return $subject instanceof Page && in_array($attribute, [self::SEE_FRONT, self::SEE, self::EDIT, self::PAGE_COMPOSER, self::TRASH, self::DELETE, self::DRAFT, self::SUBMIT, self::PUBLISH, self::UN_PUBLISH, self::ARCHIVE, self::PREVIEW], true);
}
protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool
{
$user = $token->getUser();
// the user must be logged in; if not, deny permission
if (!$user instanceof BackUser && self::SEE_FRONT != $attribute) {
return false;
}
switch ($attribute) {
case self::SEE_FRONT:
if (!$subject->isVisible()) {
return false;
}
break;
case self::PAGE_COMPOSER:
if (Config::isTemplateLocked($subject->getTemplate())) {
return false;
}
break;
case self::TRASH:
if (!$subject->isMulti()) {
return false;
}
break;
case self::DELETE:
if (!$subject->isMulti() || Page::STATUS_TRASH != $subject->getStatus()) {
return false;
}
break;
}
return VoterHelper::voteOnAttribute($attribute, $subject, $token, 'page');
}
}