Use IsGranted in SiteCredentialController

This commit is contained in:
Yassine Guedidi
2024-03-23 23:36:33 +01:00
parent 96cb024cf5
commit 247894209c
8 changed files with 158 additions and 28 deletions

View 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\SiteCredential;
use Wallabag\Entity\User;
class SiteCredentialVoter extends Voter
{
public const EDIT = 'EDIT';
public const DELETE = 'DELETE';
protected function supports(string $attribute, $subject): bool
{
if (!$subject instanceof SiteCredential) {
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 SiteCredential);
$user = $token->getUser();
if (!$user instanceof User) {
return false;
}
switch ($attribute) {
case self::EDIT:
case self::DELETE:
return $user === $subject->getUser();
}
return false;
}
}