GET Users filter on role name

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bewisesolutions
    Junior Member
    • Apr 2023
    • 16

    GET Users filter on role name

    Hi,

    I am using the user API with https://docs.espocrm.com/development/api-client-php/

    However I am not able to retrieve all users filtered on a certain role name.

    The user id: 65e98f9b2baf8b39e is linked to a role "Adviseur"

    "id": "65e98f9b2baf8b39e",
    "name": "adviseur_bouw",
    "userName": "adviseur_bouw",
    "type": "regular",
    "isActive": true,
    "rolesIds": [
    "660e7aa619df793c4",
    "660e7bc19d7f005ec"
    ],
    "rolesNames": {
    "660e7aa619df793c4": "Adviseur",
    "660e7bc19d7f005ec": "Werelds beheerder"
    },

    Now I like to filter on all users which are linked to the role "Adviseur"

    I tried a lot of things but this is the latest trial:

    ...
    $where = [
    [
    'type' => 'arrayAnyOf',
    'attribute' => 'rolesNames',
    'value' => ['Adviseur'],
    ],
    [
    'type' => 'equals',
    'attribute' => 'id',
    'value' => '65e98f9b2baf8b39e',
    ],
    ];

    $response = $client->request( Client::METHOD_GET, 'user', [
    'offset' => 0
    ,'select' => 'rolesNames'
    ,'maxSize' => 100
    ,'where' => $where
    ] );
    $parsedBody = $response->getParsedBody();
    var_dump($parsedBody);die();​

    ..

    But now response

    (note: If I remove the filter on roleNames I get the user​​)

    Does anyone have an idea how I can filter on rolename?

    Thanks
  • yuri
    Member
    • Mar 2014
    • 8455

    #2
    Maybe filter by the role ID instead.

    Add "Roles" filter to the User's Search layout. Search by the Role and see what API request is sent by the browser (F12 > Network tab).
    If you find EspoCRM good, we would greatly appreciate if you could give the project a star on GitHub. We believe our work truly deserves more recognition. Thanks.

    Comment

    • bewisesolutions
      Junior Member
      • Apr 2023
      • 16

      #3
      Hi Yuri,

      Thanks for this tip. Got me a step further. However I get a 403 error when I do exactly the same request through the the api (with my api user)


      Message Forbidden relation 'roles' in where.


      ... /api/v1/user?offset=0&maxSize=200&where%5B0%5D%5Btype%5D=l inkedWith&where%5B0%5D%5Battribute%5D=roles&where% 5B0%5D%5Bvalue%5D%5B0%5D=660e7aa619df793c4

      I have enabled all rights (in the role) for the api user, so how can I set the permissions for the api user to get the users filtered by role?

      Thanks

      By the way:
      I made an issue for the php client: https://github.com/espocrm/php-espo-api-client/issues/1
      However it looks like this git is not updated for 2 years now. Should I still use this php client?

      Comment

      Working...