forked from wallabag/wallabag
Add IsGranted to AnnotationController
This commit is contained in:
46
src/Security/Voter/AnnotationVoter.php
Normal file
46
src/Security/Voter/AnnotationVoter.php
Normal file
@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace Wallabag\Security\Voter;
|
||||
|
||||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
|
||||
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
|
||||
use Wallabag\Entity\Annotation;
|
||||
use Wallabag\Entity\User;
|
||||
|
||||
class AnnotationVoter extends Voter
|
||||
{
|
||||
public const EDIT = 'EDIT';
|
||||
public const DELETE = 'DELETE';
|
||||
|
||||
protected function supports(string $attribute, $subject): bool
|
||||
{
|
||||
if (!$subject instanceof Annotation) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!\in_array($attribute, [self::EDIT, self::DELETE], true)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
|
||||
{
|
||||
\assert($subject instanceof Annotation);
|
||||
|
||||
$user = $token->getUser();
|
||||
|
||||
if (!$user instanceof User) {
|
||||
return false;
|
||||
}
|
||||
|
||||
switch ($attribute) {
|
||||
case self::EDIT:
|
||||
case self::DELETE:
|
||||
return $subject->getUser() === $user;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@ -17,6 +17,8 @@ class EntryVoter extends Voter
|
||||
public const SHARE = 'SHARE';
|
||||
public const UNSHARE = 'UNSHARE';
|
||||
public const DELETE = 'DELETE';
|
||||
public const LIST_ANNOTATIONS = 'LIST_ANNOTATIONS';
|
||||
public const CREATE_ANNOTATIONS = 'CREATE_ANNOTATIONS';
|
||||
|
||||
protected function supports(string $attribute, $subject): bool
|
||||
{
|
||||
@ -24,7 +26,7 @@ class EntryVoter extends Voter
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!\in_array($attribute, [self::VIEW, self::EDIT, self::RELOAD, self::STAR, self::ARCHIVE, self::SHARE, self::UNSHARE, self::DELETE], true)) {
|
||||
if (!\in_array($attribute, [self::VIEW, self::EDIT, self::RELOAD, self::STAR, self::ARCHIVE, self::SHARE, self::UNSHARE, self::DELETE, self::LIST_ANNOTATIONS, self::CREATE_ANNOTATIONS], true)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -50,6 +52,8 @@ class EntryVoter extends Voter
|
||||
case self::SHARE:
|
||||
case self::UNSHARE:
|
||||
case self::DELETE:
|
||||
case self::LIST_ANNOTATIONS:
|
||||
case self::CREATE_ANNOTATIONS:
|
||||
return $user === $subject->getUser();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user