Skip to content

cleanup: use roles constants #1404

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 16, 2023

Conversation

COil
Copy link
Contributor

@COil COil commented Feb 16, 2023

Before we had to hard-code roles strings:

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;

class PostController extends Controller
{
    /**
     * @IsGranted("ROLE_ADMIN")
     *
     * or use @Security for more flexibility:
     *
     * @Security("is_granted('ROLE_ADMIN') and is_granted('ROLE_FRIENDLY_USER')")
     */
    public function index()
    {
        // ...
    }
}

But with attributes, can use constants. I find this cleaner. I have already used this on several projects and I didn't find drawbacks.

@COil COil force-pushed the chore/use-roles-constants branch from c6dcbcd to 6da7f01 Compare February 16, 2023 21:10
@javiereguiluz
Copy link
Member

Could we use an Enum instead of constants? Would Enums work everywhere? Is it worth it or is it overengineering?

@stof
Copy link
Member

stof commented Feb 17, 2023

No we cannot. Roles are strings in Symfony, not an enum object.

@COil COil force-pushed the chore/use-roles-constants branch from 6da7f01 to 1a27dce Compare February 17, 2023 18:03
@COil COil requested review from OskarStark and xabbuh and removed request for OskarStark February 17, 2023 18:05
@COil COil force-pushed the chore/use-roles-constants branch from 1a27dce to b31c0ae Compare February 18, 2023 09:36
@wouterj
Copy link
Member

wouterj commented Feb 18, 2023

You can use e.g. RoleEnum::USER->value (since PHP 8.1) in attributes and php code, bit IIRC not in Yaml.

@COil
Copy link
Contributor Author

COil commented Feb 18, 2023

You can use e.g. RoleEnum::USER->value (since PHP 8.1) in attributes and php code, bit IIRC not in Yaml.

With PHP 8.2 it will be OK, but we can't yet with 8.1:

Capture d’écran 2023-02-18 à 11 03 57

@COil COil requested review from OskarStark and removed request for xabbuh February 20, 2023 14:27
@COil
Copy link
Contributor Author

COil commented Feb 25, 2023

Hello, is it OK for this PR? Or should we close it?

@javiereguiluz
Copy link
Member

My opinion:

  • 🟢 I like a lot using these constants in PHP files
  • 🟡 I'm not entirely sure about injecting these constants in all Twig templates as global constants
  • 🔴 I don't like how complicated this looks in the YAML file, especially in the security.yaml file

What do others think about this? Thanks.

@derrabus
Copy link
Member

I fully agree with @javiereguiluz' assessment.

🟢 I like a lot using these constants in PHP files

Me too, I wouldn't make those constants members of the User class though. In my projects, I usually have a final class Roles that holds those constants.

🟡 I'm not entirely sure about injecting these constants in all Twig templates as global constants

Me neither and I wouldn't do it tbh. We don't gain much here.

🔴 I don't like how complicated this looks in the YAML file, especially in the security.yaml file

Same. I would revert this as well.

@stof
Copy link
Member

stof commented Mar 13, 2023

Sure you can workaround the bad architecture by using RoleEnum::USER->value everywhere. But I would not recommend doing that in the demo as this would give the impression that using a enum for roles is a good idea, while this is an abuse of enums based on the fact that you need role strings anyway.

@javiereguiluz
Copy link
Member

@stof but in this case we're not talking about enums. It's using PHP constants in YAML that looks too complicated to us. E.g. this change:

     role_hierarchy:
-        ROLE_ADMIN: ROLE_USER
+        !php/const App\Entity\User::ROLE_ADMIN: !php/const App\Entity\User::ROLE_USER

But in PHP files these constants look very nice.

@COil
Copy link
Contributor Author

COil commented Mar 13, 2023

Me too, I wouldn't make those constants members of the User class though. In my projects, I usually have a final class Roles that holds those constants.

But that looks weird to put this in a separated class. In this case, we should use an enum (but we don't want too here). Having these constants in the User class is the more pragmatic way, IMHO.

🟡 I'm not entirely sure about injecting these constants in all Twig templates as global constants
Me neither and I wouldn't do it tbh. We don't gain much here.

Actually, if we don't want to use a constant here, we could use a twig extension to do the job and make use of the PHP constant. But that's not as simple as using is_granted indeed.

I remember now, that was the opportunity to show the usage of the Twig globals.

🔴 I don't like how complicated this looks in the YAML file, especially in the security.yaml file

I agree, it makes the YAML unreadable. I didn't even know it was possible to do this, so let's revert this. It was a try.

@COil COil force-pushed the chore/use-roles-constants branch from 0f992ac to 1c40029 Compare March 13, 2023 20:32
@derrabus
Copy link
Member

But that looks weird to put this in a separated class.

🤨 🤷🏻‍♂️

@COil COil force-pushed the chore/use-roles-constants branch from 1c40029 to 5b74809 Compare March 14, 2023 06:27
Copy link
Member

@javiereguiluz javiereguiluz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this proposal a lot. Thanks Loïc!

@javiereguiluz
Copy link
Member

It's merged now. Thanks!

@javiereguiluz javiereguiluz force-pushed the chore/use-roles-constants branch from 5b74809 to f057af8 Compare March 16, 2023 14:29
@javiereguiluz javiereguiluz merged commit 04c1798 into symfony:main Mar 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants