From 7b80ff941cb2add284dd5a89c32c5dacc53ba644 Mon Sep 17 00:00:00 2001 From: Maxime Doutreluingne Date: Mon, 24 Jan 2022 17:44:15 +0100 Subject: [PATCH] [Voter] Refactor attributes --- src/Resources/skeleton/security/Voter.tpl.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Resources/skeleton/security/Voter.tpl.php b/src/Resources/skeleton/security/Voter.tpl.php index 1ff4f731f..d012bec09 100644 --- a/src/Resources/skeleton/security/Voter.tpl.php +++ b/src/Resources/skeleton/security/Voter.tpl.php @@ -8,11 +8,14 @@ class extends Voter { + public const EDIT = 'POST_EDIT'; + public const VIEW = 'POST_VIEW'; + protected function supports($attribute, $subject): bool { // replace with your own logic // https://symfony.com/doc/current/security/voters.html - return in_array($attribute, ['POST_EDIT', 'POST_VIEW']) + return in_array($attribute, [self::EDIT, self::VIEW]) && $subject instanceof \App\Entity\; } @@ -26,11 +29,11 @@ protected function voteOnAttribute($att // ... (check conditions and return true to grant permission) ... switch ($attribute) { - case 'POST_EDIT': + case self::EDIT: // logic to determine if the user can EDIT // return true or false break; - case 'POST_VIEW': + case self::VIEW: // logic to determine if the user can VIEW // return true or false break;