Replies: 6 comments 19 replies
-
Well, enums are meant to represent a closed list of values. In the security system, the list of permission attributes is unknown. It is an open list. So enums are not suited for that. |
Beta Was this translation helpful? Give feedback.
-
I am using Roles as Enums, because it's finite list of values. But I find it very strange that up until But when getting voter, it suddenly starts to work with only strings. At least accept Now I have to do |
Beta Was this translation helpful? Give feedback.
-
I would like to get some clarification on this issue. I would argue that most real life applications out there will have any form of constant holder for Roles / Permissions / similar tokens. When taking into considerations that database also holds values like this, then having them stored as an enum makes a hell of a lot of sense. I see absolutely no harm in supporting backedEnums for the isGranted functionality. NOT doing so, honestly just promotes userland code errors. Why the hard stance against this? |
Beta Was this translation helpful? Give feedback.
-
What about using BackedEnum::tryFrom #[IsGranted(attribute: Permission::SOMETHING->value)] protected function supports(string $attribute, mixed $subject): bool
{
$user = $this->security->getUser();
if (!$user instanceof UserInterface) {
return false;
}
return Permission::tryFrom($attribute) instanceof Permission;
} |
Beta Was this translation helpful? Give feedback.
-
I am by no means a reference is anything symfony-related but here are my two cents on this topic nonetheless. I honestly don't see how allowing a I.e, given a Role enum defined as the following :
passing However I don't think forcing people to go through hoops by using an imposed string format with And forcing people to use constants instead of enums would definitely go against the exact argument used earlier : roles are supposed to be a closed list. Also, simply allowing All it would take is one line of code to automatically retrieve the value property if the passed element is a StringBackedEnum... |
Beta Was this translation helpful? Give feedback.
-
Is this implemented now?
in Coding principles shouldnt undermine the dev experience. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
It can be nice to have the full support of Enums for security voters and maybe roles too. For now, in most places
$attribute
parameter is not type-hinted ormixed
type is used depending on Symfony version, buthere
symfony/src/Symfony/Component/Security/Core/Authorization/Voter/Voter.php
Line 82 in 9cbc853
and here
symfony/src/Symfony/Component/Security/Core/Authorization/Voter/Voter.php
Line 88 in 9cbc853
$attribute
should be string, and multiple voter implementations requires attribute to bestring
which not works if you pass Enum as attribute.Beta Was this translation helpful? Give feedback.
All reactions