<?php
namespace Crea\DocumentationBundle\Voter;
use Crea\DocumentationBundle\Entity\Article;
use Crea\DocumentationBundle\Entity\Chapter;
use Crea\DocumentationBundle\Provider\DocumentationRightCodeProvider;
use Exception;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\User\UserInterface;
class DocumentationVoter extends Voter
{
protected DocumentationRightCodeProvider $documentationRightCodeProvider;
public function __construct(DocumentationRightCodeProvider $documentationRightCodeProvider)
{
$this->documentationRightCodeProvider = $documentationRightCodeProvider;
}
/**
* @inheritDoc
* @throws Exception
*/
protected function supports($attribute, $subject): bool
{
$chapterAttributes = [$this->documentationRightCodeProvider->getMainChapterCreateRightCode()];
if ($subject instanceof Chapter) {
$chapterAttributes[] = $this->documentationRightCodeProvider->getChapterViewRightCode($subject);
$chapterAttributes[] = $this->documentationRightCodeProvider->getChapterCreateRightCode($subject);
$chapterAttributes[] = $this->documentationRightCodeProvider->getChapterUpdateRightCode($subject);
$chapterAttributes[] = $this->documentationRightCodeProvider->getChapterRemoveRightCode($subject);
$chapterAttributes[] = $this->documentationRightCodeProvider->getArticleCreateRightCode($subject);
}
if ($subject instanceof Article) {
$chapterAttributes[] = $this->documentationRightCodeProvider->getArticleUpdateRightCode($subject);
$chapterAttributes[] = $this->documentationRightCodeProvider->getArticleRemoveRightCode($subject);
}
return in_array($attribute, $chapterAttributes);
}
/**
* @inheritDoc
* @throws Exception
*/
protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
{
/** @var UserInterface $loggedUser */
$loggedUser = $token->getUser();
if (!$loggedUser instanceof UserInterface) {
return false;
}
if ($attribute === $this->documentationRightCodeProvider->getMainChapterCreateRightCode()) {
return $this->voteOnMainChapterCreate($loggedUser);
}
if ($subject instanceof Chapter) {
switch ($attribute) {
case $this->documentationRightCodeProvider->getChapterViewRightCode($subject):
return $this->voteOnChapterView($subject, $loggedUser);
case $this->documentationRightCodeProvider->getChapterCreateRightCode($subject):
return $this->voteOnChapterCreate($subject, $loggedUser);
case $this->documentationRightCodeProvider->getChapterUpdateRightCode($subject):
return $this->voteOnChapterUpdate($subject, $loggedUser);
case $this->documentationRightCodeProvider->getChapterRemoveRightCode($subject):
return $this->voteOnChapterRemove($subject, $loggedUser);
case $this->documentationRightCodeProvider->getArticleCreateRightCode($subject):
return $this->voteOnArticleCreate($subject, $loggedUser);
}
}
if ($subject instanceof Article) {
switch ($attribute) {
case $this->documentationRightCodeProvider->getArticleUpdateRightCode($subject):
return $this->voteOnArticleUpdate($subject, $loggedUser);
case $this->documentationRightCodeProvider->getArticleRemoveRightCode($subject):
return $this->voteOnArticleRemove($subject, $loggedUser);
}
}
return false;
}
private function voteOnMainChapterCreate(UserInterface $loggedUser): bool
{
return in_array($this->documentationRightCodeProvider->getMainChapterCreateRightCode(), $loggedUser->getRoles());
}
/**
* @throws Exception
*/
private function voteOnChapterView(Chapter $chapter, UserInterface $loggedUser): bool
{
return in_array($this->documentationRightCodeProvider->getChapterViewRightCode($chapter), $loggedUser->getRoles());
}
/**
* @throws Exception
*/
private function voteOnChapterCreate(Chapter $chapter, UserInterface $loggedUser): bool
{
return in_array($this->documentationRightCodeProvider->getChapterCreateRightCode($chapter), $loggedUser->getRoles());
}
/**
* @throws Exception
*/
private function voteOnChapterUpdate(Chapter $chapter, UserInterface $loggedUser): bool
{
return in_array($this->documentationRightCodeProvider->getChapterUpdateRightCode($chapter), $loggedUser->getRoles());
}
/**
* @throws Exception
*/
private function voteOnChapterRemove(Chapter $chapter, UserInterface $loggedUser): bool
{
return in_array($this->documentationRightCodeProvider->getChapterRemoveRightCode($chapter), $loggedUser->getRoles());
}
/**
* @throws Exception
*/
private function voteOnArticleCreate(Chapter $chapter, UserInterface $loggedUser): bool
{
return in_array($this->documentationRightCodeProvider->getArticleCreateRightCode($chapter), $loggedUser->getRoles());
}
/**
* @throws Exception
*/
private function voteOnArticleUpdate(Article $article, UserInterface $loggedUser): bool
{
return in_array($this->documentationRightCodeProvider->getArticleUpdateRightCode($article), $loggedUser->getRoles());
}
/**
* @throws Exception
*/
private function voteOnArticleRemove(Article $article, UserInterface $loggedUser): bool
{
return in_array($this->documentationRightCodeProvider->getArticleRemoveRightCode($article), $loggedUser->getRoles());
}
}