Java Code Examples for org.keycloak.representations.idm.RoleRepresentation#setName()
The following examples show how to use
org.keycloak.representations.idm.RoleRepresentation#setName() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FineGrainAdminUnitTest.java From keycloak with Apache License 2.0 | 7 votes |
@Test @AuthServerContainerExclude(AuthServer.REMOTE) public void testRealmWithComposites() throws Exception { testingClient.server().run(FineGrainAdminUnitTest::setup5152); try (Keycloak realmClient = AdminClientUtil.createAdminClient(suiteContext.isAdapterCompatTesting(), TEST, "realm-admin", "password", Constants.ADMIN_CLI_CLIENT_ID, null)) { RoleRepresentation composite = new RoleRepresentation(); composite.setName("composite"); composite.setComposite(true); realmClient.realm(TEST).roles().create(composite); composite = adminClient.realm(TEST).roles().get("composite").toRepresentation(); ClientRepresentation client = adminClient.realm(TEST).clients().findByClientId(Constants.REALM_MANAGEMENT_CLIENT_ID).get(0); RoleRepresentation viewUsers = adminClient.realm(TEST).clients().get(client.getId()).roles().get(AdminRoles.CREATE_CLIENT).toRepresentation(); List<RoleRepresentation> composites = new LinkedList<>(); composites.add(viewUsers); realmClient.realm(TEST).rolesById().addComposites(composite.getId(), composites); } }
Example 2
Source File: RealmsConfigurationLoader.java From keycloak with Apache License 2.0 | 6 votes |
private List<RoleRepresentation> convertClientRoleNamesToRepresentation(String clientId, List<String> roles) { LinkedList<RoleRepresentation> result = new LinkedList<>(); Map<String, String> roleIdMap = clientRoleIdMap.get(clientId); if (roleIdMap == null || roleIdMap.isEmpty()) { throw new RuntimeException("No client roles created for clientId: " + clientId); } for (String role: roles) { RoleRepresentation r = new RoleRepresentation(); String id = roleIdMap.get(role); if (id == null) { throw new RuntimeException("No client role created on client '" + clientId + "' for name: " + role); } r.setId(id); r.setName(role); result.add(r); } return result; }
Example 3
Source File: RealmRolesTest.java From keycloak with Apache License 2.0 | 6 votes |
@Test public void updateRole() { RoleRepresentation role = resource.get("role-a").toRepresentation(); role.setName("role-a-new"); role.setDescription("Role A New"); resource.get("role-a").update(role); assertAdminEvents.assertEvent(realmId, OperationType.UPDATE, AdminEventPaths.roleResourcePath("role-a"), role, ResourceType.REALM_ROLE); role = resource.get("role-a-new").toRepresentation(); assertNotNull(role); assertEquals("role-a-new", role.getName()); assertEquals("Role A New", role.getDescription()); assertFalse(role.isComposite()); }
Example 4
Source File: FineGrainAdminUnitTest.java From keycloak with Apache License 2.0 | 6 votes |
@Test public void testMasterRealmWithComposites() throws Exception { RoleRepresentation composite = new RoleRepresentation(); composite.setName("composite"); composite.setComposite(true); adminClient.realm(TEST).roles().create(composite); composite = adminClient.realm(TEST).roles().get("composite").toRepresentation(); ClientRepresentation client = adminClient.realm(TEST).clients().findByClientId(Constants.REALM_MANAGEMENT_CLIENT_ID).get(0); RoleRepresentation createClient = adminClient.realm(TEST).clients().get(client.getId()).roles().get(AdminRoles.CREATE_CLIENT).toRepresentation(); RoleRepresentation queryRealms = adminClient.realm(TEST).clients().get(client.getId()).roles().get(AdminRoles.QUERY_REALMS).toRepresentation(); List<RoleRepresentation> composites = new LinkedList<>(); composites.add(createClient); composites.add(queryRealms); adminClient.realm(TEST).rolesById().addComposites(composite.getId(), composites); }
Example 5
Source File: RoleByIdResourceTest.java From keycloak with Apache License 2.0 | 6 votes |
@Test public void updateRole() { RoleRepresentation role = resource.getRole(ids.get("role-a")); role.setName("role-a-new"); role.setDescription("Role A New"); resource.updateRole(ids.get("role-a"), role); assertAdminEvents.assertEvent(realmId, OperationType.UPDATE, AdminEventPaths.roleByIdResourcePath(ids.get("role-a")), role, ResourceType.REALM_ROLE); role = resource.getRole(ids.get("role-a")); assertNotNull(role); assertEquals("role-a-new", role.getName()); assertEquals("Role A New", role.getDescription()); assertFalse(role.isComposite()); }
Example 6
Source File: FluentTestsHelper.java From keycloak with Apache License 2.0 | 5 votes |
/** * Associates a user with a role. This method also creates a role if that is missing. * * @param userName A username to be associated with a role. * @param roleName A role to be associated with a user name. * @return <code>this</code> */ public FluentTestsHelper assignRoleWithUser(String userName, String roleName) { if (keycloak.realms().realm(testRealm).roles().get(roleName) == null) { RoleRepresentation representation = new RoleRepresentation(); representation.setName(roleName); keycloak.realms().realm(testRealm).roles().create(representation); } UserRepresentation userRepresentation = keycloak.realms().realm(testRealm).users().search(userName).get(0); RoleRepresentation realmRole = keycloak.realms().realm(testRealm).roles().get(roleName).toRepresentation(); keycloak.realms().realm(testRealm).users().get(userRepresentation.getId()).roles().realmLevel().add(Arrays.asList(realmRole)); return this; }
Example 7
Source File: TestsHelper.java From keycloak with Apache License 2.0 | 5 votes |
public static boolean createTestUser(String username, String password, String realmName) throws IOException { Keycloak keycloak = Keycloak.getInstance( keycloakBaseUrl, "master", username, password, "admin-cli"); UserRepresentation userRepresentation = new UserRepresentation(); userRepresentation.setUsername(username); userRepresentation.setEnabled(Boolean.TRUE); Response response = keycloak.realms().realm(realmName).users().create(userRepresentation); String userId = getCreatedId(response); response.close(); CredentialRepresentation rep = new CredentialRepresentation(); rep.setType(CredentialRepresentation.PASSWORD); rep.setValue(password); rep.setTemporary(false); keycloak.realms().realm(realmName).users().get(userId).resetPassword(rep); //add roles RoleRepresentation representation = new RoleRepresentation(); representation.setName("user"); keycloak.realms().realm(realmName).roles().create(representation); RoleRepresentation realmRole = keycloak.realms().realm(realmName).roles().get("user").toRepresentation(); keycloak.realms().realm(realmName).users().get(userId).roles().realmLevel().add(Arrays.asList(realmRole)); return true; }
Example 8
Source File: RoleDetailsForm.java From keycloak with Apache License 2.0 | 5 votes |
public RoleRepresentation getBasicAttributes() { RoleRepresentation role = new RoleRepresentation(); role.setName(getName()); role.setDescription(getDescription()); role.setComposite(isComposite()); log.info(role.getName() + ": " + role.getDescription() + ", comp: " + role.isComposite()); return role; }
Example 9
Source File: UserStorageTest.java From keycloak with Apache License 2.0 | 5 votes |
@Test @Ignore public void testEntityRemovalHooksCascade() { testingClient.server().run(session -> { UserMapStorage.realmRemovals.set(0); UserMapStorage.groupRemovals.set(0); UserMapStorage.roleRemovals.set(0); }); GroupRepresentation g1 = new GroupRepresentation(); g1.setName("group1"); GroupRepresentation g2 = new GroupRepresentation(); g2.setName("group2"); String gid1 = ApiUtil.getCreatedId(testRealmResource().groups().add(g1)); String gid2 = ApiUtil.getCreatedId(testRealmResource().groups().add(g2)); RoleRepresentation role1 = new RoleRepresentation(); role1.setName("role1"); RoleRepresentation role2 = new RoleRepresentation(); role2.setName("role2"); testRealmResource().roles().create(role1); testRealmResource().roles().create(role2); // remove realm with groups and roles in it testRealmResource().remove(); testingClient.server().run(session -> { Assert.assertEquals(1, UserMapStorage.realmRemovals.get()); Assert.assertEquals(2, UserMapStorage.groupRemovals.get()); // check if group removal hooks were called Assert.assertEquals(2, UserMapStorage.roleRemovals.get()); // check if role removal hooks were called }); }
Example 10
Source File: RoleInvalidationClusterTest.java From keycloak with Apache License 2.0 | 5 votes |
@Override protected RoleRepresentation createTestEntityRepresentation() { RoleRepresentation role = new RoleRepresentation(); role.setName("role_" + RandomStringUtils.randomAlphabetic(5)); role.setComposite(false); role.setDescription("description of "+role.getName()); return role; }
Example 11
Source File: PartialImportTest.java From keycloak with Apache License 2.0 | 5 votes |
private List<RoleRepresentation> makeRoles(String prefix) { List<RoleRepresentation> roles = new ArrayList<>(); for (int i = 0; i < NUM_ENTITIES; i++) { RoleRepresentation role = new RoleRepresentation(); role.setName(prefix + i); roles.add(role); } return roles; }
Example 12
Source File: ImportAuthorizationSettingsTest.java From keycloak with Apache License 2.0 | 5 votes |
@Before public void createRole() { ClientResource clientResource = getClientResource(); RoleRepresentation role = new RoleRepresentation(); role.setName("admin"); clientResource.roles().create(role); testRealmResource().users().create(UserBuilder.create().username("alice").build()); }
Example 13
Source File: ClientScopeTest.java From keycloak with Apache License 2.0 | 5 votes |
private RoleRepresentation createRealmRole(String roleName) { RoleRepresentation roleRep = new RoleRepresentation(); roleRep.setName(roleName); testRealmResource().roles().create(roleRep); assertAdminEvents.assertEvent(getRealmId(), OperationType.CREATE, AdminEventPaths.roleResourcePath(roleName), roleRep, ResourceType.REALM_ROLE); return testRealmResource().roles().get(roleName).toRepresentation(); }
Example 14
Source File: GroupTest.java From keycloak with Apache License 2.0 | 5 votes |
/** * KEYCLOAK-2716 * @throws Exception */ @Test public void testClientRemoveWithClientRoleGroupMapping() throws Exception { RealmResource realm = adminClient.realms().realm("test"); ClientRepresentation client = new ClientRepresentation(); client.setClientId("foo"); client.setRootUrl("http://foo"); client.setProtocol("openid-connect"); Response response = realm.clients().create(client); response.close(); String clientUuid = ApiUtil.getCreatedId(response); assertAdminEvents.assertEvent("test", OperationType.CREATE, AdminEventPaths.clientResourcePath(clientUuid), client, ResourceType.CLIENT); client = realm.clients().findByClientId("foo").get(0); RoleRepresentation role = new RoleRepresentation(); role.setName("foo-role"); realm.clients().get(client.getId()).roles().create(role); assertAdminEvents.assertEvent("test", OperationType.CREATE, AdminEventPaths.clientRoleResourcePath(clientUuid, "foo-role"), role, ResourceType.CLIENT_ROLE); role = realm.clients().get(client.getId()).roles().get("foo-role").toRepresentation(); GroupRepresentation group = new GroupRepresentation(); group.setName("2716"); group = createGroup(realm, group); List<RoleRepresentation> list = new LinkedList<>(); list.add(role); realm.groups().group(group.getId()).roles().clientLevel(client.getId()).add(list); assertAdminEvents.assertEvent("test", OperationType.CREATE, AdminEventPaths.groupRolesClientRolesPath(group.getId(), clientUuid), list, ResourceType.CLIENT_ROLE_MAPPING); realm.clients().get(client.getId()).remove(); assertAdminEvents.assertEvent("test", OperationType.DELETE, AdminEventPaths.clientResourcePath(clientUuid), ResourceType.CLIENT); }
Example 15
Source File: AudienceTest.java From keycloak with Apache License 2.0 | 5 votes |
@Override public void configureTestRealm(RealmRepresentation testRealm) { // Create service client with some client role ClientRepresentation client1 = new ClientRepresentation(); client1.setClientId("service-client"); client1.setProtocol(OIDCLoginProtocol.LOGIN_PROTOCOL); client1.setBearerOnly(true); client1.setBaseUrl("http://foo/service-client"); testRealm.getClients().add(client1); RoleRepresentation role1 = new RoleRepresentation(); role1.setName("role1"); testRealm.getRoles().getClient().put("service-client", Arrays.asList(role1)); // Disable FullScopeAllowed for the 'test-app' client ClientRepresentation testApp = testRealm.getClients().stream().filter((ClientRepresentation client) -> { return "test-app".equals(client.getClientId()); }).findFirst().get(); testApp.setFullScopeAllowed(false); // Create sample user UserRepresentation user = UserBuilder.create() .id(userId) .username("john") .enabled(true) .email("[email protected]") .firstName("John") .lastName("Doe") .password("password") .role("account", "manage-account") .role("account", "view-profile") .role("service-client", "role1") .build(); testRealm.getUsers().add(user); }
Example 16
Source File: RealmsConfigurationLoader.java From keycloak with Apache License 2.0 | 5 votes |
private List<RoleRepresentation> convertRealmRoleNamesToRepresentation(List<String> roles) { LinkedList<RoleRepresentation> result = new LinkedList<>(); for (String role: roles) { RoleRepresentation r = new RoleRepresentation(); String id = realmRoleIdMap.get(role); if (id == null) { throw new RuntimeException("No realm role created for name: " + role); } r.setId(id); r.setName(role); result.add(r); } return result; }
Example 17
Source File: ClientRolesTest.java From keycloak with Apache License 2.0 | 4 votes |
private RoleRepresentation makeRole(String name) { RoleRepresentation role = new RoleRepresentation(); role.setName(name); return role; }
Example 18
Source File: RealmRolesTest.java From keycloak with Apache License 2.0 | 4 votes |
private RoleRepresentation makeRole(String name) { RoleRepresentation role = new RoleRepresentation(); role.setName(name); return role; }
Example 19
Source File: UserStorageTest.java From keycloak with Apache License 2.0 | 4 votes |
@Test public void testEntityRemovalHooks() { testingClient.server().run(session -> { UserMapStorage.realmRemovals.set(0); UserMapStorage.groupRemovals.set(0); UserMapStorage.roleRemovals.set(0); }); // remove group GroupRepresentation g1 = new GroupRepresentation(); g1.setName("group1"); GroupRepresentation g2 = new GroupRepresentation(); g2.setName("group2"); String gid1 = ApiUtil.getCreatedId(testRealmResource().groups().add(g1)); String gid2 = ApiUtil.getCreatedId(testRealmResource().groups().add(g2)); testRealmResource().groups().group(gid1).remove(); testRealmResource().groups().group(gid2).remove(); testingClient.server().run(session -> { Assert.assertEquals(2, UserMapStorage.groupRemovals.get()); UserMapStorage.realmRemovals.set(0); }); // remove role RoleRepresentation role1 = new RoleRepresentation(); role1.setName("role1"); RoleRepresentation role2 = new RoleRepresentation(); role2.setName("role2"); testRealmResource().roles().create(role1); testRealmResource().roles().create(role2); testRealmResource().roles().get("role1").remove(); testRealmResource().roles().get("role2").remove(); testingClient.server().run(session -> { Assert.assertEquals(2, UserMapStorage.roleRemovals.get()); UserMapStorage.realmRemovals.set(0); }); // remove realm RealmRepresentation testRealmRepresentation = testRealmResource().toRepresentation(); testRealmResource().remove(); testingClient.server().run(session -> { Assert.assertEquals(1, UserMapStorage.realmRemovals.get()); UserMapStorage.realmRemovals.set(0); }); // Re-create realm RealmRepresentation repOrig = testContext.getTestRealmReps().get(0); adminClient.realms().create(repOrig); }
Example 20
Source File: OIDCScopeTest.java From keycloak with Apache License 2.0 | 4 votes |
@Override public void configureTestRealm(RealmRepresentation testRealm) { UserRepresentation user = UserBuilder.create() .id(userId) .username("john") .enabled(true) .email("[email protected]") .firstName("John") .lastName("Doe") .password("password") .role("account", "manage-account") .role("account", "view-profile") .addRoles("role-1", "role-2") .build(); user.setEmailVerified(true); MultivaluedHashMap<String, String> attrs = new MultivaluedHashMap<>(); attrs.add("street", "Elm 5"); attrs.add("phoneNumber", "111-222-333"); attrs.add("phoneNumberVerified", "true"); user.setAttributes(attrs); testRealm.getUsers().add(user); // Add sample realm roles RoleRepresentation role1 = new RoleRepresentation(); role1.setName("role-1"); testRealm.getRoles().getRealm().add(role1); RoleRepresentation role2 = new RoleRepresentation(); role2.setName("role-2"); testRealm.getRoles().getRealm().add(role2); RoleRepresentation roleParent = RoleBuilder.create() .name("role-parent") .realmComposite("role-1") .build(); testRealm.getRoles().getRealm().add(roleParent); // Add sample group GroupRepresentation group = new GroupRepresentation(); group.setName("group-role-1"); group.setRealmRoles(Collections.singletonList("role-1")); testRealm.getGroups().add(group); // Add more sample users user = UserBuilder.create() .username("role-1-user") .enabled(true) .password("password") .addRoles("role-1") .build(); testRealm.getUsers().add(user); user = UserBuilder.create() .username("role-2-user") .enabled(true) .password("password") .addRoles("role-2") .build(); testRealm.getUsers().add(user); user = UserBuilder.create() .username("role-parent-user") .enabled(true) .password("password") .addRoles("role-parent") .build(); testRealm.getUsers().add(user); user = UserBuilder.create() .username("group-role-1-user") .enabled(true) .password("password") .addGroups("group-role-1") .build(); testRealm.getUsers().add(user); }