diff options
author | 2017-04-27 07:53:18 -0400 | |
---|---|---|
committer | 2017-04-27 08:31:27 -0400 | |
commit | 6beb446925c967aca92f5513adf36c5db77c00d6 (patch) | |
tree | 9392057ed0739de2445c5b2a2a8bee6dcdacbcf7 /ecomp-sdk/epsdk-app-common/src/main/java/org/openecomp/portalapp/service | |
parent | 246b225194e3e8dc1926294de591a94fd9787fa7 (diff) |
[PORTAL-7] Rebase
This rebasing includes common libraries and common overlays projects
abstraction of components
Change-Id: Ia1efa4deacdc5701e6205104ac021a6c80ed60ba
Signed-off-by: st782s <statta@research.att.com>
Diffstat (limited to 'ecomp-sdk/epsdk-app-common/src/main/java/org/openecomp/portalapp/service')
2 files changed, 426 insertions, 0 deletions
diff --git a/ecomp-sdk/epsdk-app-common/src/main/java/org/openecomp/portalapp/service/IAdminAuthExtension.java b/ecomp-sdk/epsdk-app-common/src/main/java/org/openecomp/portalapp/service/IAdminAuthExtension.java new file mode 100644 index 00000000..c670c2ba --- /dev/null +++ b/ecomp-sdk/epsdk-app-common/src/main/java/org/openecomp/portalapp/service/IAdminAuthExtension.java @@ -0,0 +1,65 @@ +/*- + * ================================================================================ + * ECOMP Portal SDK + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ================================================================================ + */ + +package org.openecomp.portalapp.service; + +import java.util.Set; + +import org.openecomp.portalsdk.core.domain.Role; +import org.openecomp.portalsdk.core.domain.User; + +/** + * Defines methods that can be used to extend the features of the user/role + * provisioning class supplied in EPSDK-Common. + */ +public interface IAdminAuthExtension { + + /** + * Invoked from OnBoardingApiServiceImpl#pushUser after user was saved. App + * developers can provide a class with their own logic following this + * action; for example, updating app's related tables + * + * @param user + * User object sent by Portal + */ + public void saveUserExtension(User user); + + /** + * Invoked from OnBoardingApiServiceImpl#editUser after user was updated. + * App developers can provide a class with their own logic following this + * action; for example, updating app's related tables + * + * @param user + * User object sent by Portal + */ + public void editUserExtension(User user); + + /** + * Invoked from OnBoardingApiServiceImpl#pushUserRole after user's roles + * were updated. App developers can provide a class with their own logic + * following this action; for example, updating app's related tables + * + * @param roles + * Roles object sent by Portal + * @param user + * User object sent by Portal + */ + public void saveUserRoleExtension(Set<Role> roles, User user); +} diff --git a/ecomp-sdk/epsdk-app-common/src/main/java/org/openecomp/portalapp/service/OnBoardingApiServiceImpl.java b/ecomp-sdk/epsdk-app-common/src/main/java/org/openecomp/portalapp/service/OnBoardingApiServiceImpl.java new file mode 100644 index 00000000..cb822494 --- /dev/null +++ b/ecomp-sdk/epsdk-app-common/src/main/java/org/openecomp/portalapp/service/OnBoardingApiServiceImpl.java @@ -0,0 +1,361 @@ +/*- + * ================================================================================ + * ECOMP Portal SDK + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ================================================================================ + */ +package org.openecomp.portalapp.service; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Set; +import java.util.SortedSet; +import java.util.TreeSet; + +import javax.servlet.http.HttpServletRequest; + +import org.openecomp.portalsdk.core.auth.LoginStrategy; +import org.openecomp.portalsdk.core.domain.Role; +import org.openecomp.portalsdk.core.domain.User; +import org.openecomp.portalsdk.core.domain.UserApp; +import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate; +import org.openecomp.portalsdk.core.onboarding.client.AppContextManager; +import org.openecomp.portalsdk.core.onboarding.crossapi.IPortalRestAPIService; +import org.openecomp.portalsdk.core.onboarding.exception.PortalAPIException; +import org.openecomp.portalsdk.core.onboarding.listener.PortalTimeoutHandler; +import org.openecomp.portalsdk.core.restful.domain.EcompRole; +import org.openecomp.portalsdk.core.restful.domain.EcompUser; +import org.openecomp.portalsdk.core.service.RoleService; +import org.openecomp.portalsdk.core.service.UserProfileService; +import org.openecomp.portalsdk.core.service.WebServiceCallService; +import org.openecomp.portalsdk.core.util.JSONUtil; +import org.openecomp.portalsdk.core.util.SystemProperties; +import org.openecomp.portalsdk.core.web.support.UserUtils; +import org.slf4j.MDC; +import org.springframework.context.ApplicationContext; + +/** + * Implements the REST API interface to answer requests made by Portal app about + * users and active sessions. + * + * Since an instance of this class will be instantiated by the OnBoarding + * servlet from the ecompFW library, we cannot use Spring injections here. This + * 'injection' is done indirectly using AppContextManager class. + * + * @author Ikram Ikramullah + * + */ +public class OnBoardingApiServiceImpl implements IPortalRestAPIService { + + private RoleService roleService; + private UserProfileService userProfileService; + private IAdminAuthExtension adminAuthExtensionServiceImpl; + + private LoginStrategy loginStrategy; + + EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(OnBoardingApiServiceImpl.class); + + public OnBoardingApiServiceImpl() { + // Defend against null-pointer exception during server startup + // that was caused by a spurious Spring annotation on this class. + ApplicationContext appContext = AppContextManager.getAppContext(); + if (appContext == null) + throw new RuntimeException("OnBoardingApiServiceImpl ctor failed to get appContext"); + roleService = appContext.getBean(RoleService.class); + userProfileService = appContext.getBean(UserProfileService.class); + loginStrategy = appContext.getBean(LoginStrategy.class); + // initialize the base class definition for Admin Auth Extension + adminAuthExtensionServiceImpl = appContext.getBean(IAdminAuthExtension.class); + } + + private void setCurrentAttributes(User user, EcompUser userJson) { + + user.setEmail(userJson.getEmail()); + user.setFirstName(userJson.getFirstName()); + user.setHrid(userJson.getHrid()); + user.setJobTitle(userJson.getJobTitle()); + user.setLastName(userJson.getLastName()); + user.setLoginId(userJson.getLoginId()); + user.setOrgManagerUserId(userJson.getOrgManagerUserId()); + user.setMiddleInitial(userJson.getMiddleInitial()); + user.setOrgCode(userJson.getOrgCode()); + user.setOrgId(userJson.getOrgId()); + user.setPhone(userJson.getPhone()); + user.setOrgUserId(userJson.getOrgUserId()); + user.setActive(userJson.isActive()); + // user.setRoles(new TreeSet(userJson.getRoles())); + } + + @Override + public void pushUser(EcompUser userJson) throws PortalAPIException { + + if (logger.isDebugEnabled()) + logger.debug(EELFLoggerDelegate.debugLogger, "pushUser was invoked: {}", userJson); + User user = new User(); + String response = ""; + try { + // Set input attributes to the object obout to be saved + setCurrentAttributes(user, userJson); + user.setRoles(new TreeSet<Role>()); + user.setUserApps(new TreeSet<UserApp>()); + user.setPseudoRoles(new TreeSet<Role>()); + userProfileService.saveUser(user); + logger.debug(EELFLoggerDelegate.debugLogger, "push user success."); + + // After successful creation, call admin auth extension + if (adminAuthExtensionServiceImpl != null) { + try { + adminAuthExtensionServiceImpl.saveUserExtension(user); + } catch (Exception ex) { + logger.error("pushUser: saveUserExtension failed", ex); + } + } + + response = "push user success."; + response = JSONUtil.convertResponseToJSON(response); + } catch (Exception e) { + response = "OnboardingApiService.pushUser failed"; + logger.error(EELFLoggerDelegate.errorLogger, response, e); + throw new PortalAPIException(response, e); + } finally { + MDC.remove(SystemProperties.MDC_TIMER); + } + } + + @Override + public void editUser(String loginId, EcompUser userJson) throws PortalAPIException { + + if (logger.isDebugEnabled()) + logger.debug(EELFLoggerDelegate.debugLogger, "OnboardingApi editUser was invoked with loginID {}, JSON {}", + loginId, userJson); + User editUser = new User(); + String response = ""; + try { + setCurrentAttributes(editUser, userJson); + if (editUser.getOrgUserId() != null) { + editUser.setLoginId(editUser.getOrgUserId()); + } + User domainUser = userProfileService.getUserByLoginId(loginId); + if (domainUser != null) + domainUser = JSONUtil.mapToDomainUser(domainUser, editUser); + else + domainUser = editUser; + userProfileService.saveUser(domainUser); + logger.debug(EELFLoggerDelegate.debugLogger, "edit user success."); + + // After successful edit, call the admin auth extension + if (adminAuthExtensionServiceImpl != null) { + try { + adminAuthExtensionServiceImpl.editUserExtension(domainUser); + } catch (Exception ex) { + logger.error("editUser: editUserExtension failed", ex); + } + } + + response = "edit user success."; + response = JSONUtil.convertResponseToJSON(response); + } catch (Exception e) { + response = "OnboardingApiService.editUser failed"; + logger.error(EELFLoggerDelegate.errorLogger, response, e); + throw new PortalAPIException(response, e); + } finally { + MDC.remove(SystemProperties.MDC_TIMER); + } + + // return response; + } + + @Override + public EcompUser getUser(String loginId) throws PortalAPIException { + try { + if (logger.isDebugEnabled()) + logger.debug(EELFLoggerDelegate.debugLogger, "## REST API ## loginId: {}", loginId); + User user = userProfileService.getUserByLoginId(loginId); + if (user == null) { + logger.info(EELFLoggerDelegate.debugLogger, "User + " + loginId + " doesn't exist"); + return null; + // Unfortunately, Portal is not ready to accept proper error + // response yet .. + // commenting throw clauses until portal is ready + // throw new PortalAPIException("User + " + loginId + " doesn't + // exist"); + } else + return UserUtils.convertToEcompUser(user); + } catch (Exception e) { + String response = "OnboardingApiService.getUser failed"; + logger.error(EELFLoggerDelegate.errorLogger, response, e); + return null; + // Unfortunately, Portal is not ready to accept proper error response + // yet .. commenting throw clauses until portal is ready + // throw new PortalAPIException(response, e); + } + + } + + @Override + public List<EcompUser> getUsers() throws PortalAPIException { + try { + List<User> users = userProfileService.findAllActive(); + List<EcompUser> ecompUsers = new ArrayList<EcompUser>(); + for (User user : users) + ecompUsers.add(UserUtils.convertToEcompUser(user)); + return ecompUsers; + } catch (Exception e) { + String response = "OnboardingApiService.getUsers failed"; + logger.error(EELFLoggerDelegate.errorLogger, response, e); + throw new PortalAPIException(response, e); + } + } + + @Override + public List<EcompRole> getAvailableRoles() throws PortalAPIException { + try { + List<Role> roles = roleService.getActiveRoles(); + List<EcompRole> ecompRoles = new ArrayList<EcompRole>(); + for (Role role : roles) + ecompRoles.add(UserUtils.convertToEcompRole(role)); + return ecompRoles; + } catch (Exception e) { + String response = "OnboardingApiService.getAvailableRoles failed"; + logger.error(EELFLoggerDelegate.errorLogger, response, e); + throw new PortalAPIException(response, e); + } + } + + @Override + public void pushUserRole(String loginId, List<EcompRole> rolesJson) throws PortalAPIException { + String response = ""; + try { + if (logger.isDebugEnabled()) + logger.debug(EELFLoggerDelegate.debugLogger, "## REST API ## loginId: {}, roles Json {}", loginId, + rolesJson); + User user = userProfileService.getUserByLoginId(loginId); + /* + * List<EcompRole> ecompRoles = mapper.readValue(rolesJson, + * TypeFactory.defaultInstance().constructCollectionType(List.class, + * EcompRole.class)); + */ + SortedSet<Role> roles = new TreeSet<Role>(); + for (EcompRole role : rolesJson) { + roles.add(roleService.getRole(role.getId())); + } + // Replace existing roles with new ones + replaceExistingRoles(roles, user); + + logger.debug(EELFLoggerDelegate.debugLogger, "push user role success."); + + // After successful creation, call admin auth extension + if (adminAuthExtensionServiceImpl != null) { + try { + adminAuthExtensionServiceImpl.saveUserRoleExtension(roles, user); + } catch (Exception ex) { + logger.error("pushUserRole: saveUserRoleExtension failed", ex); + } + } + response = "push user role success."; + response = JSONUtil.convertResponseToJSON(response); + + } catch (Exception e) { + response = "OnboardingApiService.pushUserRole failed"; + logger.error(EELFLoggerDelegate.errorLogger, response, e); + throw new PortalAPIException(response, e); + } finally { + MDC.remove(SystemProperties.MDC_TIMER); + } + + } + + @Override + public List<EcompRole> getUserRoles(String loginId) throws PortalAPIException { + if (logger.isDebugEnabled()) + logger.debug(EELFLoggerDelegate.debugLogger, "## REST API ## loginId: {}", loginId); + List<EcompRole> ecompRoles = new ArrayList<EcompRole>(); + try { + User user = userProfileService.getUserByLoginId(loginId); + SortedSet<Role> currentRoles = null; + if (user != null) { + currentRoles = user.getRoles(); + if (currentRoles != null) + for (Role role : currentRoles) + ecompRoles.add(UserUtils.convertToEcompRole(role)); + } + return ecompRoles; + } catch (Exception e) { + String response = "OnboardingApiService.getUserRoles failed"; + logger.error(EELFLoggerDelegate.errorLogger, response, e); + throw new PortalAPIException(response, e); + } + } + + @SuppressWarnings("unchecked") + private void replaceExistingRoles(SortedSet<Role> roles, User user) { + // 1. remove existing roles + Set<UserApp> userApps = user.getUserApps(); + Iterator<UserApp> appsItr = userApps.iterator(); + while (appsItr.hasNext()) { + UserApp tempUserApp = appsItr.next(); + boolean roleFound = false; + for (Role role : roles) { + if (tempUserApp.getRole().getId().equals(role.getId())) { + roleFound = true; + break; + } + } + if (!roleFound) + appsItr.remove(); + } + user.setUserApps(userApps); + userProfileService.saveUser(user); + + // 2. add new roles + user.setRoles(roles); + userProfileService.saveUser(user); + } + + @Override + public boolean isAppAuthenticated(HttpServletRequest request) throws PortalAPIException { + WebServiceCallService securityService = AppContextManager.getAppContext().getBean(WebServiceCallService.class); + try { + String appUser = request.getHeader("username"); + String password = request.getHeader("password"); + // System.out.println("username = " + appUser); + // System.out.println("password = " + password); + boolean flag = securityService.verifyRESTCredential(null, appUser, password); + // System.out.println("username = " + appUser); + // System.out.println("password = " + password); + return flag; + + } catch (Exception e) { + String response = "OnboardingApiService.isAppAuthenticated failed"; + logger.error(EELFLoggerDelegate.errorLogger, response, e); + throw new PortalAPIException(response, e); + } + } + + public String getSessionTimeOuts() throws Exception { + return PortalTimeoutHandler.gatherSessionExtensions(); + } + + public void updateSessionTimeOuts(String sessionMap) throws Exception { + PortalTimeoutHandler.updateSessionExtensions(sessionMap); + } + + @Override + public String getUserId(HttpServletRequest request) throws PortalAPIException { + return loginStrategy.getUserId(request); + } +} |