<?php
namespace DW\GingerBundle\Security;
use DW\GingerBundle\Entity\GroupInterface;
use DW\GingerBundle\Entity\TagInterface;
use DW\GingerBundle\Entity\User;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
class TagVoter extends Voter
{
const VIEW = 'view';
const EDIT = 'edit';
const CREATE = 'create';
const DELETE = 'delete';
const USE = 'use';
private AccessDecisionManagerInterface $decisionManager;
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::EDIT, self::CREATE, self::DELETE, self::USE))) {
return false;
}
// only vote on tag objects inside this voter
if (!$subject instanceof TagInterface) {
return false;
}
return true;
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
if (!$user instanceof User) {
// the user must be logged in; if not, deny access
return false;
}
/** @var Tag $tag */
$tag = $subject;
switch ($attribute) {
case self::VIEW:
return $this->canView($tag, $token);
case self::EDIT:
return $this->canEdit($tag, $token);
case self::CREATE:
return $this->canCreate($tag, $token);
case self::DELETE:
return $this->canDelete($tag, $token);
case self::USE:
return $this->canUse($tag, $token);
}
throw new \LogicException('This code should not be reached!');
}
private function canCreate(TagInterface $tag, TokenInterface $token): bool
{
if ($this->decisionManager->decide($token, ['ROLE_GINGER_TAG_CREATE'])) {
return true;
}
return false;
}
private function canView(TagInterface $tag, TokenInterface $token): bool
{
// Everyone can view tags
return true;
}
private function canEdit(TagInterface $tag, TokenInterface $token): bool
{
if (!$this->decisionManager->decide($token, ['ROLE_GINGER_TAG_EDIT'])) {
return false;
}
$user = $token->getUser();
// "God users" have no groups
if ($user->getGroups()->isEmpty()) {
return true;
}
// Users groups must have the tag
/**@var GroupInterface $group */
foreach ($user->getGroups() as $group) {
if ($group->getTags()->contains($tag)) {
return true;
}
}
return false;
}
private function canDelete(TagInterface $tag, TokenInterface $token): bool
{
if ($this->canEdit($tag, $token) && $this->decisionManager->decide($token, ['ROLE_GINGER_TAG_DELETE'])) {
return true;
}
return false;
}
private function canUse(TagInterface $tag, TokenInterface $token): bool
{
// everyone may use general tags
if ($tag->getType() === TagInterface::TYPE_GENERAL) {
return true;
}
$user = $token->getUser();
// "God users" have no groups
if ($user->getGroups()->isEmpty()) {
return true;
}
// Special usecase: Editors may use special tags when merging duplicates.
// https://mantis.dwerk.at/view.php?id=29636
if ($this->decisionManager->decide($token, ['ROLE_GINGER_DUPLICATE_CONTROLLER'])) {
return true;
}
// Users groups must have the tag
/**@var GroupInterface $group */
foreach ($user->getGroups() as $group) {
if ($group->getTags()->contains($tag)) {
return true;
}
}
return false;
}
}