From daea502326bd68e473d2d8ecf65bf829c97d57ed Mon Sep 17 00:00:00 2001 From: SergioCruzA Date: Thu, 10 Oct 2019 18:27:01 -0500 Subject: [PATCH] List users with role in realm (#27) * List users with a role in the realm --- README.md | 1 + src/resources/roles.ts | 8 ++++++++ test/roles.spec.ts | 8 ++++++++ 3 files changed, 17 insertions(+) diff --git a/README.md b/README.md index c8aa2f12..27d14463 100644 --- a/README.md +++ b/README.md @@ -108,6 +108,7 @@ Demo code: https://github.com/keycloak/keycloak-nodejs-admin-client/blob/master/ - Get a role by name (`GET /{realm}/roles/{role-name}`) - Update a role by name (`PUT /{realm}/roles/{role-name}`) - Delete a role by name (`DELETE /{realm}/roles/{role-name}`) +- Get all users in a role by name for the realm (`GET /{realm}/roles/{role-name}/users`) ### [Roles (by ID)](https://www.keycloak.org/docs-api/4.1/rest-api/index.html#_roles_by_id_resource) diff --git a/src/resources/roles.ts b/src/resources/roles.ts index 755b8be6..9ca51324 100644 --- a/src/resources/roles.ts +++ b/src/resources/roles.ts @@ -1,5 +1,6 @@ import Resource from './resource'; import RoleRepresentation from '../defs/roleRepresentation'; +import UserRepresentation from '../defs/userRepresentation'; import {KeycloakAdminClient} from '../client'; export class Roles extends Resource<{realm?: string}> { @@ -45,6 +46,13 @@ export class Roles extends Resource<{realm?: string}> { urlParamKeys: ['name'], }); + public findUsersWithRole = this.makeRequest<{name: string}, UserRepresentation[]>({ + method: 'GET', + path: '/roles/{name}/users', + urlParamKeys: ['name'], + catchNotFound: true, + }); + /** * Roles by id */ diff --git a/test/roles.spec.ts b/test/roles.spec.ts index fd4b9a69..137f3c01 100644 --- a/test/roles.spec.ts +++ b/test/roles.spec.ts @@ -104,4 +104,12 @@ describe('Roles', function() { }); expect(roleDelByName).to.be.null; }); + + it('get users with role by name in realm', async () => { + const users = await this.client.roles.findUsersWithRole({ + name: 'admin', + }); + expect(users).to.be.ok; + expect(users).to.be.an('array'); + }) });