[keycloak-user] How to assign client roles to realm admin user programmatically

Stan Silvert ssilvert at redhat.com
Mon May 23 10:10:55 EDT 2016


Assigning roles with the admin client is rather tricky.  I just finished 
migrating a test where I had to assign all kinds of roles.  I think 
you'll probably find what you are looking for:
https://github.com/keycloak/keycloak/blob/master/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/composites/CompositeRoleTest.java

On 5/23/2016 9:58 AM, Haim Vana wrote:
>
> Hi,
>
> I am trying to create admin user for a specific realm 
> programmatically, I am able to create the user, however I can't assign 
> the realm client roles to it.
>
> For example in the UI I would go to the user 'Role Mappings'  choose 
> the realm client role and move the required roles from the Available 
> section to the Assigned.
>
> When I try to it programmatically I am getting 404, my code is below, 
> note that I am getting 404 on the last line - 
> adminUserClientRole.listAvailable())
>
> createUserAndPsw(keyCloakClient, *"master"*, user);
> RealmResource realm = keyCloakClient.realm(*"master"*);
> UserResource userResource = realm.users().get(user.getKeyCloakId());
> RoleMappingResource roles = userResource.roles();
> RoleScopeResource adminUserClientRole = roles.clientLevel(tenantId + 
> *"-realm"*);
>
>
> adminUserClientRole.add(adminUserClientRole.listAvailable());
>
> Any advice will be appreciated.
>
> Thanks,
>
> Haim.
>
> The information contained in this message is proprietary to the 
> sender, protected from disclosure, and may be privileged. The 
> information is intended to be conveyed only to the designated 
> recipient(s) of the message. If the reader of this message is not the 
> intended recipient, you are hereby notified that any dissemination, 
> use, distribution or copying of this communication is strictly 
> prohibited and may be unlawful. If you have received this 
> communication in error, please notify us immediately by replying to 
> the message and deleting it from your computer. Thank you.
>
>
> _______________________________________________
> keycloak-user mailing list
> keycloak-user at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/keycloak-user

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/keycloak-user/attachments/20160523/94682d10/attachment.html 


More information about the keycloak-user mailing list