keycloak-schematic icon indicating copy to clipboard operation
keycloak-schematic copied to clipboard

How do I obtain roles of a user

Open everflux opened this issue 6 years ago • 5 comments

I want to be able to render conditionally some content depending on the roles of a user. Could you add an example for that use case? (Coding to roles would implicitly do a role-permission mapping inside the app, but that could be acceptable.)

everflux avatar Mar 21 '18 14:03 everflux

We could add a method to the KeycloakService called "hasRole()" where you just pass in a string and it returns a boolean. Is that what you are looking for? I could add that very soon. You could then do:

The next step would be to create an attribute directive so you could say something like:

ssilvert avatar Mar 21 '18 18:03 ssilvert

That sounds exactly like the solution

everflux avatar Mar 22 '18 15:03 everflux

Hi everflux. I haven't forgotten about you, but I don't know when I will have time to implement the above.

In the mean time, you can call the hasRealmRole() and hasResourceRole() on the underlying Keycloak adapter (client) instance. So you would do something like this:

keycloakService.client().hasRealmRole('myRole'); keycloakService.client().hasResourceRole('myRole', 'myResource');

ssilvert avatar Apr 02 '18 17:04 ssilvert

Yes, that is how I helped myself out, but calling the underlying JS adapter seemed a little like a workaround.

everflux avatar Apr 02 '18 18:04 everflux

There are examples on how to do that in https://github.com/giovannicandido/angular-spa/tree/master/src/auth/directives

You will need access to the underline keycloak adapter

Api docs: https://angular-spa.netlify.com/auth/directives/

You could add or remove dom elements, or add remove css classes (disable by example). This behaviour is configured per directive or global.

giovannicandido avatar Apr 26 '18 12:04 giovannicandido