Skip to content
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

[Request feature] Allow bool val for setPermission() methods #6444

Open
bastien70 opened this issue Sep 13, 2024 · 0 comments
Open

[Request feature] Allow bool val for setPermission() methods #6444

bastien70 opened this issue Sep 13, 2024 · 0 comments

Comments

@bastien70
Copy link
Contributor

bastien70 commented Sep 13, 2024

Short description of what this feature will allow to do:
Currently, when configuring a permission for a MenuItem, an action, a crud, or a field, the setPermission() method expects a string or an expression. It would be interesting to also accept a boolean based on conditions

Example of how to use this feature

yield MenuItem::linkToCrud('Administrateurs', 'bi bi-shield-lock', User::class)
            ->setController(AdminCrudController::class)
            ->setPermission($this->isGranted(UserRole::ROLE_SUPER_ADMIN->value) || $this->isGranted(UserRole::ROLE_ENTREPRISE->value));

Here, the permission is validated if the user has the ROLE_SUPER_ADMIN role or the ROLE_ENTERPRISE role.

Here is an example with roles, but it could be something else.

The main reason for this request is that it forces us to create a Voter on purpose for this, which is disruptive.
Or the alternative is currently something like this :

yield MenuItem::linkToCrud('Administrateurs', 'bi bi-shield-lock', User::class)
            ->setController(AdminCrudController::class)
            ->setPermission(new Expression("is_granted('".UserRole::ROLE_SUPER_ADMIN->value."') or is_granted('".UserRole::ROLE_ENTREPRISE->value."')"));

But it's not practical.

Likewise, from CRUD, it will be useful to be able to do the same thing in the configureCrud() method :

$crud->setPermission($this->isGranted(UserRole::ROLE_SUPER_ADMIN->value) || $this->isGranted(UserRole::ROLE_ENTREPRISE->value));

EDIT :

I created a helper which returns me an expression, conforming to my expectations, if it can help some people :

<?php

namespace App\Helper;

use Symfony\Component\ExpressionLanguage\Expression;

class EasyAdminPermissionHelper
{
    /**
     * Returns an Expression that checks if the user has any of the given roles.
     *
     * @param array $roles Array of role strings
     * @return Expression
     */
    public static function isGrantedAny(array $roles): Expression
    {
        $expressions = array_map(
            fn($role) => "is_granted('{$role}')",
            $roles
        );

        $expressionString = implode(' or ', $expressions);

        return new Expression($expressionString);
    }

    /**
     * Returns an Expression that checks if the user has all of the given roles.
     *
     * @param array $roles Array of role strings
     * @return Expression
     */
    public static function isGrantedAll(array $roles): Expression
    {
        $expressions = array_map(
            fn($role) => "is_granted('{$role}')",
            $roles
        );

        $expressionString = implode(' and ', $expressions);

        return new Expression($expressionString);
    }
}

And now :

yield MenuItem::linkToCrud('Administrateurs', 'bi bi-shield-lock', User::class)
            ->setController(AdminCrudController::class)
            ->setPermission(EasyAdminPermissionHelper::isGrantedAny([UserRole::ROLE_SUPER_ADMIN->value, UserRole::ROLE_ENTREPRISE->value]));
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

No branches or pull requests

1 participant