2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
22 package org.openecomp.portalapp.service;
24 import java.util.ArrayList;
25 import java.util.Iterator;
26 import java.util.List;
28 import java.util.SortedSet;
29 import java.util.TreeSet;
31 import javax.servlet.http.HttpServletRequest;
33 import org.openecomp.portalsdk.core.domain.Role;
34 import org.openecomp.portalsdk.core.domain.User;
35 import org.openecomp.portalsdk.core.domain.UserApp;
36 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
37 import org.openecomp.portalsdk.core.onboarding.client.AppContextManager;
38 import org.openecomp.portalsdk.core.onboarding.crossapi.IPortalRestAPIService;
39 import org.openecomp.portalsdk.core.onboarding.crossapi.PortalAPIException;
40 import org.openecomp.portalsdk.core.onboarding.crossapi.PortalTimeoutHandler;
41 import org.openecomp.portalsdk.core.restful.domain.EcompRole;
42 import org.openecomp.portalsdk.core.restful.domain.EcompUser;
43 import org.openecomp.portalsdk.core.service.RoleService;
44 import org.openecomp.portalsdk.core.service.UserProfileService;
45 import org.openecomp.portalsdk.core.service.WebServiceCallService;
46 import org.openecomp.portalsdk.core.util.JSONUtil;
47 import org.openecomp.portalsdk.core.util.SystemProperties;
48 import org.openecomp.portalsdk.core.web.support.UserUtils;
52 public class OnBoardingApiServiceImplPolicy implements IPortalRestAPIService {
53 RoleService roleService;
54 UserProfileService userProfileService;
55 AdminAuthExtension adminAuthExtension;
57 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(OnBoardingApiServiceImplPolicy.class);
59 public OnBoardingApiServiceImplPolicy(){
60 roleService = AppContextManager.getAppContext().getBean(RoleService.class);
61 userProfileService = AppContextManager.getAppContext().getBean(UserProfileService.class);
62 adminAuthExtension = AppContextManager.getAppContext().getBean(AdminAuthExtension.class);
65 private void setCurrentAttributes(User user, EcompUser userJson){
66 user.setEmail(userJson.getEmail());
67 user.setFirstName(userJson.getFirstName());
68 user.setHrid(userJson.getHrid());
69 user.setJobTitle(userJson.getJobTitle());
70 user.setLastName(userJson.getLastName());
71 user.setLoginId(userJson.getLoginId());
72 user.setOrgManagerUserId(userJson.getOrgManagerUserId());
73 user.setMiddleInitial(userJson.getMiddleInitial());
74 user.setOrgCode(userJson.getOrgCode());
75 user.setOrgId(userJson.getOrgId());
76 user.setPhone(userJson.getPhone());
77 user.setOrgUserId(userJson.getOrgUserId());
78 user.setActive(userJson.isActive());
83 public void pushUser(EcompUser userJson) throws PortalAPIException {
85 logger.debug(EELFLoggerDelegate.debugLogger, "pushUser was invoked" + userJson);
87 User user = new User();
90 logger.debug(EELFLoggerDelegate.debugLogger, "## REST API ## userJson: " + userJson);
94 //Set input attributes to the object about to be saved
95 setCurrentAttributes(user, userJson);
97 user.setRoles(new TreeSet());
98 user.setUserApps(new TreeSet());
99 user.setPseudoRoles(new TreeSet());
101 userProfileService.saveUser(user);
102 adminAuthExtension.saveUserExtension(user);
103 logger.debug(EELFLoggerDelegate.debugLogger, "push user success.");
104 response = "push user success.";
105 response = JSONUtil.convertResponseToJSON(response);
106 } catch (Exception e) {
108 response = "push user failed with error: " + e.getMessage();
109 logger.error(EELFLoggerDelegate.debugLogger, response);
110 logger.error(EELFLoggerDelegate.errorLogger, "Error happened during OnboardingApiService.pushUser operation: " + response);
111 logger.error(EELFLoggerDelegate.debugLogger, "Error happened during OnboardingApiService.pushUser operation: " + response);
112 logger.info(EELFLoggerDelegate.metricsLogger, "OnboardingApiService.pushUser operation has failed.");
113 throw new PortalAPIException(response, e);
115 MDC.remove(SystemProperties.MDC_TIMER);
120 public void editUser(String loginId, EcompUser userJson) throws PortalAPIException {
122 logger.debug(EELFLoggerDelegate.debugLogger, "OnboardingApi editUser was invoked" + userJson);
124 User editUser = new User();
125 String response = "";
126 logger.debug(EELFLoggerDelegate.debugLogger, "## REST API ## loginId: " + loginId);
127 logger.debug(EELFLoggerDelegate.debugLogger, "## REST API ## userJson: " + userJson);
131 setCurrentAttributes(editUser, userJson);
133 if (editUser.getOrgUserId() != null) {
134 editUser.setLoginId(editUser.getOrgUserId());
137 User domainUser = userProfileService.getUserByLoginId(loginId);
138 if (domainUser != null)
139 domainUser = JSONUtil.mapToDomainUser(domainUser, editUser);
141 domainUser = editUser;
142 userProfileService.saveUser(domainUser);
143 adminAuthExtension.saveUserExtension(domainUser);
145 logger.debug(EELFLoggerDelegate.debugLogger, "edit user success.");
146 response = "edit user success.";
147 response = JSONUtil.convertResponseToJSON(response);
148 } catch (Exception e) {
150 response = "edit user failed with error: " + e.getMessage();
151 logger.error(EELFLoggerDelegate.errorLogger, response);
152 logger.error(EELFLoggerDelegate.debugLogger, "Error happened during OnboardingApiService.editUser operation: " + response);
153 logger.error(EELFLoggerDelegate.errorLogger, "Error happened during OnboardingApiService.editUser operation: " + response);
154 throw new PortalAPIException(response, e);
156 MDC.remove(SystemProperties.MDC_TIMER);
163 public EcompUser getUser(String loginId) throws PortalAPIException {
165 logger.debug(EELFLoggerDelegate.debugLogger, "## REST API ## loginId: " + loginId);
167 User user = userProfileService.getUserByLoginId(loginId);
170 logger.info(EELFLoggerDelegate.debugLogger, "User + " + loginId + " doesn't exist");
174 return UserUtils.convertToEcompUser(user);
177 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage());
184 public List<EcompUser> getUsers() throws PortalAPIException {
187 String response = "";
191 List<User> users = userProfileService.findAllActive();
192 List<EcompUser> ecompUsers = new ArrayList<EcompUser>();
193 for(User user : users)
194 ecompUsers.add(UserUtils.convertToEcompUser(user));
199 catch (Exception pe){
201 response = "getUsers failed with error: " + pe.getMessage();
202 pe.printStackTrace();
203 logger.error(EELFLoggerDelegate.debugLogger, response);
204 logger.error(EELFLoggerDelegate.errorLogger, response);
205 throw new PortalAPIException(response, pe);
212 public List<EcompRole> getAvailableRoles() throws PortalAPIException{
214 String response = "";
217 List<Role> roles = roleService.getActiveRoles();
218 List<EcompRole> ecompRoles = new ArrayList<EcompRole>();
220 for(Role role : roles)
221 ecompRoles.add(UserUtils.convertToEcompRole(role));
226 catch (Exception pe){
227 response = "getUsers failed with error: " + pe.getMessage();
228 pe.printStackTrace();
229 logger.error(EELFLoggerDelegate.debugLogger, response);
230 logger.error(EELFLoggerDelegate.errorLogger, response);
231 throw new PortalAPIException(response, pe);
236 public void pushUserRole(String loginId, List<EcompRole> rolesJson) throws PortalAPIException {
239 String response = "";
241 logger.debug(EELFLoggerDelegate.debugLogger, "## REST API ## loginId: " + loginId);
242 logger.debug(EELFLoggerDelegate.debugLogger, "## REST API ## rolesJson: " + rolesJson);
243 User user = userProfileService.getUserByLoginId(loginId);
245 SortedSet<Role> roles = new TreeSet();
246 for (EcompRole role : rolesJson) {
247 roles.add(roleService.getRole(role.getId()));
249 // Replace existing roles with new ones
250 replaceExistingRoles(roles, user);
252 logger.debug(EELFLoggerDelegate.debugLogger, "push user role success.");
253 response = "push user role success.";
254 response = JSONUtil.convertResponseToJSON(response);
256 } catch (Exception e) {
257 response = "pushUserRole failed with error: " + e.getMessage();
259 logger.error(EELFLoggerDelegate.debugLogger, response);
260 logger.error(EELFLoggerDelegate.errorLogger, response);
261 throw new PortalAPIException(response, e);
263 MDC.remove(SystemProperties.MDC_TIMER);
269 public List<EcompRole> getUserRoles(String loginId) throws PortalAPIException {
271 logger.debug(EELFLoggerDelegate.debugLogger, "## REST API ## loginId: " + loginId);
273 List<EcompRole> ecompRoles = new ArrayList<EcompRole>();
277 User user = userProfileService.getUserByLoginId(loginId);
279 SortedSet<Role> currentRoles = null;
282 currentRoles = user.getRoles();
284 if(currentRoles != null)
285 for(Role role : currentRoles)
286 ecompRoles.add(UserUtils.convertToEcompRole(role));
291 String response = "getUserRoles failed with error: " + e.getMessage();
293 logger.error(EELFLoggerDelegate.debugLogger, response);
294 logger.debug(EELFLoggerDelegate.errorLogger, response);
295 throw new PortalAPIException(response, e);
299 private void replaceExistingRoles(SortedSet<Role> roles, User user) {
300 // 1. remove existing roles
301 Set<UserApp> userApps = user.getUserApps();
302 Iterator appsItr = (Iterator) userApps.iterator();
303 while (appsItr.hasNext()) {
304 UserApp tempUserApp = (UserApp)appsItr.next();
305 boolean roleFound = false;
306 for (Role role : roles) {
307 if (tempUserApp.getRole().getId().equals(role.getId())) {
315 user.setUserApps(userApps);
316 userProfileService.saveUser(user);
319 user.setRoles(roles);
320 userProfileService.saveUser(user);
321 adminAuthExtension.saveUserExtension(user);
326 public boolean isAppAuthenticated(HttpServletRequest request) throws PortalAPIException {
327 WebServiceCallService securityService = AppContextManager.getAppContext().getBean(WebServiceCallService.class);
329 String appUser = request.getHeader("username");
330 String password = request.getHeader("password");
331 boolean flag = securityService.verifyRESTCredential(null, appUser, password);
334 } catch (Exception e) {
335 logger.error(EELFLoggerDelegate.errorLogger, "Failed to authenticate" + e.getMessage());
336 throw new PortalAPIException("Failed to authenticate: " + e.getMessage());
341 public String getSessionTimeOuts() throws Exception{
342 return PortalTimeoutHandler.gatherSessionExtensions();
345 public void updateSessionTimeOuts(String sessionMap) throws Exception{
346 PortalTimeoutHandler.updateSessionExtensions(sessionMap);