EasyAdminBundle icon indicating copy to clipboard operation
EasyAdminBundle copied to clipboard

Using php8 enums cause TypeError

Open sadikoff opened this issue 2 years ago • 3 comments

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.

sadikoff avatar Apr 12 '22 08:04 sadikoff

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

jmsche avatar Apr 21 '22 13:04 jmsche

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

sadikoff avatar Apr 21 '22 18:04 sadikoff

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.

tsantos84 avatar Aug 03 '22 00:08 tsantos84