Java Code Examples for org.keycloak.representations.idm.UserRepresentation#getRealmRoles()

The following examples show how to use org.keycloak.representations.idm.UserRepresentation#getRealmRoles() . 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: RepresentationToModel.java    From keycloak with Apache License 2.0 6 votes vote down vote up
public static void createRoleMappings(UserRepresentation userRep, UserModel user, RealmModel realm) {
    if (userRep.getRealmRoles() != null) {
        for (String roleString : userRep.getRealmRoles()) {
            RoleModel role = realm.getRole(roleString.trim());
            if (role == null) {
                role = realm.addRole(roleString.trim());
            }
            user.grantRole(role);
        }
    }
    if (userRep.getClientRoles() != null) {
        for (Map.Entry<String, List<String>> entry : userRep.getClientRoles().entrySet()) {
            ClientModel client = realm.getClientByClientId(entry.getKey());
            if (client == null) {
                throw new RuntimeException("Unable to find client role mappings for client: " + entry.getKey());
            }
            createClientRoleMappings(client, user, entry.getValue());
        }
    }
}
 
Example 2
Source File: RepresentationToModel.java    From keycloak with Apache License 2.0 6 votes vote down vote up
public static void createFederatedRoleMappings(UserFederatedStorageProvider federatedStorage, UserRepresentation userRep, RealmModel realm) {
    if (userRep.getRealmRoles() != null) {
        for (String roleString : userRep.getRealmRoles()) {
            RoleModel role = realm.getRole(roleString.trim());
            if (role == null) {
                role = realm.addRole(roleString.trim());
            }
            federatedStorage.grantRole(realm, userRep.getId(), role);
        }
    }
    if (userRep.getClientRoles() != null) {
        for (Map.Entry<String, List<String>> entry : userRep.getClientRoles().entrySet()) {
            ClientModel client = realm.getClientByClientId(entry.getKey());
            if (client == null) {
                throw new RuntimeException("Unable to find client role mappings for client: " + entry.getKey());
            }
            createFederatedClientRoleMappings(federatedStorage, realm, client, userRep, entry.getValue());
        }
    }
}
 
Example 3
Source File: PartialExportTest.java    From keycloak with Apache License 2.0 6 votes vote down vote up
private void checkServiceAccountRoles(UserRepresentation serviceAccount, boolean rolesExpected) {
    Assert.assertTrue("User is a service account", serviceAccount.getUsername().startsWith(ServiceAccountConstants.SERVICE_ACCOUNT_USER_PREFIX));
    Assert.assertNull("Password should be null", serviceAccount.getCredentials());
    if (rolesExpected) {
        List<String> realmRoles = serviceAccount.getRealmRoles();
        Assert.assertThat("Realm roles are OK", realmRoles, Matchers.containsInAnyOrder("uma_authorization", "user", "offline_access"));

        Map<String, List<String>> clientRoles = serviceAccount.getClientRoles();
        Assert.assertNotNull("Client roles are exported", clientRoles);
        Assert.assertThat("Client roles for test-app-service-account are OK", clientRoles.get("test-app-service-account"),
                Matchers.containsInAnyOrder("test-app-service-account", "test-app-service-account-parent"));
        Assert.assertThat("Client roles for account are OK", clientRoles.get("account"),
                Matchers.containsInAnyOrder("manage-account", "view-profile"));
    } else {
        Assert.assertNull("Service account should be exported without realm roles", serviceAccount.getRealmRoles());
        Assert.assertNull("Service account should be exported without client roles", serviceAccount.getClientRoles());
    }
}
 
Example 4
Source File: UserManager.java    From keycloak with Apache License 2.0 5 votes vote down vote up
public void assignRoles(String... roles) {
    UserRepresentation user = userResource.toRepresentation();
    if (user != null && user.getRealmRoles() == null) {
        user.setRealmRoles(new ArrayList<String>());
    }
    user.setRealmRoles(Arrays.asList(roles));
    userResource.update(user);
}