Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
Allow searching users by custom attributes (#195)
Browse files Browse the repository at this point in the history
* 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 keycloak/keycloak#8329

* Allow numbers in custom attributes

This is required for typescript to accept paging parameters
  • Loading branch information
bartmentech authored Jul 30, 2021
1 parent 89956b1 commit bcc6eae
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/resources/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export interface UserQuery {
}

export class Users extends Resource<{realm?: string}> {
public find = this.makeRequest<UserQuery, UserRepresentation[]>({
public find = this.makeRequest<UserQuery & {[key: string]: string | number}, UserRepresentation[]>({
method: 'GET',
});

Expand Down
14 changes: 14 additions & 0 deletions test/users.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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({
Expand Down

0 comments on commit bcc6eae

Please sign in to comment.