Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7375 from yguedidi/use-isgranted-in-entrycontroller
Use IsGranted in EntryController
- Loading branch information
Showing
16 changed files
with
575 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
namespace Wallabag\Security\Voter; | ||
|
||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; | ||
use Symfony\Component\Security\Core\Authorization\Voter\Voter; | ||
use Wallabag\Entity\Entry; | ||
use Wallabag\Entity\User; | ||
|
||
class EntryVoter extends Voter | ||
{ | ||
public const VIEW = 'VIEW'; | ||
public const EDIT = 'EDIT'; | ||
public const RELOAD = 'RELOAD'; | ||
public const STAR = 'STAR'; | ||
public const ARCHIVE = 'ARCHIVE'; | ||
public const SHARE = 'SHARE'; | ||
public const UNSHARE = 'UNSHARE'; | ||
public const DELETE = 'DELETE'; | ||
|
||
protected function supports(string $attribute, $subject): bool | ||
{ | ||
if (!$subject instanceof Entry) { | ||
return false; | ||
} | ||
|
||
if (!\in_array($attribute, [self::VIEW, self::EDIT, self::RELOAD, self::STAR, self::ARCHIVE, self::SHARE, self::UNSHARE, self::DELETE], true)) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool | ||
{ | ||
\assert($subject instanceof Entry); | ||
|
||
$user = $token->getUser(); | ||
|
||
if (!$user instanceof User) { | ||
return false; | ||
} | ||
|
||
switch ($attribute) { | ||
case self::VIEW: | ||
case self::EDIT: | ||
case self::RELOAD: | ||
case self::STAR: | ||
case self::ARCHIVE: | ||
case self::SHARE: | ||
case self::UNSHARE: | ||
case self::DELETE: | ||
return $user === $subject->getUser(); | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 Symfony\Component\Security\Core\Security; | ||
|
||
class MainVoter extends Voter | ||
{ | ||
public const LIST_ENTRIES = 'LIST_ENTRIES'; | ||
public const CREATE_ENTRIES = 'CREATE_ENTRIES'; | ||
public const EDIT_ENTRIES = 'EDIT_ENTRIES'; | ||
|
||
private Security $security; | ||
|
||
public function __construct(Security $security) | ||
{ | ||
$this->security = $security; | ||
} | ||
|
||
protected function supports(string $attribute, $subject): bool | ||
{ | ||
if (null !== $subject) { | ||
return false; | ||
} | ||
|
||
if (!\in_array($attribute, [self::LIST_ENTRIES, self::CREATE_ENTRIES, self::EDIT_ENTRIES], true)) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool | ||
{ | ||
switch ($attribute) { | ||
case self::LIST_ENTRIES: | ||
case self::CREATE_ENTRIES: | ||
case self::EDIT_ENTRIES: | ||
return $this->security->isGranted('ROLE_USER'); | ||
} | ||
|
||
return false; | ||
} | ||
} |
Oops, something went wrong.