Java Code Examples for org.keycloak.representations.idm.RoleRepresentation#Composites

The following examples show how to use org.keycloak.representations.idm.RoleRepresentation#Composites . 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: ImportRolesIT.java    From keycloak-config-cli with Apache License 2.0 6 votes vote down vote up
@Test
@Order(11)
void shouldAddRealmRoleWithRealmComposite() {
    doImport("11_update_realm__add_realm_role_with_realm_composite.json");

    RealmRepresentation createdRealm = keycloakProvider.get().realm(REALM_NAME).toRepresentation();

    assertThat(createdRealm.getRealm(), is(REALM_NAME));
    assertThat(createdRealm.isEnabled(), is(true));

    RoleRepresentation realmRole = keycloakRepository.getRealmRole(
            REALM_NAME,
            "my_composite_realm_role"
    );

    assertThat(realmRole.getName(), is("my_composite_realm_role"));
    assertThat(realmRole.isComposite(), is(true));
    assertThat(realmRole.getClientRole(), is(false));
    assertThat(realmRole.getDescription(), is("My added composite realm role"));

    RoleRepresentation.Composites composites = realmRole.getComposites();
    assertThat(composites, notNullValue());
    assertThat(composites.getRealm(), contains("my_realm_role"));
    assertThat(composites.getClient(), is(nullValue()));
}
 
Example 2
Source File: ImportRolesIT.java    From keycloak-config-cli with Apache License 2.0 6 votes vote down vote up
@Test
@Order(13)
void shouldAddRealmCompositeToRealmRole() {
    doImport("13_update_realm__add_realm_composite_to_realm_role.json");

    RealmRepresentation createdRealm = keycloakProvider.get().realm(REALM_NAME).toRepresentation();

    assertThat(createdRealm.getRealm(), is(REALM_NAME));
    assertThat(createdRealm.isEnabled(), is(true));

    RoleRepresentation realmRole = keycloakRepository.getRealmRole(
            REALM_NAME,
            "my_composite_realm_role"
    );

    assertThat(realmRole.getName(), is("my_composite_realm_role"));
    assertThat(realmRole.isComposite(), is(true));
    assertThat(realmRole.getClientRole(), is(false));
    assertThat(realmRole.getDescription(), is("My added composite realm role"));

    RoleRepresentation.Composites composites = realmRole.getComposites();
    assertThat(composites, notNullValue());
    assertThat(composites.getRealm(), containsInAnyOrder("my_realm_role", "my_other_realm_role"));
    assertThat(composites.getClient(), is(nullValue()));
}
 
Example 3
Source File: PartialExportTest.java    From keycloak with Apache License 2.0 6 votes vote down vote up
private void checkClientRoles(Map<String, List<RoleRepresentation>> clientRoles) {
    Map<String, RoleRepresentation> roles = collectRoles(clientRoles.get("test-app"));
    Assert.assertTrue("Client role customer-admin for test-app", roles.containsKey("customer-admin"));
    Assert.assertTrue("Client role sample-client-role for test-app", roles.containsKey("sample-client-role"));
    Assert.assertTrue("Client role customer-user for test-app", roles.containsKey("customer-user"));

    Assert.assertTrue("Client role customer-admin-composite-role for test-app", roles.containsKey("customer-admin-composite-role"));
    RoleRepresentation.Composites cmp = roles.get("customer-admin-composite-role").getComposites();
    Assert.assertTrue("customer-admin-composite-role / realm / customer-user-premium", cmp.getRealm().contains("customer-user-premium"));
    Assert.assertTrue("customer-admin-composite-role / client['test-app'] / customer-admin", cmp.getClient().get("test-app").contains("customer-admin"));

    roles = collectRoles(clientRoles.get("test-app-scope"));
    Assert.assertTrue("Client role test-app-disallowed-by-scope for test-app-scope", roles.containsKey("test-app-disallowed-by-scope"));
    Assert.assertTrue("Client role test-app-allowed-by-scope for test-app-scope", roles.containsKey("test-app-allowed-by-scope"));

    roles = collectRoles(clientRoles.get("test-app-service-account"));
    Assert.assertThat("Client roles are OK for test-app-service-account", roles.keySet(),
            Matchers.containsInAnyOrder("test-app-service-account", "test-app-service-account-parent", "test-app-service-account-child"));
}
 
Example 4
Source File: ImportRolesIT.java    From keycloak-config-cli with Apache License 2.0 6 votes vote down vote up
@Test
@Order(16)
void shouldAddClientRoleWithRealmRoleComposite() {
    doImport("16_update_realm__add_client_role_with_realm_role_composite.json");

    RealmRepresentation createdRealm = keycloakProvider.get().realm(REALM_NAME).toRepresentation();

    assertThat(createdRealm.getRealm(), is(REALM_NAME));
    assertThat(createdRealm.isEnabled(), is(true));

    RoleRepresentation realmRole = keycloakRepository.getClientRole(
            REALM_NAME,
            "moped-client",
            "my_composite_moped_client_role"
    );

    assertThat(realmRole.getName(), is("my_composite_moped_client_role"));
    assertThat(realmRole.isComposite(), is(true));
    assertThat(realmRole.getClientRole(), is(true));
    assertThat(realmRole.getDescription(), is("My composite moped-client role"));

    RoleRepresentation.Composites composites = realmRole.getComposites();
    assertThat(composites, notNullValue());
    assertThat(composites.getRealm(), contains("my_realm_role"));
    assertThat(composites.getClient(), is(nullValue()));
}
 
Example 5
Source File: ImportRolesIT.java    From keycloak-config-cli with Apache License 2.0 6 votes vote down vote up
@Test
@Order(18)
void shouldAddRealmRoleCompositeToClientRole() {
    doImport("18_update_realm__add_realm_role_composite to_client_role.json");

    RealmRepresentation createdRealm = keycloakProvider.get().realm(REALM_NAME).toRepresentation();

    assertThat(createdRealm.getRealm(), is(REALM_NAME));
    assertThat(createdRealm.isEnabled(), is(true));

    RoleRepresentation realmRole = keycloakRepository.getClientRole(
            REALM_NAME,
            "moped-client",
            "my_composite_moped_client_role"
    );

    assertThat(realmRole.getName(), is("my_composite_moped_client_role"));
    assertThat(realmRole.isComposite(), is(true));
    assertThat(realmRole.getClientRole(), is(true));
    assertThat(realmRole.getDescription(), is("My composite moped-client role"));

    RoleRepresentation.Composites composites = realmRole.getComposites();
    assertThat(composites, notNullValue());
    assertThat(composites.getRealm(), containsInAnyOrder("my_realm_role", "my_other_realm_role"));
    assertThat(composites.getClient(), is(nullValue()));
}
 
Example 6
Source File: ImportRolesIT.java    From keycloak-config-cli with Apache License 2.0 6 votes vote down vote up
@Test
@Order(24)
void shouldRemoveRealmCompositeFromClientRole() {
    doImport("24_update_realm__remove_realm_role_composite_from_client_role.json");

    RealmRepresentation createdRealm = keycloakProvider.get().realm(REALM_NAME).toRepresentation();

    assertThat(createdRealm.getRealm(), is(REALM_NAME));
    assertThat(createdRealm.isEnabled(), is(true));

    RoleRepresentation realmRole = keycloakRepository.getClientRole(
            REALM_NAME,
            "moped-client",
            "my_composite_moped_client_role"
    );

    assertThat(realmRole.getName(), is("my_composite_moped_client_role"));
    assertThat(realmRole.isComposite(), is(true));
    assertThat(realmRole.getClientRole(), is(true));
    assertThat(realmRole.getDescription(), is("My composite moped-client role"));

    RoleRepresentation.Composites composites = realmRole.getComposites();
    assertThat(composites, notNullValue());
    assertThat(composites.getRealm(), contains("my_other_realm_role"));
    assertThat(composites.getClient(), is(nullValue()));
}
 
Example 7
Source File: ImportRolesIT.java    From keycloak-config-cli with Apache License 2.0 6 votes vote down vote up
@Test
@Order(21)
void shouldRemoveRealmCompositeFromRealmRole() {
    doImport("21_update_realm__remove_realm_role_composite_from_realm_role.json");

    RealmRepresentation createdRealm = keycloakProvider.get().realm(REALM_NAME).toRepresentation();

    assertThat(createdRealm.getRealm(), is(REALM_NAME));
    assertThat(createdRealm.isEnabled(), is(true));

    RoleRepresentation realmRole = keycloakRepository.getRealmRole(
            REALM_NAME,
            "my_composite_realm_role"
    );

    assertThat(realmRole.getName(), is("my_composite_realm_role"));
    assertThat(realmRole.isComposite(), is(true));
    assertThat(realmRole.getClientRole(), is(false));
    assertThat(realmRole.getDescription(), is("My added composite realm role"));

    RoleRepresentation.Composites composites = realmRole.getComposites();
    assertThat(composites, notNullValue());
    assertThat(composites.getRealm(), contains("my_other_realm_role"));
    assertThat(composites.getClient(), is(nullValue()));
}
 
Example 8
Source File: ImportRolesIT.java    From keycloak-config-cli with Apache License 2.0 5 votes vote down vote up
@Test
@Order(12)
void shouldAddRealmRoleWithClientComposite() {
    doImport("12_update_realm__add_realm_role_with_client_composite.json");

    RealmRepresentation createdRealm = keycloakProvider.get().realm(REALM_NAME).toRepresentation();

    assertThat(createdRealm.getRealm(), is(REALM_NAME));
    assertThat(createdRealm.isEnabled(), is(true));

    RoleRepresentation realmRole = keycloakRepository.getRealmRole(
            REALM_NAME,
            "my_composite_client_role"
    );

    assertThat(realmRole.getName(), is("my_composite_client_role"));
    assertThat(realmRole.isComposite(), is(true));
    assertThat(realmRole.getClientRole(), is(false));
    assertThat(realmRole.getDescription(), is("My added composite client role"));

    RoleRepresentation.Composites composites = realmRole.getComposites();
    assertThat(composites, notNullValue());
    assertThat(composites.getRealm(), is(nullValue()));

    assertThat(composites.getClient(), aMapWithSize(1));
    assertThat(composites.getClient(), hasEntry(is("moped-client"), containsInAnyOrder("my_client_role")));
}
 
Example 9
Source File: ImportRolesIT.java    From keycloak-config-cli with Apache License 2.0 5 votes vote down vote up
@Test
@Order(26)
void shouldRemoveClientCompositesFromClientRole() {
    doImport("26_update_realm__remove_client_role_composites_from_client_role.json");

    RealmRepresentation createdRealm = keycloakProvider.get().realm(REALM_NAME).toRepresentation();

    assertThat(createdRealm.getRealm(), is(REALM_NAME));
    assertThat(createdRealm.isEnabled(), is(true));

    RoleRepresentation realmRole = keycloakRepository.getClientRole(
            REALM_NAME,
            "moped-client",
            "my_other_composite_moped_client_role"
    );

    assertThat(realmRole.getName(), is("my_other_composite_moped_client_role"));
    assertThat(realmRole.isComposite(), is(true));
    assertThat(realmRole.getClientRole(), is(true));
    assertThat(realmRole.getDescription(), is("My other composite moped-client role"));

    RoleRepresentation.Composites composites = realmRole.getComposites();
    assertThat(composites, notNullValue());
    assertThat(composites.getRealm(), is(nullValue()));

    assertThat(composites.getClient(), aMapWithSize(1));
    assertThat(composites.getClient(), hasEntry(is("second-moped-client"), containsInAnyOrder("my_other_second_client_role")));
}
 
Example 10
Source File: ImportRolesIT.java    From keycloak-config-cli with Apache License 2.0 5 votes vote down vote up
@Test
@Order(25)
void shouldRemoveClientCompositeFromClientRole() {
    doImport("25_update_realm__remove_client_role_composite_from_client_role.json");

    RealmRepresentation createdRealm = keycloakProvider.get().realm(REALM_NAME).toRepresentation();

    assertThat(createdRealm.getRealm(), is(REALM_NAME));
    assertThat(createdRealm.isEnabled(), is(true));

    RoleRepresentation realmRole = keycloakRepository.getClientRole(
            REALM_NAME,
            "moped-client",
            "my_other_composite_moped_client_role"
    );

    assertThat(realmRole.getName(), is("my_other_composite_moped_client_role"));
    assertThat(realmRole.isComposite(), is(true));
    assertThat(realmRole.getClientRole(), is(true));
    assertThat(realmRole.getDescription(), is("My other composite moped-client role"));

    RoleRepresentation.Composites composites = realmRole.getComposites();
    assertThat(composites, notNullValue());
    assertThat(composites.getRealm(), is(nullValue()));

    assertThat(composites.getClient(), aMapWithSize(2));
    assertThat(composites.getClient(), hasEntry(is("moped-client"), containsInAnyOrder("my_client_role", "my_other_client_role")));
    assertThat(composites.getClient(), hasEntry(is("second-moped-client"), containsInAnyOrder("my_other_second_client_role")));
}
 
Example 11
Source File: ImportRolesIT.java    From keycloak-config-cli with Apache License 2.0 5 votes vote down vote up
@Test
@Order(23)
void shouldRemoveClientCompositesFromRealmRole() {
    doImport("23_update_realm__remove_client_role_composites_from_realm_role.json");

    RealmRepresentation createdRealm = keycloakProvider.get().realm(REALM_NAME).toRepresentation();

    assertThat(createdRealm.getRealm(), is(REALM_NAME));
    assertThat(createdRealm.isEnabled(), is(true));

    RoleRepresentation realmRole = keycloakRepository.getRealmRole(
            REALM_NAME,
            "my_composite_client_role"
    );

    assertThat(realmRole.getName(), is("my_composite_client_role"));
    assertThat(realmRole.isComposite(), is(true));
    assertThat(realmRole.getClientRole(), is(false));
    assertThat(realmRole.getDescription(), is("My added composite client role"));

    RoleRepresentation.Composites composites = realmRole.getComposites();
    assertThat(composites, notNullValue());
    assertThat(composites.getRealm(), is(nullValue()));

    assertThat(composites.getClient(), aMapWithSize(1));
    assertThat(composites.getClient(), hasEntry(is("moped-client"), containsInAnyOrder("my_other_client_role")));
}
 
Example 12
Source File: ImportRolesIT.java    From keycloak-config-cli with Apache License 2.0 5 votes vote down vote up
@Test
@Order(22)
void shouldRemoveCompositeClientFromRealmRole() {
    doImport("22_update_realm__remove_client_role_composite_from_realm_role.json");

    RealmRepresentation createdRealm = keycloakProvider.get().realm(REALM_NAME).toRepresentation();

    assertThat(createdRealm.getRealm(), is(REALM_NAME));
    assertThat(createdRealm.isEnabled(), is(true));

    RoleRepresentation realmRole = keycloakRepository.getRealmRole(
            REALM_NAME,
            "my_composite_client_role"
    );

    assertThat(realmRole.getName(), is("my_composite_client_role"));
    assertThat(realmRole.isComposite(), is(true));
    assertThat(realmRole.getClientRole(), is(false));
    assertThat(realmRole.getDescription(), is("My added composite client role"));

    RoleRepresentation.Composites composites = realmRole.getComposites();
    assertThat(composites, notNullValue());
    assertThat(composites.getRealm(), is(nullValue()));

    assertThat(composites.getClient(), aMapWithSize(2));
    assertThat(composites.getClient(), hasEntry(is("moped-client"), containsInAnyOrder("my_other_client_role")));
    assertThat(composites.getClient(), hasEntry(is("second-moped-client"), containsInAnyOrder("my_other_second_client_role", "my_second_client_role")));
}
 
Example 13
Source File: ImportRolesIT.java    From keycloak-config-cli with Apache License 2.0 5 votes vote down vote up
@Test
@Order(20)
void shouldAddClientRoleCompositesToClientRole() {
    doImport("20_update_realm__add_client_role_composites_to_client_role.json");

    RealmRepresentation createdRealm = keycloakProvider.get().realm(REALM_NAME).toRepresentation();

    assertThat(createdRealm.getRealm(), is(REALM_NAME));
    assertThat(createdRealm.isEnabled(), is(true));

    RoleRepresentation realmRole = keycloakRepository.getClientRole(
            REALM_NAME,
            "moped-client",
            "my_other_composite_moped_client_role"
    );

    assertThat(realmRole.getName(), is("my_other_composite_moped_client_role"));
    assertThat(realmRole.isComposite(), is(true));
    assertThat(realmRole.getClientRole(), is(true));
    assertThat(realmRole.getDescription(), is("My other composite moped-client role"));

    RoleRepresentation.Composites composites = realmRole.getComposites();
    assertThat(composites, notNullValue());
    assertThat(composites.getRealm(), is(nullValue()));

    assertThat(composites.getClient(), aMapWithSize(2));
    assertThat(composites.getClient(), hasEntry(is("moped-client"), containsInAnyOrder("my_client_role", "my_other_client_role")));
    assertThat(composites.getClient(), hasEntry(is("second-moped-client"), containsInAnyOrder("my_other_second_client_role", "my_second_client_role")));
}
 
Example 14
Source File: ImportRolesIT.java    From keycloak-config-cli with Apache License 2.0 5 votes vote down vote up
@Test
@Order(19)
void shouldAddClientRoleCompositeToClientRole() {
    doImport("19_update_realm__add_client_role_composite to_client_role.json");

    RealmRepresentation createdRealm = keycloakProvider.get().realm(REALM_NAME).toRepresentation();

    assertThat(createdRealm.getRealm(), is(REALM_NAME));
    assertThat(createdRealm.isEnabled(), is(true));

    RoleRepresentation realmRole = keycloakRepository.getClientRole(
            REALM_NAME,
            "moped-client",
            "my_other_composite_moped_client_role"
    );

    assertThat(realmRole.getName(), is("my_other_composite_moped_client_role"));
    assertThat(realmRole.isComposite(), is(true));
    assertThat(realmRole.getClientRole(), is(true));
    assertThat(realmRole.getDescription(), is("My other composite moped-client role"));

    RoleRepresentation.Composites composites = realmRole.getComposites();
    assertThat(composites, notNullValue());
    assertThat(composites.getRealm(), is(nullValue()));

    assertThat(composites.getClient(), aMapWithSize(1));
    assertThat(composites.getClient(), hasEntry(is("moped-client"), containsInAnyOrder("my_client_role", "my_other_client_role")));
}
 
Example 15
Source File: ImportRolesIT.java    From keycloak-config-cli with Apache License 2.0 5 votes vote down vote up
@Test
@Order(17)
void shouldAddClientRoleWithClientRoleComposite() {
    doImport("17_update_realm__add_client_role_with_client_role_composite.json");

    RealmRepresentation createdRealm = keycloakProvider.get().realm(REALM_NAME).toRepresentation();

    assertThat(createdRealm.getRealm(), is(REALM_NAME));
    assertThat(createdRealm.isEnabled(), is(true));

    RoleRepresentation realmRole = keycloakRepository.getClientRole(
            REALM_NAME,
            "moped-client",
            "my_other_composite_moped_client_role"
    );

    assertThat(realmRole.getName(), is("my_other_composite_moped_client_role"));
    assertThat(realmRole.isComposite(), is(true));
    assertThat(realmRole.getClientRole(), is(true));
    assertThat(realmRole.getDescription(), is("My other composite moped-client role"));

    RoleRepresentation.Composites composites = realmRole.getComposites();
    assertThat(composites, notNullValue());
    assertThat(composites.getRealm(), is(nullValue()));

    assertThat(composites.getClient(), aMapWithSize(1));
    assertThat(composites.getClient(), hasEntry(is("moped-client"), containsInAnyOrder("my_client_role")));
}
 
Example 16
Source File: ImportRolesIT.java    From keycloak-config-cli with Apache License 2.0 5 votes vote down vote up
@Test
@Order(15)
void shouldAddCompositeClientToRealmRole() {
    doImport("15_update_realm__add_composite_client_to_realm_role.json");

    RealmRepresentation createdRealm = keycloakProvider.get().realm(REALM_NAME).toRepresentation();

    assertThat(createdRealm.getRealm(), is(REALM_NAME));
    assertThat(createdRealm.isEnabled(), is(true));

    RoleRepresentation realmRole = keycloakRepository.getRealmRole(
            REALM_NAME,
            "my_composite_client_role"
    );

    assertThat(realmRole.getName(), is("my_composite_client_role"));
    assertThat(realmRole.isComposite(), is(true));
    assertThat(realmRole.getClientRole(), is(false));
    assertThat(realmRole.getDescription(), is("My added composite client role"));

    RoleRepresentation.Composites composites = realmRole.getComposites();
    assertThat(composites, notNullValue());
    assertThat(composites.getRealm(), is(nullValue()));

    assertThat(composites.getClient(), aMapWithSize(2));
    assertThat(composites.getClient(), hasEntry(is("moped-client"), containsInAnyOrder("my_client_role", "my_other_client_role")));
    assertThat(composites.getClient(), hasEntry(is("second-moped-client"), containsInAnyOrder("my_other_second_client_role", "my_second_client_role")));
}
 
Example 17
Source File: ImportRolesIT.java    From keycloak-config-cli with Apache License 2.0 5 votes vote down vote up
@Test
@Order(14)
void shouldAddClientCompositeToRealmRole() {
    doImport("14_update_realm__add_client_composite_to_realm_role.json");

    RealmRepresentation createdRealm = keycloakProvider.get().realm(REALM_NAME).toRepresentation();

    assertThat(createdRealm.getRealm(), is(REALM_NAME));
    assertThat(createdRealm.isEnabled(), is(true));

    RoleRepresentation realmRole = keycloakRepository.getRealmRole(
            REALM_NAME,
            "my_composite_client_role"
    );

    assertThat(realmRole.getName(), is("my_composite_client_role"));
    assertThat(realmRole.isComposite(), is(true));
    assertThat(realmRole.getClientRole(), is(false));
    assertThat(realmRole.getDescription(), is("My added composite client role"));

    RoleRepresentation.Composites composites = realmRole.getComposites();
    assertThat(composites, notNullValue());
    assertThat(composites.getRealm(), is(nullValue()));

    assertThat(composites.getClient(), aMapWithSize(1));
    assertThat(composites.getClient(), hasEntry(is("moped-client"), containsInAnyOrder("my_client_role", "my_other_client_role")));
}
 
Example 18
Source File: ExportUtils.java    From keycloak with Apache License 2.0 4 votes vote down vote up
/**
 * Full export of role including composite roles
 * @param role
 * @return RoleRepresentation with all stuff filled (including composite roles)
 */
public static RoleRepresentation exportRole(RoleModel role) {
    RoleRepresentation roleRep = ModelToRepresentation.toRepresentation(role);

    Set<RoleModel> composites = role.getComposites();
    if (composites != null && composites.size() > 0) {
        Set<String> compositeRealmRoles = null;
        Map<String, List<String>> compositeClientRoles = null;

        for (RoleModel composite : composites) {
            RoleContainerModel crContainer = composite.getContainer();
            if (crContainer instanceof RealmModel) {

                if (compositeRealmRoles == null) {
                    compositeRealmRoles = new HashSet<>();
                }
                compositeRealmRoles.add(composite.getName());
            } else {
                if (compositeClientRoles == null) {
                    compositeClientRoles = new HashMap<>();
                }

                ClientModel app = (ClientModel)crContainer;
                String appName = app.getClientId();
                List<String> currentAppComposites = compositeClientRoles.get(appName);
                if (currentAppComposites == null) {
                    currentAppComposites = new ArrayList<>();
                    compositeClientRoles.put(appName, currentAppComposites);
                }
                currentAppComposites.add(composite.getName());
            }
        }

        RoleRepresentation.Composites compRep = new RoleRepresentation.Composites();
        if (compositeRealmRoles != null) {
            compRep.setRealm(compositeRealmRoles);
        }
        if (compositeClientRoles != null) {
            compRep.setClient(compositeClientRoles);
        }

        roleRep.setComposites(compRep);
    }

    return roleRep;
}