Skip to content

Commit 3b50e81

Browse files
committed
feature #1057 [Voter] Refactor attributes (mdoutreluingne)
This PR was squashed before being merged into the 1.0-dev branch. Discussion ---------- [Voter] Refactor attributes This fixes #1051 this refactoring improves the code and makes it more maintainable in the future after the class has been generated by the command `make:voter`. Commits ------- 7b80ff9 [Voter] Refactor attributes
2 parents 164989f + 7b80ff9 commit 3b50e81

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

src/Resources/skeleton/security/Voter.tpl.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@
88

99
class <?= $class_name ?> extends Voter
1010
{
11+
public const EDIT = 'POST_EDIT';
12+
public const VIEW = 'POST_VIEW';
13+
1114
protected function supports(<?= $use_type_hints ? 'string ' : null ?>$attribute, $subject): bool
1215
{
1316
// replace with your own logic
1417
// https://symfony.com/doc/current/security/voters.html
15-
return in_array($attribute, ['POST_EDIT', 'POST_VIEW'])
18+
return in_array($attribute, [self::EDIT, self::VIEW])
1619
&& $subject instanceof \App\Entity\<?= str_replace('Voter', null, $class_name) ?>;
1720
}
1821

@@ -26,11 +29,11 @@ protected function voteOnAttribute(<?= $use_type_hints ? 'string ' : null ?>$att
2629

2730
// ... (check conditions and return true to grant permission) ...
2831
switch ($attribute) {
29-
case 'POST_EDIT':
32+
case self::EDIT:
3033
// logic to determine if the user can EDIT
3134
// return true or false
3235
break;
33-
case 'POST_VIEW':
36+
case self::VIEW:
3437
// logic to determine if the user can VIEW
3538
// return true or false
3639
break;

0 commit comments

Comments
 (0)