+ if (!userInfo.isEmpty()) {
+ validateExternalRequestFields(app);
+ user = userInfo.get(0);
+ epRequestId = epUserRolesRequestService.userAppRolesRequestList(user.getId(), app.getId());
+ epRequestIdSize = epRequestId.size();
+ }
+ if (!app.getId().equals(PortalConstants.PORTAL_APP_ID) && !app.getAuthCentral()) {
+ logger.debug(EELFLoggerDelegate.debugLogger,
+ "setExternalRequestUserAppRole: Starting GET roles for app {}", app.getId());
+ EcompRole[] appRoles = applicationsRestClientService.get(EcompRole[].class, app.getId(), "/roles");
+ logger.debug(EELFLoggerDelegate.debugLogger,
+ "setExternalRequestUserAppRole: Finshed GET roles for app {} and payload {}", app.getId(),
+ appRoles);
+ if (appRoles.length > 0) {
+ syncAppRoles(app.getId(), appRoles);
+ }
+ }
+ List<RoleInAppForUser> roleInAppForUserList = roleInAppForUserList(newAppRolesForUser.getRoles(),
+ app.getId(), app.getMlAppName());
+ List<EcompUserAppRoles> userRoleList = null;
+ if (!userInfo.isEmpty()) {
+ userRoleList = ecompUserAppRolesService.getUserAppExistingRoles(app.getId(), user.getId());
+ }
+ // Check if list contains just account admin role
+ boolean checkIfAdminRoleExists = false;
+ if (reqType.equals("DELETE") && userRoleList != null) {
+ checkIfAdminRoleExists = userRoleList.stream()
+ .anyMatch(userRole -> userRole.getRoleId().equals(PortalConstants.ACCOUNT_ADMIN_ROLE_ID));
+ } else {
+ checkIfAdminRoleExists = roleInAppForUserList.stream()
+ .anyMatch(roleList -> roleList.getRoleId().equals(PortalConstants.ACCOUNT_ADMIN_ROLE_ID));
+ }
+ if (app.getAuthCentral()) {
+ try {
+ if (!(app.getId().equals(PortalConstants.PORTAL_APP_ID) && reqType.equals("DELETE"))
+ && ((checkIfAdminRoleExists && roleInAppForUserList.size() > 1)
+ || (!checkIfAdminRoleExists && roleInAppForUserList.size() >= 1))) {
+ List<RoleInAppForUser> remoteUserRoles = new ArrayList<>(roleInAppForUserList);
+ remoteUserRoles.removeIf(role -> {
+ return (role.getRoleId().equals(PortalConstants.ACCOUNT_ADMIN_ROLE_ID));
+ });
+ String orgUserIdNewOrExist = (!userInfo.isEmpty()) ? user.getOrgUserId() : orgUserId;
+ pushRemoteUser(remoteUserRoles, orgUserIdNewOrExist, app, mapper,
+ applicationsRestClientService, true);
+ }
+ } catch (Exception e) {
+ reqMessage = e.getMessage();
+ logger.error(EELFLoggerDelegate.errorLogger,
+ "setExternalRequestUserAppRole: Failed to added remote user", e);
+ throw new Exception(reqMessage);
+ }
+ Set<EcompRole> userRolesInLocalApp = postUsersRolesToLocalApp(roleInAppForUserList);
+ RolesInAppForUser rolesInAppForUser = constructRolesInAppForUserUpdate(orgUserId, app.getId(),
+ userRolesInLocalApp);
+ List<RoleInAppForUser> roleAppUserList = rolesInAppForUser.getRoles();
+ Set<EcompRole> rolesGotDeletedByApprover = new TreeSet<>();
+ if (EcompPortalUtils.checkIfRemoteCentralAccessAllowed()) {
+ updateUserRolesInExternalSystem(app, rolesInAppForUser.getOrgUserId(), roleAppUserList,
+ externalSystemRequest, false, rolesGotDeletedByApprover, false);
+ }
+ logger.info(EELFLoggerDelegate.debugLogger,
+ "setExternalRequestUserAppRole: {} user app roles: for app {}, user {}", logMessage,
+ newAppRolesForUser.getApplicationName(), newAppRolesForUser.getLoginId());
+ result = applyChangesInUserRolesForAppToEcompDB(rolesInAppForUser, externalSystemRequest, reqType,
+ false, rolesGotDeletedByApprover, false);
+ } else if (!app.getAuthCentral() && app.getId().equals(PortalConstants.PORTAL_APP_ID)) {
+ Set<EcompRole> userRolesInLocalApp = postUsersRolesToLocalApp(roleInAppForUserList);
+ RolesInAppForUser rolesInAppForUser = constructRolesInAppForUserUpdate(orgUserId, app.getId(),
+ userRolesInLocalApp);
+ Set<EcompRole> rolesGotDeletedByApprover = new TreeSet<>();
+
+ result = applyChangesInUserRolesForAppToEcompDB(rolesInAppForUser, externalSystemRequest, reqType,
+ false, rolesGotDeletedByApprover, false);
+ } else {
+ if (!((roleInAppForUserList.size() == 1 || reqType.equals("DELETE")) && checkIfAdminRoleExists)) {
+ FnUser remoteAppUser = null;
+ remoteAppUser = checkIfRemoteUserExits(orgUserId, app, applicationsRestClientService);
+ if (remoteAppUser == null) {
+ addRemoteUser(roleInAppForUserList, orgUserId, app, mapper,
+ applicationsRestClientService);
+ reqMessage = "Saved Successfully";
+ }
+ Set<EcompRole> userRolesInRemoteApp = postUsersRolesToRemoteApp(roleInAppForUserList, mapper,
+ applicationsRestClientService, app.getId(), orgUserId);
+ RolesInAppForUser rolesInAppForUser = constructRolesInAppForUserUpdate(orgUserId, app.getId(),
+ userRolesInRemoteApp);
+ logger.info(EELFLoggerDelegate.debugLogger,
+ "setExternalRequestUserAppRole: {} user app roles: for app {}, user {}", logMessage,
+ newAppRolesForUser.getApplicationName(), newAppRolesForUser.getLoginId());
+ Set<EcompRole> rolesGotDeletedByApprover = new TreeSet<>();
+ result = applyChangesInUserRolesForAppToEcompDB(rolesInAppForUser, externalSystemRequest,
+ reqType, false, rolesGotDeletedByApprover, false);
+ } else {
+ if (!(reqType.equals("DELETE")) && userInfo.isEmpty()) {
+ reqMessage = "Saved Successfully";
+ }
+ Set<EcompRole> userRolesInRemoteApp = constructUsersEcompRoles(roleInAppForUserList);
+ RolesInAppForUser rolesInAppForUser = constructRolesInAppForUserUpdate(orgUserId, app.getId(),
+ userRolesInRemoteApp);
+ logger.info(EELFLoggerDelegate.debugLogger,
+ "setExternalRequestUserAppRole: {} user app roles: for app {}, user {}",
+ logMessage, newAppRolesForUser.getApplicationName(), newAppRolesForUser.getLoginId());
+ Set<EcompRole> rolesGotDeletedByApprover = new TreeSet<>();
+ result = applyChangesInUserRolesForAppToEcompDB(rolesInAppForUser, externalSystemRequest,
+ reqType, false, rolesGotDeletedByApprover, false);
+ }
+ if (!result) {
+ reqMessage = "Failed to save the user app role(s)";
+ }
+ if (epRequestIdSize > 0 && !userInfo.isEmpty()) {
+ updateStatus = "C";
+ applyChangesToAppRolesRequest(user.getId(), updateStatus, epRequestId.get(0));
+ }