EasyAdminBundle
EasyAdminBundle copied to clipboard
Using php8 enums cause TypeError
Trying to use PHP8 enums as Security attributes for voters caused the following error
EasyCorp\Bundle\EasyAdminBundle\Security\Permission::exists():
Argument #1 ($permissionName) must be of type ?string, App\Security\Attributes\TeamSecurityAttrribute given,
called in /Users/user/projects/test/vendor/easycorp/easyadmin-bundle/src/Security/SecurityVoter.php on line 35
Steps to reproduce
- Create Enum
enum SecurityAttribute: string {
case CAN_EDIT = 'can_edit';
case CAN_DELETE = 'can_delete';
}
- Use it in any controller
class TestController extends AbstractController
{
#[Route('/test', name: 'test_action')]
public function test(SomeObject $someObject): Response
{
$this->denyAccessUnlessGranted(SecurityAttribute::CAN_EDIT, $someObject);
// rest of code
}
}
In most places inside the Security component $attribute
is not type-hinted or mixed
type-hint used, so in this case, restricting it to be a string is not quite correct.
Actually in the base Voter class the $attribute
parameter is typed: https://github.com/symfony/symfony/blob/6.0/src/Symfony/Component/Security/Core/Authorization/Voter/Voter.php#L82
yeah that can be complex question, because in other places attribute is mixed
so it is some sort of soft-allow to use whatever we want for it, I also opened a discussion about it on Symfony repo
I yet don't understand why enums
can't have __toString
methods. It'd solves this problem by casting it to string internally in the security component.