This is an automated email from the ASF dual-hosted git repository. yasith pushed a commit to branch resource-mgmt-rest-api in repository https://gitbox.apache.org/repos/asf/airavata.git
commit 8cf91c330a09a92f2c595c62117f868bbf25067e Author: yasithdev <[email protected]> AuthorDate: Mon Nov 10 18:42:41 2025 -0500 update userprofileservicehandler --- ...ServiceHandler.java => UserProfileService.java} | 53 ++------ .../handlers/UserProfileServiceHandler.java | 151 ++++----------------- 2 files changed, 36 insertions(+), 168 deletions(-) diff --git a/airavata-api/src/main/java/org/apache/airavata/service/profile/handlers/UserProfileServiceHandler.java b/airavata-api/src/main/java/org/apache/airavata/service/UserProfileService.java similarity index 87% copy from airavata-api/src/main/java/org/apache/airavata/service/profile/handlers/UserProfileServiceHandler.java copy to airavata-api/src/main/java/org/apache/airavata/service/UserProfileService.java index 2dec971b1b..ea0553c13b 100644 --- a/airavata-api/src/main/java/org/apache/airavata/service/profile/handlers/UserProfileServiceHandler.java +++ b/airavata-api/src/main/java/org/apache/airavata/service/UserProfileService.java @@ -17,10 +17,9 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.airavata.service.profile.handlers; +package org.apache.airavata.service; import java.util.List; -import org.apache.airavata.common.exception.ApplicationSettingsException; import org.apache.airavata.common.utils.AiravataUtils; import org.apache.airavata.common.utils.Constants; import org.apache.airavata.common.utils.DBEventService; @@ -28,7 +27,6 @@ import org.apache.airavata.common.utils.ServerSettings; import org.apache.airavata.messaging.core.util.DBEventPublisherUtils; import org.apache.airavata.model.dbevent.CrudType; import org.apache.airavata.model.dbevent.EntityType; -import org.apache.airavata.model.error.AuthorizationException; import org.apache.airavata.model.security.AuthzToken; import org.apache.airavata.model.user.Status; import org.apache.airavata.model.user.UserProfile; @@ -37,38 +35,25 @@ import org.apache.airavata.service.profile.client.ProfileServiceClientFactory; import org.apache.airavata.service.profile.iam.admin.services.cpi.IamAdminServices; import org.apache.airavata.service.profile.iam.admin.services.cpi.exception.IamAdminServicesException; import org.apache.airavata.service.profile.user.core.repositories.UserProfileRepository; -import org.apache.airavata.service.profile.user.cpi.UserProfileService; import org.apache.airavata.service.profile.user.cpi.exception.UserProfileServiceException; -import org.apache.airavata.service.profile.user.cpi.profile_user_cpiConstants; import org.apache.airavata.service.security.AiravataSecurityManager; import org.apache.airavata.service.security.SecurityManagerFactory; import org.apache.airavata.service.security.UserInfo; -import org.apache.airavata.service.security.interceptor.SecurityCheck; import org.apache.thrift.TException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class UserProfileServiceHandler implements UserProfileService.Iface { - - private static final Logger logger = LoggerFactory.getLogger(UserProfileServiceHandler.class); +public class UserProfileService { + private static final Logger logger = LoggerFactory.getLogger(UserProfileService.class); private UserProfileRepository userProfileRepository; private DBEventPublisherUtils dbEventPublisherUtils = new DBEventPublisherUtils(DBEventService.USER_PROFILE); - public UserProfileServiceHandler() { - + public UserProfileService() { userProfileRepository = new UserProfileRepository(); } - @Override - public String getAPIVersion() throws TException { - return profile_user_cpiConstants.USER_PROFILE_CPI_VERSION; - } - - @Override - @SecurityCheck - public String initializeUserProfile(AuthzToken authzToken) - throws UserProfileServiceException, AuthorizationException, TException { + public String initializeUserProfile(AuthzToken authzToken) throws UserProfileServiceException { String gatewayId = authzToken.getClaimsMap().get(Constants.GATEWAY_ID); try { // Load UserInfo for the access token and create an initial UserProfile from it @@ -108,10 +93,7 @@ public class UserProfileServiceHandler implements UserProfileService.Iface { } } - @Override - @SecurityCheck - public String addUserProfile(AuthzToken authzToken, UserProfile userProfile) - throws UserProfileServiceException, AuthorizationException, TException { + public String addUserProfile(AuthzToken authzToken, UserProfile userProfile) throws UserProfileServiceException { try { // Lowercase user id and internal id userProfile.setUserId(userProfile.getUserId().toLowerCase()); @@ -135,10 +117,7 @@ public class UserProfileServiceHandler implements UserProfileService.Iface { } } - @Override - @SecurityCheck - public boolean updateUserProfile(AuthzToken authzToken, UserProfile userProfile) - throws UserProfileServiceException, AuthorizationException, TException { + public boolean updateUserProfile(AuthzToken authzToken, UserProfile userProfile) throws UserProfileServiceException { try { // After updating the user profile in the database but before committing the transaction, the // following will update the user profile in the IAM service also. If the update in the IAM service @@ -175,10 +154,8 @@ public class UserProfileServiceHandler implements UserProfileService.Iface { }; } - @Override - @SecurityCheck public UserProfile getUserProfileById(AuthzToken authzToken, String userId, String gatewayId) - throws UserProfileServiceException, AuthorizationException, TException { + throws UserProfileServiceException { try { UserProfile userProfile = userProfileRepository.getUserProfileByIdAndGateWay(userId, gatewayId); if (userProfile != null) return userProfile; @@ -192,10 +169,8 @@ public class UserProfileServiceHandler implements UserProfileService.Iface { } } - @Override - @SecurityCheck public boolean deleteUserProfile(AuthzToken authzToken, String userId, String gatewayId) - throws UserProfileServiceException, AuthorizationException, TException { + throws UserProfileServiceException { try { // find user-profile UserProfile userProfile = userProfileRepository.getUserProfileByIdAndGateWay(userId, gatewayId); @@ -217,10 +192,8 @@ public class UserProfileServiceHandler implements UserProfileService.Iface { } } - @Override - @SecurityCheck public List<UserProfile> getAllUserProfilesInGateway(AuthzToken authzToken, String gatewayId, int offset, int limit) - throws UserProfileServiceException, AuthorizationException, TException { + throws UserProfileServiceException { try { List<UserProfile> usersInGateway = userProfileRepository.getAllUserProfilesInGateway(gatewayId, offset, limit); @@ -234,9 +207,8 @@ public class UserProfileServiceHandler implements UserProfileService.Iface { } } - @Override public boolean doesUserExist(AuthzToken authzToken, String userId, String gatewayId) - throws UserProfileServiceException, AuthorizationException, TException { + throws UserProfileServiceException { try { UserProfile userProfile = userProfileRepository.getUserProfileByIdAndGateWay(userId, gatewayId); return null != userProfile; @@ -253,7 +225,7 @@ public class UserProfileServiceHandler implements UserProfileService.Iface { final int serverPort = Integer.parseInt(ServerSettings.getProfileServiceServerPort()); final String serverHost = ServerSettings.getProfileServiceServerHost(); return ProfileServiceClientFactory.createIamAdminServiceClient(serverHost, serverPort); - } catch (IamAdminServicesException | ApplicationSettingsException e) { + } catch (IamAdminServicesException | org.apache.airavata.common.exception.ApplicationSettingsException e) { logger.error("Failed to create IAM Admin Services client", e); UserProfileServiceException ex = new UserProfileServiceException("Failed to create IAM Admin Services client"); @@ -261,3 +233,4 @@ public class UserProfileServiceHandler implements UserProfileService.Iface { } } } + diff --git a/airavata-api/src/main/java/org/apache/airavata/service/profile/handlers/UserProfileServiceHandler.java b/airavata-api/src/main/java/org/apache/airavata/service/profile/handlers/UserProfileServiceHandler.java index 2dec971b1b..fb4ef9a2d6 100644 --- a/airavata-api/src/main/java/org/apache/airavata/service/profile/handlers/UserProfileServiceHandler.java +++ b/airavata-api/src/main/java/org/apache/airavata/service/profile/handlers/UserProfileServiceHandler.java @@ -20,29 +20,12 @@ package org.apache.airavata.service.profile.handlers; import java.util.List; -import org.apache.airavata.common.exception.ApplicationSettingsException; -import org.apache.airavata.common.utils.AiravataUtils; -import org.apache.airavata.common.utils.Constants; -import org.apache.airavata.common.utils.DBEventService; -import org.apache.airavata.common.utils.ServerSettings; -import org.apache.airavata.messaging.core.util.DBEventPublisherUtils; -import org.apache.airavata.model.dbevent.CrudType; -import org.apache.airavata.model.dbevent.EntityType; import org.apache.airavata.model.error.AuthorizationException; import org.apache.airavata.model.security.AuthzToken; -import org.apache.airavata.model.user.Status; import org.apache.airavata.model.user.UserProfile; -import org.apache.airavata.security.AiravataSecurityException; -import org.apache.airavata.service.profile.client.ProfileServiceClientFactory; -import org.apache.airavata.service.profile.iam.admin.services.cpi.IamAdminServices; -import org.apache.airavata.service.profile.iam.admin.services.cpi.exception.IamAdminServicesException; -import org.apache.airavata.service.profile.user.core.repositories.UserProfileRepository; import org.apache.airavata.service.profile.user.cpi.UserProfileService; import org.apache.airavata.service.profile.user.cpi.exception.UserProfileServiceException; import org.apache.airavata.service.profile.user.cpi.profile_user_cpiConstants; -import org.apache.airavata.service.security.AiravataSecurityManager; -import org.apache.airavata.service.security.SecurityManagerFactory; -import org.apache.airavata.service.security.UserInfo; import org.apache.airavata.service.security.interceptor.SecurityCheck; import org.apache.thrift.TException; import org.slf4j.Logger; @@ -51,13 +34,10 @@ import org.slf4j.LoggerFactory; public class UserProfileServiceHandler implements UserProfileService.Iface { private static final Logger logger = LoggerFactory.getLogger(UserProfileServiceHandler.class); - - private UserProfileRepository userProfileRepository; - private DBEventPublisherUtils dbEventPublisherUtils = new DBEventPublisherUtils(DBEventService.USER_PROFILE); + private org.apache.airavata.service.UserProfileService userProfileService; public UserProfileServiceHandler() { - - userProfileRepository = new UserProfileRepository(); + userProfileService = new org.apache.airavata.service.UserProfileService(); } @Override @@ -69,37 +49,10 @@ public class UserProfileServiceHandler implements UserProfileService.Iface { @SecurityCheck public String initializeUserProfile(AuthzToken authzToken) throws UserProfileServiceException, AuthorizationException, TException { - String gatewayId = authzToken.getClaimsMap().get(Constants.GATEWAY_ID); try { - // Load UserInfo for the access token and create an initial UserProfile from it - UserInfo userInfo = SecurityManagerFactory.getSecurityManager().getUserInfoFromAuthzToken(authzToken); - final UserProfile existingProfile = - userProfileRepository.getUserProfileByIdAndGateWay(userInfo.getUsername(), gatewayId); - // If a user profile already exists, just return the userId - if (existingProfile != null) { - return existingProfile.getUserId(); - } - UserProfile userProfile = new UserProfile(); - userProfile.setUserId(userInfo.getUsername().toLowerCase()); - userProfile.setGatewayId(gatewayId); - userProfile.setAiravataInternalUserId(userProfile.getUserId() + "@" + gatewayId); - userProfile.addToEmails(userInfo.getEmailAddress()); - userProfile.setFirstName(userInfo.getFirstName()); - userProfile.setLastName(userInfo.getLastName()); - userProfile.setCreationTime(AiravataUtils.getCurrentTimestamp().getTime()); - userProfile.setLastAccessTime(AiravataUtils.getCurrentTimestamp().getTime()); - userProfile.setValidUntil(-1); - userProfile.setState(Status.ACTIVE); - userProfile = userProfileRepository.createUserProfile(userProfile); - if (null != userProfile) { - logger.info("Added UserProfile with userId: " + userProfile.getUserId()); - // replicate userProfile at end-places - dbEventPublisherUtils.publish(EntityType.USER_PROFILE, CrudType.CREATE, userProfile); - // return userId - return userProfile.getUserId(); - } else { - throw new Exception("User creation failed. Please try again."); - } + return userProfileService.initializeUserProfile(authzToken); + } catch (UserProfileServiceException e) { + throw e; } catch (Exception e) { logger.error("Error while initializing user profile", e); UserProfileServiceException exception = new UserProfileServiceException(); @@ -113,20 +66,9 @@ public class UserProfileServiceHandler implements UserProfileService.Iface { public String addUserProfile(AuthzToken authzToken, UserProfile userProfile) throws UserProfileServiceException, AuthorizationException, TException { try { - // Lowercase user id and internal id - userProfile.setUserId(userProfile.getUserId().toLowerCase()); - userProfile.setAiravataInternalUserId(userProfile.getUserId() + "@" + userProfile.getGatewayId()); - userProfile = userProfileRepository.updateUserProfile( - userProfile, getIAMUserProfileUpdater(authzToken, userProfile)); - if (null != userProfile) { - logger.info("Added UserProfile with userId: " + userProfile.getUserId()); - // replicate userProfile at end-places - dbEventPublisherUtils.publish(EntityType.USER_PROFILE, CrudType.CREATE, userProfile); - // return userId - return userProfile.getUserId(); - } else { - throw new Exception("User creation failed. Please try again."); - } + return userProfileService.addUserProfile(authzToken, userProfile); + } catch (UserProfileServiceException e) { + throw e; } catch (Exception e) { logger.error("Error while creating user profile", e); UserProfileServiceException exception = new UserProfileServiceException(); @@ -140,17 +82,9 @@ public class UserProfileServiceHandler implements UserProfileService.Iface { public boolean updateUserProfile(AuthzToken authzToken, UserProfile userProfile) throws UserProfileServiceException, AuthorizationException, TException { try { - // After updating the user profile in the database but before committing the transaction, the - // following will update the user profile in the IAM service also. If the update in the IAM service - // fails then the transaction will be rolled back. - Runnable iamUserProfileUpdater = getIAMUserProfileUpdater(authzToken, userProfile); - if (userProfileRepository.updateUserProfile(userProfile, iamUserProfileUpdater) != null) { - logger.info("Updated UserProfile with userId: " + userProfile.getUserId()); - // replicate userProfile at end-places - dbEventPublisherUtils.publish(EntityType.USER_PROFILE, CrudType.UPDATE, userProfile); - return true; - } - return false; + return userProfileService.updateUserProfile(authzToken, userProfile); + } catch (UserProfileServiceException e) { + throw e; } catch (Exception e) { logger.error("Error while Updating user profile", e); UserProfileServiceException exception = new UserProfileServiceException(); @@ -159,31 +93,14 @@ public class UserProfileServiceHandler implements UserProfileService.Iface { } } - private Runnable getIAMUserProfileUpdater(AuthzToken authzToken, UserProfile userProfile) - throws UserProfileServiceException { - String gatewayId = authzToken.getClaimsMap().get(Constants.GATEWAY_ID); - return () -> { - try { - AiravataSecurityManager securityManager = SecurityManagerFactory.getSecurityManager(); - AuthzToken serviceAccountAuthzToken = - securityManager.getUserManagementServiceAccountAuthzToken(gatewayId); - IamAdminServices.Client iamAdminServicesClient = getIamAdminServicesClient(); - iamAdminServicesClient.updateUserProfile(serviceAccountAuthzToken, userProfile); - } catch (AiravataSecurityException | TException e) { - throw new RuntimeException("Failed to update user profile in IAM service", e); - } - }; - } - @Override @SecurityCheck public UserProfile getUserProfileById(AuthzToken authzToken, String userId, String gatewayId) throws UserProfileServiceException, AuthorizationException, TException { try { - UserProfile userProfile = userProfileRepository.getUserProfileByIdAndGateWay(userId, gatewayId); - if (userProfile != null) return userProfile; - else - throw new Exception("User with userId: " + userId + ", in Gateway: " + gatewayId + ", does not exist."); + return userProfileService.getUserProfileById(authzToken, userId, gatewayId); + } catch (UserProfileServiceException e) { + throw e; } catch (Exception e) { logger.error("Error retrieving user profile by ID", e); UserProfileServiceException exception = new UserProfileServiceException(); @@ -197,18 +114,9 @@ public class UserProfileServiceHandler implements UserProfileService.Iface { public boolean deleteUserProfile(AuthzToken authzToken, String userId, String gatewayId) throws UserProfileServiceException, AuthorizationException, TException { try { - // find user-profile - UserProfile userProfile = userProfileRepository.getUserProfileByIdAndGateWay(userId, gatewayId); - - // delete user - boolean deleteSuccess = userProfileRepository.delete(userId); - logger.info("Delete UserProfile with userId: " + userId + ", " + (deleteSuccess ? "Success!" : "Failed!")); - - if (deleteSuccess) { - // delete userProfile at end-places - dbEventPublisherUtils.publish(EntityType.USER_PROFILE, CrudType.DELETE, userProfile); - } - return deleteSuccess; + return userProfileService.deleteUserProfile(authzToken, userId, gatewayId); + } catch (UserProfileServiceException e) { + throw e; } catch (Exception e) { logger.error("Error while deleting user profile", e); UserProfileServiceException exception = new UserProfileServiceException(); @@ -222,10 +130,9 @@ public class UserProfileServiceHandler implements UserProfileService.Iface { public List<UserProfile> getAllUserProfilesInGateway(AuthzToken authzToken, String gatewayId, int offset, int limit) throws UserProfileServiceException, AuthorizationException, TException { try { - List<UserProfile> usersInGateway = - userProfileRepository.getAllUserProfilesInGateway(gatewayId, offset, limit); - if (usersInGateway != null) return usersInGateway; - else throw new Exception("There are no users for the requested gatewayId: " + gatewayId); + return userProfileService.getAllUserProfilesInGateway(authzToken, gatewayId, offset, limit); + } catch (UserProfileServiceException e) { + throw e; } catch (Exception e) { logger.error("Error while retrieving user profile List", e); UserProfileServiceException exception = new UserProfileServiceException(); @@ -238,8 +145,9 @@ public class UserProfileServiceHandler implements UserProfileService.Iface { public boolean doesUserExist(AuthzToken authzToken, String userId, String gatewayId) throws UserProfileServiceException, AuthorizationException, TException { try { - UserProfile userProfile = userProfileRepository.getUserProfileByIdAndGateWay(userId, gatewayId); - return null != userProfile; + return userProfileService.doesUserExist(authzToken, userId, gatewayId); + } catch (UserProfileServiceException e) { + throw e; } catch (Exception e) { logger.error("Error while finding user profile", e); UserProfileServiceException exception = new UserProfileServiceException(); @@ -247,17 +155,4 @@ public class UserProfileServiceHandler implements UserProfileService.Iface { throw exception; } } - - private IamAdminServices.Client getIamAdminServicesClient() throws UserProfileServiceException { - try { - final int serverPort = Integer.parseInt(ServerSettings.getProfileServiceServerPort()); - final String serverHost = ServerSettings.getProfileServiceServerHost(); - return ProfileServiceClientFactory.createIamAdminServiceClient(serverHost, serverPort); - } catch (IamAdminServicesException | ApplicationSettingsException e) { - logger.error("Failed to create IAM Admin Services client", e); - UserProfileServiceException ex = - new UserProfileServiceException("Failed to create IAM Admin Services client"); - throw ex; - } - } }
