use of org.keycloak.admin.client.Keycloak in project airavata by apache.
the class TenantManagementKeycloakImpl method updateUserProfile.
@Override
public void updateUserProfile(PasswordCredential realmAdminCreds, String tenantId, String username, UserProfile userDetails) throws IamAdminServicesException {
Keycloak client = null;
try {
client = TenantManagementKeycloakImpl.getClient(ServerSettings.getIamServerUrl(), tenantId, realmAdminCreds);
List<UserRepresentation> retrieveUserList = client.realm(tenantId).users().search(username, null, null, null, 0, 1);
if (!retrieveUserList.isEmpty()) {
UserRepresentation userRepresentation = retrieveUserList.get(0);
userRepresentation.setFirstName(userDetails.getFirstName());
userRepresentation.setLastName(userDetails.getLastName());
userRepresentation.setEmail(userDetails.getEmails().get(0));
UserResource userResource = client.realm(tenantId).users().get(userRepresentation.getId());
userResource.update(userRepresentation);
} else {
throw new IamAdminServicesException("User [" + username + "] wasn't found in Keycloak!");
}
} catch (ApplicationSettingsException ex) {
logger.error("Error getting values from property file, reason: " + ex.getMessage(), ex);
IamAdminServicesException exception = new IamAdminServicesException();
exception.setMessage("Error getting values from property file, reason " + ex.getMessage());
throw exception;
} catch (Exception ex) {
logger.error("Error updating user profile in keycloak server, reason: " + ex.getMessage(), ex);
IamAdminServicesException exception = new IamAdminServicesException();
exception.setMessage("Error updating user profile in keycloak server, reason: " + ex.getMessage());
throw exception;
} finally {
if (client != null) {
client.close();
}
}
}
use of org.keycloak.admin.client.Keycloak in project airavata by apache.
the class TenantManagementKeycloakImpl method addRoleToUser.
@Override
public boolean addRoleToUser(PasswordCredential realmAdminCreds, String tenantId, String username, String roleName) throws IamAdminServicesException {
Keycloak client = null;
try {
client = TenantManagementKeycloakImpl.getClient(ServerSettings.getIamServerUrl(), tenantId, realmAdminCreds);
List<UserRepresentation> retrieveCreatedUserList = client.realm(tenantId).users().search(username, null, null, null, 0, 1);
UserResource retrievedUser = client.realm(tenantId).users().get(retrieveCreatedUserList.get(0).getId());
// Add user to the role
RoleResource roleResource = client.realm(tenantId).roles().get(roleName);
retrievedUser.roles().realmLevel().add(Arrays.asList(roleResource.toRepresentation()));
return true;
} catch (ApplicationSettingsException ex) {
logger.error("Error getting values from property file, reason: " + ex.getMessage(), ex);
IamAdminServicesException exception = new IamAdminServicesException();
exception.setMessage("Error getting values from property file, reason " + ex.getMessage());
throw exception;
} finally {
if (client != null) {
client.close();
}
}
}
use of org.keycloak.admin.client.Keycloak in project airavata by apache.
the class TenantManagementKeycloakImpl method resetUserPassword.
@Override
public boolean resetUserPassword(PasswordCredential realmAdminCreds, String tenantId, String username, String newPassword) throws IamAdminServicesException {
Keycloak client = null;
try {
client = TenantManagementKeycloakImpl.getClient(ServerSettings.getIamServerUrl(), tenantId, realmAdminCreds);
List<UserRepresentation> retrieveUserList = client.realm(tenantId).users().search(username, null, null, null, 0, 1);
if (!retrieveUserList.isEmpty()) {
UserResource retrievedUser = client.realm(tenantId).users().get(retrieveUserList.get(0).getId());
CredentialRepresentation credential = new CredentialRepresentation();
credential.setType(CredentialRepresentation.PASSWORD);
credential.setValue(newPassword);
credential.setTemporary(false);
retrievedUser.resetPassword(credential);
// Remove the UPDATE_PASSWORD required action
UserRepresentation userRepresentation = retrievedUser.toRepresentation();
userRepresentation.getRequiredActions().remove("UPDATE_PASSWORD");
retrievedUser.update(userRepresentation);
return true;
} else {
logger.error("requested User not found");
return false;
}
} catch (ApplicationSettingsException ex) {
logger.error("Error getting values from property file, reason: " + ex.getMessage(), ex);
IamAdminServicesException exception = new IamAdminServicesException();
exception.setMessage("Error getting values from property file, reason " + ex.getMessage());
throw exception;
} catch (Exception ex) {
logger.error("Error resetting user password in keycloak server, reason: " + ex.getMessage(), ex);
IamAdminServicesException exception = new IamAdminServicesException();
exception.setMessage("Error resetting user password in keycloak server, reason: " + ex.getMessage());
throw exception;
} finally {
if (client != null) {
client.close();
}
}
}
use of org.keycloak.admin.client.Keycloak in project airavata by apache.
the class TenantManagementKeycloakImpl method findUser.
@Override
public List<UserProfile> findUser(PasswordCredential realmAdminCreds, String tenantId, String email, String userName) throws IamAdminServicesException {
Keycloak client = null;
try {
client = TenantManagementKeycloakImpl.getClient(ServerSettings.getIamServerUrl(), tenantId, realmAdminCreds);
List<UserRepresentation> retrieveUserList = client.realm(tenantId).users().search(userName, null, null, email, 0, 1);
if (!retrieveUserList.isEmpty()) {
List<UserProfile> userList = new ArrayList<>();
for (UserRepresentation user : retrieveUserList) {
UserProfile profile = new UserProfile();
profile.setUserId(user.getUsername());
profile.setFirstName(user.getFirstName());
profile.setLastName(user.getLastName());
profile.setEmails(Arrays.asList(new String[] { user.getEmail() }));
userList.add(profile);
}
return userList;
} else {
logger.error("requested User not found");
return null;
}
} catch (ApplicationSettingsException ex) {
logger.error("Error getting values from property file, reason: " + ex.getMessage(), ex);
IamAdminServicesException exception = new IamAdminServicesException();
exception.setMessage("Error getting values from property file, reason " + ex.getMessage());
throw exception;
} catch (Exception ex) {
logger.error("Error finding user in keycloak server, reason: " + ex.getMessage(), ex);
IamAdminServicesException exception = new IamAdminServicesException();
exception.setMessage("Error finding user in keycloak server, reason: " + ex.getMessage());
throw exception;
} finally {
if (client != null) {
client.close();
}
}
}
use of org.keycloak.admin.client.Keycloak in project airavata by apache.
the class TenantManagementKeycloakImpl method removeRoleFromUser.
@Override
public boolean removeRoleFromUser(PasswordCredential realmAdminCreds, String tenantId, String username, String roleName) throws IamAdminServicesException {
Keycloak client = null;
try {
client = TenantManagementKeycloakImpl.getClient(ServerSettings.getIamServerUrl(), tenantId, realmAdminCreds);
List<UserRepresentation> retrieveCreatedUserList = client.realm(tenantId).users().search(username, null, null, null, 0, 1);
UserResource retrievedUser = client.realm(tenantId).users().get(retrieveCreatedUserList.get(0).getId());
// Remove role from user
RoleResource roleResource = client.realm(tenantId).roles().get(roleName);
retrievedUser.roles().realmLevel().remove(Arrays.asList(roleResource.toRepresentation()));
return true;
} catch (ApplicationSettingsException ex) {
logger.error("Error getting values from property file, reason: " + ex.getMessage(), ex);
IamAdminServicesException exception = new IamAdminServicesException();
exception.setMessage("Error getting values from property file, reason " + ex.getMessage());
throw exception;
} finally {
if (client != null) {
client.close();
}
}
}
Aggregations