keycloak-schematic
keycloak-schematic copied to clipboard
How do I obtain roles of a user
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.)
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:
That sounds exactly like the solution
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');
Yes, that is how I helped myself out, but calling the underlying JS adapter seemed a little like a workaround.
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.