Search code examples
phpsqlitesymfonydoctrine-ormeasyadmin

Is it possible to filter the user through roles (a JSON array) in EasyAdmin?


I use EasyadminBundle for the Backend of a Symfony application.

I'd like to filter my users through their $roles with 1 table with only the ROLE_WEBMASTER, another on with the ROLE_ADMIN and the final one with the loosers (ROLE_USER).

Unfortunately the $roles member of the User class is an array.

class User implements UserInterface
{
    /**
     * @ORM\Column(type="json")
     */
    private $roles = [];
}

My storage back-end is SQLite.

I tried two solutions:

1. use search queries

- { icon: user-tie, label: admin, entity: User, params: {action: search, query: ROLE_ADMIN}}

do nothing

2. use a DQL filter

webmaster:
    class: App\Entity\User
    label: Webmaster
    list:
        dql_filter: "'ROLE_WEBMASTER' IN entity.sqlRoles()"

with

public function  sqlRoles():string
{
    return implode(', ',$this->getRoles());
}

but it launch the following error

An exception has been thrown during the rendering of a template ("[Syntax Error] line 0, col 65: Error: Expected =, <, <=, <>, >, >=, !=, got 'IN'").


Is there a way to perform this kind of trick or do I need to forget this idea ?


Solution

  • In order to filter a JSON array in EasyadminBundle, it turns out that you don't need to use dql_filter as filters are already built in the bundle.

    First you need to do some preparation in your entity description :

    easy_admin:
        entities:
            User:
                class: App\Entity\User
                list:
                    filters:
                        - { property: roles, type: array }
    

    as the filter is now set up on the entity we can reach it in our menu :

    easy_admin:
        design:
            menu:
                - { icon: user-cog, label: webmaser, entity: User, params: { action: list, filters: { roles: { comparison: like, value: [ROLE_WEBMASTER] } } } }