Add roles to user
[portal.git] / ecomp-portal-BE-common / src / main / java / org / onap / portalapp / portal / service / UserRolesCommonServiceImpl.java
index 1811f8d..b0dd4a2 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START==========================================
  * ONAP Portal
  * ===================================================================
- * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
  * ===================================================================
  *
  * Unless otherwise specified, all software contained herein is licensed
@@ -482,6 +482,7 @@ public class UserRolesCommonServiceImpl  {
                                                        oldAppRole.setName(role.getName());
                                                        localSession.update(oldAppRole);
                                                }
+                                               oldAppRole.setActive(true);
                                                newRolesMap.remove(oldAppRole.getAppRoleId());
                                        } else {
                                                obsoleteRoles.add(oldAppRole);
@@ -817,20 +818,77 @@ public class UserRolesCommonServiceImpl  {
        private EPUser addRemoteUser(List<RoleInAppForUser> roleInAppForUserList, String userId, EPApp app, ObjectMapper mapper, SearchService searchService, ApplicationsRestClientService applicationsRestClientService) throws Exception{
                EPUser addRemoteUser = null;
                if (remoteUserShouldBeCreated(roleInAppForUserList)) {
-                       
                        createNewUserOnRemoteApp(userId, app, applicationsRestClientService, searchService, mapper, isAppUpgradeVersion(app));
-                       // If we succeed, we know that the new user was
-                       // persisted on remote app.
-                       addRemoteUser = getUserFromApp(userId, app, applicationsRestClientService);
-                       if (addRemoteUser == null) {
-                               logger.error(EELFLoggerDelegate.errorLogger,
-                                               "Failed to persist new user: " + userId + " in remote app. appId = " + app.getId());
-                               // return null;
-                       }
                }
                return addRemoteUser;
        }
        
+       
+       private EPUser pushRemoteUser(List<RoleInAppForUser> roleInAppForUserList, String userId, EPApp app,
+                       ObjectMapper mapper, SearchService searchService,
+                       ApplicationsRestClientService applicationsRestClientService) throws Exception {
+               EPUser addRemoteUser = null;
+               if (remoteUserShouldBeCreated(roleInAppForUserList)) {
+                       pushUserOnRemoteApp(userId, app, applicationsRestClientService, searchService, mapper,
+                                       isAppUpgradeVersion(app), roleInAppForUserList);
+               }
+               return addRemoteUser;
+       }
+
+       protected void pushUserOnRemoteApp(String userId, EPApp app,
+                       ApplicationsRestClientService applicationsRestClientService, SearchService searchService,
+                       ObjectMapper mapper, boolean postOpenSource, List<RoleInAppForUser> roleInAppForUserList) throws Exception {
+
+               EPUser client = searchService.searchUserByUserId(userId);
+
+               mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
+
+               if (client == null) {
+                       String msg = "cannot create user " + userId + ", because he/she cannot be found in phonebook.";
+                       logger.error(EELFLoggerDelegate.errorLogger, msg);
+                       throw new Exception(msg);
+               }
+
+               client.setLoginId(userId);
+               client.setActive(true);
+               roleInAppForUserList.removeIf(role -> role.isApplied.equals(false));
+               Set<EcompRole> userRolesInRemoteApp = constructUsersRemoteAppRoles(roleInAppForUserList);
+               SortedSet<Role> roles = new TreeSet<>();
+               List<EPRole> getAppRoles = getAppRoles(app.getId());
+               for (EcompRole epRole : userRolesInRemoteApp) {
+                       Role role = new Role();
+                       EPRole appRole = getAppRoles.stream()
+                                         .filter(applicationRole -> epRole.getId().equals(applicationRole.getId()))
+                                         .findAny()
+                                         .orElse(null);
+                       if(appRole != null)
+                       role.setId(appRole.getAppRoleId());
+                       role.setName(epRole.getName());
+                       roles.add(role);
+               }
+               client.setRoles(roles);
+               String userInString = null;
+               userInString = mapper.writerFor(EPUser.class).writeValueAsString(client);
+               logger.debug(EELFLoggerDelegate.debugLogger,
+                               "about to post a client to remote application, users json = " + userInString);
+               applicationsRestClientService.post(EPUser.class, app.getId(), userInString, String.format("/user/%s", userId));
+
+       }
+       
+       
+       public List<EPRole> getAppRoles(Long appId) throws Exception {
+               List<EPRole> applicationRoles = null;
+               final Map<String, Long> appParams = new HashMap<>();
+               try {
+                               appParams.put("appId", appId);
+                               applicationRoles = dataAccessService.executeNamedQuery("getPartnerAppRolesList", appParams, null);
+               } catch (Exception e) {
+                       logger.error(EELFLoggerDelegate.errorLogger, "getAppRoles: failed", e);
+                       throw e;
+               }
+               return applicationRoles;
+       }
+       
        /**
         * It checks whether the remote user exists or not
         * if exits returns user object else null
@@ -890,18 +948,13 @@ public class UserRolesCommonServiceImpl  {
 
                                // if centralized app
                                if (app.getCentralAuth()) {
-                                       // We should add If user does not exist in remote application
                                        if (!app.getId().equals(PortalConstants.PORTAL_APP_ID)) {
-                                               EPUser remoteAppUser = null;
-                                               remoteAppUser = checkIfRemoteUserExits(userId, app, applicationsRestClientService);
-
-                                               if (remoteAppUser == null) {
-                                                       addRemoteUser(roleInAppForUserList, userId, app, mapper, searchService,
+                                                       pushRemoteUser(roleInAppForUserList, userId, app, mapper, searchService,
                                                                        applicationsRestClientService);
-                                               }
                                        }
                                        
-                                       Set<EcompRole> userRolesInLocalApp = postUsersRolesToLocalApp(roleInAppForUserList, mapper,
+                                       
+                                       Set<EcompRole>  userRolesInLocalApp = postUsersRolesToLocalApp(roleInAppForUserList, mapper,
                                                        applicationsRestClientService, appId, userId);
                                        RolesInAppForUser rolesInAppForUser = constructRolesInAppForUserUpdate(userId, appId,
                                                        userRolesInLocalApp);