From bcc6eae6c9caf64274d0916f4b801c0480733dc7 Mon Sep 17 00:00:00 2001 From: Bart Monhemius Date: Fri, 30 Jul 2021 09:51:23 +0200 Subject: [PATCH] Allow searching users by custom attributes (#195) * Allow searching users by custom attributes This will allow searching users by custom attributes. The attributes will be set as query parameters. This depends on https://github.com/keycloak/keycloak/pull/8329 * Allow numbers in custom attributes This is required for typescript to accept paging parameters --- src/resources/users.ts | 2 +- test/users.spec.ts | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/resources/users.ts b/src/resources/users.ts index 97be5ee2..93f69f3e 100644 --- a/src/resources/users.ts +++ b/src/resources/users.ts @@ -23,7 +23,7 @@ export interface UserQuery { } export class Users extends Resource<{realm?: string}> { - public find = this.makeRequest({ + public find = this.makeRequest({ method: 'GET', }); diff --git a/test/users.spec.ts b/test/users.spec.ts index 5c1988ad..1bf4721e 100644 --- a/test/users.spec.ts +++ b/test/users.spec.ts @@ -33,6 +33,9 @@ describe('Users', function () { // enabled required to be true in order to send actions email emailVerified: true, enabled: true, + attributes: { + key: 'value', + }, }); expect(user.id).to.be.ok; @@ -91,6 +94,17 @@ describe('Users', function () { } }); + it('find users by custom attributes', async function() { + // Searching by attributes is only available from Keycloak > 15 + if (process.env.KEYCLOAK_VERSION && process.env.KEYCLOAK_VERSION.startsWith('15.')) { + const users = await kcAdminClient.users.find({key: 'value'}); + expect(users.length).to.be.equal(1); + expect(users[0]).to.be.deep.include(currentUser); + } else { + this.skip(); + } + }); + it('get single users', async () => { const userId = currentUser.id; const user = await kcAdminClient.users.findOne({