From 7246eabfd23d6cadc9f658f666df62b93f30ed70 Mon Sep 17 00:00:00 2001 From: st782s Date: Tue, 20 Nov 2018 07:31:32 -0500 Subject: CADI Integration Issue-ID: PORTAL-474 System to system authorization using CADI Change-Id: I76487f8155a36fca8283669fe5e28ec7d5aec91d Signed-off-by: st782s --- .../service/OnBoardingApiServiceImpl.java | 81 +++++++++++++++++++--- 1 file changed, 72 insertions(+), 9 deletions(-) (limited to 'ecomp-sdk/epsdk-app-common/src/main/java/org/onap/portalapp/service/OnBoardingApiServiceImpl.java') diff --git a/ecomp-sdk/epsdk-app-common/src/main/java/org/onap/portalapp/service/OnBoardingApiServiceImpl.java b/ecomp-sdk/epsdk-app-common/src/main/java/org/onap/portalapp/service/OnBoardingApiServiceImpl.java index 966eb8f2..139f69a2 100644 --- a/ecomp-sdk/epsdk-app-common/src/main/java/org/onap/portalapp/service/OnBoardingApiServiceImpl.java +++ b/ecomp-sdk/epsdk-app-common/src/main/java/org/onap/portalapp/service/OnBoardingApiServiceImpl.java @@ -38,6 +38,7 @@ package org.onap.portalapp.service; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.Iterator; import java.util.List; @@ -45,12 +46,14 @@ import java.util.Map; import java.util.Set; import java.util.SortedSet; import java.util.TreeSet; +import java.util.stream.Collectors; import javax.servlet.http.HttpServletRequest; import org.onap.portalsdk.core.auth.LoginStrategy; import org.onap.portalsdk.core.domain.App; import org.onap.portalsdk.core.domain.Role; +import org.onap.portalsdk.core.domain.RoleFunction; import org.onap.portalsdk.core.domain.User; import org.onap.portalsdk.core.domain.UserApp; import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate; @@ -64,6 +67,7 @@ import org.onap.portalsdk.core.onboarding.util.CipherUtil; import org.onap.portalsdk.core.onboarding.util.PortalApiConstants; import org.onap.portalsdk.core.onboarding.util.PortalApiProperties; import org.onap.portalsdk.core.restful.domain.EcompRole; +import org.onap.portalsdk.core.restful.domain.EcompRoleFunction; import org.onap.portalsdk.core.restful.domain.EcompUser; import org.onap.portalsdk.core.service.AppService; import org.onap.portalsdk.core.service.RestApiRequestBuilder; @@ -77,6 +81,7 @@ import org.onap.portalsdk.core.web.support.UserUtils; import org.slf4j.MDC; import org.springframework.context.ApplicationContext; +import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.type.TypeFactory; @@ -120,12 +125,14 @@ public class OnBoardingApiServiceImpl implements IPortalRestAPIService, IPortalR // initialize the base class definition for Admin Auth Extension adminAuthExtensionServiceImpl = appContext.getBean(IAdminAuthExtension.class); userService = appContext.getBean(UserService.class); - if(isCentralized.equals(isAccessCentralized)){ - restApiRequestBuilder = appContext.getBean(RestApiRequestBuilder.class); appServiceImpl = appContext.getBean(AppService.class); + + if(isCentralized.equals(isAccessCentralized)){ + restApiRequestBuilder = appContext.getBean(RestApiRequestBuilder.class); } } + @SuppressWarnings("unchecked") private void setCurrentAttributes(User user, EcompUser userJson) { user.setEmail(userJson.getEmail()); @@ -141,12 +148,11 @@ public class OnBoardingApiServiceImpl implements IPortalRestAPIService, IPortalR user.setPhone(userJson.getPhone()); user.setOrgUserId(userJson.getOrgUserId()); user.setActive(userJson.isActive()); - // user.setRoles(new TreeSet(userJson.getRoles())); +// 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(); @@ -157,10 +163,28 @@ public class OnBoardingApiServiceImpl implements IPortalRestAPIService, IPortalR user.setRoles(new TreeSet()); user.setUserApps(new TreeSet()); user.setPseudoRoles(new TreeSet()); - userProfileService.saveUser(user); + + User domainUser = userProfileService.getUserByLoginId(user.getLoginId()); + if (domainUser != null) { + JSONUtil.mapToDomainUser(domainUser, user); + userProfileService.saveUser(domainUser); + } else { + userProfileService.saveUser(user); + } logger.debug(EELFLoggerDelegate.debugLogger, "push user success."); // After successful creation, call admin auth extension + + Set ecompRoles = userJson.getRoles(); + SortedSet roles = new TreeSet<>(); + Iterator roleIter = ecompRoles.iterator(); + ObjectMapper mapper = new ObjectMapper(); + while (roleIter.hasNext()) { + Object nextValue = roleIter.next(); + EcompRole epRole = mapper.convertValue(nextValue, EcompRole.class); + roles.add(convertToRole(epRole)); + } + user.setRoles(roles); if (adminAuthExtensionServiceImpl != null) { try { adminAuthExtensionServiceImpl.saveUserExtension(user); @@ -168,7 +192,6 @@ public class OnBoardingApiServiceImpl implements IPortalRestAPIService, IPortalR logger.error("pushUser: saveUserExtension failed", ex); } } - response = "push user success."; response = JSONUtil.convertResponseToJSON(response); } catch (Exception e) { @@ -179,6 +202,35 @@ public class OnBoardingApiServiceImpl implements IPortalRestAPIService, IPortalR MDC.remove(SystemProperties.MDC_TIMER); } } + + public Role convertToRole(EcompRole epRole) { + Role role = new Role(); + role.setId(epRole.getId()); + role.setName(epRole.getName()); + role.setActive(true); + Set ecompRolefunctions = new TreeSet<>(); + @SuppressWarnings("unchecked") + Set rolefunctions = epRole.getRoleFunctions(); + ObjectMapper mapper = new ObjectMapper(); + Iterator roleFnIter = rolefunctions.iterator(); + while (roleFnIter.hasNext()) { + Object nextValue = roleFnIter.next(); + EcompRoleFunction epRoleFunction = mapper.convertValue(nextValue, EcompRoleFunction.class); + ecompRolefunctions.add(convertToRoleFunction(epRoleFunction)); + } + role.setRoleFunctions(ecompRolefunctions); + return role; + } + + public RoleFunction convertToRoleFunction(EcompRoleFunction rolefun) { + RoleFunction roleFunction = new RoleFunction(); + roleFunction.setName(rolefun.getName()); + roleFunction.setCode(rolefun.getCode()); + roleFunction.setType(rolefun.getType()); + roleFunction.setAction(rolefun.getAction()); + return roleFunction; + } + @Override public void editUser(String loginId, EcompUser userJson) throws PortalAPIException { @@ -200,7 +252,19 @@ public class OnBoardingApiServiceImpl implements IPortalRestAPIService, IPortalR domainUser = editUser; userProfileService.saveUser(domainUser); logger.debug(EELFLoggerDelegate.debugLogger, "edit user success."); - + + + Set ecompRoles = userJson.getRoles(); + SortedSet roles = new TreeSet<>(); + Iterator roleIter = ecompRoles.iterator(); + ObjectMapper mapper = new ObjectMapper(); + while (roleIter.hasNext()) { + Object nextValue = roleIter.next(); + EcompRole epRole = mapper.convertValue(nextValue, EcompRole.class); + roles.add(convertToRole(epRole)); + } + domainUser.setRoles(roles); + // After successful edit, call the admin auth extension if (adminAuthExtensionServiceImpl != null) { try { @@ -482,5 +546,4 @@ public class OnBoardingApiServiceImpl implements IPortalRestAPIService, IPortalR return getAppCredentials(); } - -} +} \ No newline at end of file -- cgit 1.2.3-korg