/* * ============LICENSE_START========================================== * ONAP Portal SDK * =================================================================== * Copyright © 2017 AT&T Intellectual Property. All rights reserved. * =================================================================== * * Unless otherwise specified, all software contained herein is licensed * under the Apache License, Version 2.0 (the "License"); * you may not use this software 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. * * Unless otherwise specified, all documentation contained herein is licensed * under the Creative Commons License, Attribution 4.0 Intl. (the "License"); * you may not use this documentation except in compliance with the License. * You may obtain a copy of the License at * * https://creativecommons.org/licenses/by/4.0/ * * Unless required by applicable law or agreed to in writing, documentation * 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. * * ============LICENSE_END============================================ * * */ package org.onap.portalapp.controller.core; import java.io.IOException; import java.io.PrintWriter; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.json.JSONObject; import org.onap.portalsdk.core.controller.RestrictedBaseController; import org.onap.portalsdk.core.domain.Role; import org.onap.portalsdk.core.domain.User; import org.onap.portalsdk.core.domain.UserApp; import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate; import org.onap.portalsdk.core.service.RoleService; import org.onap.portalsdk.core.service.UserProfileService; import org.onap.portalsdk.core.service.UserService; import org.onap.portalsdk.core.util.SystemProperties; import org.onap.portalsdk.core.web.support.AppUtils; import org.onap.portalsdk.core.web.support.JsonMessage; import org.onap.portalsdk.core.web.support.UserUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; @Controller @RequestMapping("/") public class ProfileController extends RestrictedBaseController { private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(ProfileController.class); private static final String PROFILE_ID = "profile_id"; private static final String PROFILEID = "profileId"; private static final String STATE_LIST = "stateList"; private static final String COUNTRIES = "countries"; private static final String TIME_ZONES = "timeZones"; private static final String AVAILABLE_ROLES= "availableRoles"; private static final String PROFILE = "profile"; private static final String PROFILE_CONTAINER = "ProfileController.save"; private static final String UTF8 = "UTF-8"; private static final String APPLICATION_JSON = "application / json"; @Autowired private UserProfileService service; @Autowired private UserService userService; @Autowired private RoleService roleService; @RequestMapping(value = { "/get_self_profile" }, method = RequestMethod.GET) public void getSelfProfile(HttpServletRequest request, HttpServletResponse response) { Map model = new HashMap<>(); ObjectMapper mapper = new ObjectMapper(); User user = UserUtils.getUserSession(request); Long profileId = null; User profile = UserUtils.getUserSession(request); @SuppressWarnings("unchecked") Set userapps = user.getUserApps(); Set userapplications = UserUtils.getUserApps(userapps); profile.setUserApps(userapplications); try { model.put(STATE_LIST, mapper.writeValueAsString(getStates())); model.put(COUNTRIES, mapper.writeValueAsString(getCountries())); model.put(TIME_ZONES, mapper.writeValueAsString(getTimeZones())); model.put(AVAILABLE_ROLES, mapper.writeValueAsString(getAvailableRoles(user.getOrgUserId()))); model.put(PROFILE, mapper.writeValueAsString(profile)); model.put(PROFILEID, mapper.writeValueAsString(profileId)); JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model)); JSONObject j = new JSONObject(msg); response.getWriter().write(j.toString()); } catch (Exception e) { logger.error(EELFLoggerDelegate.errorLogger, "getSelfProfile: failed to write JSON", e); } } @RequestMapping(value = { "/get_profile" }, method = RequestMethod.GET) public void getUser(HttpServletRequest request, HttpServletResponse response) { Map model = new HashMap<>(); User user = UserUtils.getUserSession(request); ObjectMapper mapper = new ObjectMapper(); try { User profile; Long profileId; if (request.getRequestURI().indexOf("self_profile.htm") > -1) { profile = UserUtils.getUserSession(request); profileId = profile.getId(); } else { profileId = Long.parseLong(request.getParameter(PROFILE_ID)); profile = userService.getUser(String.valueOf(profileId)); } @SuppressWarnings("unchecked") Set userapps = user.getUserApps(); Set userapplications = UserUtils.getUserApps(userapps); profile.setUserApps(userapplications); model.put(STATE_LIST, mapper.writeValueAsString(getStates())); model.put(COUNTRIES, mapper.writeValueAsString(getCountries())); model.put(TIME_ZONES, mapper.writeValueAsString(getTimeZones())); model.put(AVAILABLE_ROLES, mapper.writeValueAsString(getAvailableRoles(user.getOrgUserId()))); model.put(PROFILE, mapper.writeValueAsString(profile)); model.put(PROFILEID, mapper.writeValueAsString(profileId)); JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model)); JSONObject j = new JSONObject(msg); response.getWriter().write(j.toString()); } catch (Exception e) { logger.error(EELFLoggerDelegate.errorLogger, "getUser: failed to write JSON", e); } } @RequestMapping(value = { "/profile/saveProfile" }, method = RequestMethod.POST) public ModelAndView saveProfile(HttpServletRequest request, HttpServletResponse response) { logger.info(EELFLoggerDelegate.debugLogger, PROFILE_CONTAINER); try { ObjectMapper mapper = new ObjectMapper(); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); JsonNode root = mapper.readTree(request.getReader()); User user = mapper.readValue(root.get(PROFILE).toString(), User.class); String selectedCountry = mapper.readValue(root.get("selectedCountry").toString(), String.class); String selectedState = mapper.readValue(root.get("selectedState").toString(), String.class); String selectedTimeZone = mapper.readValue(root.get("selectedTimeZone").toString(), String.class); Long profileId = Long.parseLong(request.getParameter(PROFILE_ID)); User domainUser = userService.getUser(String.valueOf(profileId)); user.setPseudoRoles(domainUser.getPseudoRoles()); user.setUserApps(domainUser.getUserApps()); if (!selectedCountry.isEmpty()) { user.setCountry(selectedCountry); } if (!selectedState.isEmpty()) { user.setState(selectedState); } if (!selectedTimeZone.isEmpty()) { user.setTimeZoneId(Long.parseLong(selectedTimeZone)); } service.saveUser(user); HttpSession session = request.getSession(true); session.setAttribute(SystemProperties.getProperty(SystemProperties.USER_ATTRIBUTE_NAME), user); logger.info(EELFLoggerDelegate.auditLogger, "Save profile for user {}", profileId); response.setCharacterEncoding(UTF8); response.setContentType(APPLICATION_JSON); PrintWriter out = response.getWriter(); out.write("" + profileId); return null; } catch (Exception e) { logger.error(EELFLoggerDelegate.errorLogger, "saveProfile failed", e); response.setCharacterEncoding(UTF8); try { PrintWriter out = response.getWriter(); out.write("An error occurred in the saveProfile ()"); } catch (IOException e1) { logger.error(EELFLoggerDelegate.errorLogger, "saveProfile: failed to write", e1); } return null; } } @RequestMapping(value = { "/profile/removeRole" }, method = RequestMethod.POST) public ModelAndView removeRole(HttpServletRequest request, HttpServletResponse response) throws IOException { logger.info(EELFLoggerDelegate.debugLogger, PROFILE_CONTAINER); try { ObjectMapper mapper = new ObjectMapper(); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); JsonNode root = mapper.readTree(request.getReader()); Role role = mapper.readValue(root.get("role").toString(), Role.class); String profileId = request.getParameter(PROFILE_ID); User domainUser = userService.getUser(profileId); domainUser.removeRole(role.getId()); service.saveUser(domainUser); /*If adding new roles on the current logged in user, we need to update the user value in session*/ if(UserUtils.getUserId(request)==Integer.valueOf(profileId)){ HttpSession session = request.getSession(true); session.setAttribute(SystemProperties.getProperty(SystemProperties.USER_ATTRIBUTE_NAME), domainUser); } logger.info(EELFLoggerDelegate.auditLogger, "Remove role " + role.getId() + " from user " + profileId); response.setCharacterEncoding(UTF8); response.setContentType(APPLICATION_JSON); request.setCharacterEncoding(UTF8); PrintWriter out = response.getWriter(); Map model = new HashMap<>(); model.put(PROFILE, mapper.writeValueAsString(domainUser)); JSONObject j = new JSONObject(mapper.writeValueAsString(domainUser)); out.write(j.toString()); return null; } catch (Exception e) { logger.error(EELFLoggerDelegate.errorLogger, "removeRole failed", e); response.setCharacterEncoding(UTF8); PrintWriter out = response.getWriter(); out.write("An error occurred in the removeRole ()"); return null; } } @RequestMapping(value = { "/profile/addNewRole" }, method = RequestMethod.POST) public ModelAndView addNewRole(HttpServletRequest request, HttpServletResponse response) throws IOException { logger.info(EELFLoggerDelegate.debugLogger, PROFILE_CONTAINER ); try { ObjectMapper mapper = new ObjectMapper(); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); JsonNode root = mapper.readTree(request.getReader()); Role role = mapper.readValue(root.get("role").toString(), Role.class); String profileId = request.getParameter(PROFILE_ID); User domainUser = userService.getUser(profileId); domainUser.addRole(role); service.saveUser(domainUser); /*If removing roles on the current logged in user, we need to update the user value in session*/ if(UserUtils.getUserId(request)==Integer.valueOf(profileId)){ HttpSession session = request.getSession(true); session.setAttribute(SystemProperties.getProperty(SystemProperties.USER_ATTRIBUTE_NAME), domainUser); } logger.info(EELFLoggerDelegate.auditLogger, "Add new role " + role.getName() + " to user " + profileId); response.setCharacterEncoding(UTF8); response.setContentType(APPLICATION_JSON); request.setCharacterEncoding(UTF8); PrintWriter out = response.getWriter(); Map model = new HashMap<>(); model.put(PROFILE, mapper.writeValueAsString(domainUser)); JSONObject j = new JSONObject(mapper.writeValueAsString(domainUser)); out.write(j.toString()); return null; } catch (Exception e) { logger.error(EELFLoggerDelegate.errorLogger, "addNewRole failed", e); response.setCharacterEncoding(UTF8); request.setCharacterEncoding(UTF8); PrintWriter out = response.getWriter(); out.write("An error occurred in the addNewRole ()"); return null; } } @SuppressWarnings("rawtypes") public List getStates() { return AppUtils.getLookupList("FN_LU_STATE", "STATE_CD", "STATE", null, "STATE_CD"); } @SuppressWarnings("rawtypes") public List getCountries() { return AppUtils.getLookupList("FN_LU_COUNTRY", "COUNTRY_CD", "COUNTRY", null, "COUNTRY"); } @SuppressWarnings("rawtypes") public List getTimeZones() { return AppUtils.getLookupList("FN_LU_TIMEZONE", "TIMEZONE_ID", "TIMEZONE_NAME", null, "TIMEZONE_NAME"); } @SuppressWarnings("rawtypes") public List getAvailableRoles(String requestedLoginId) throws IOException { return roleService.getAvailableRoles(requestedLoginId); } @Override public String getViewName() { return viewName; } @Override public void setViewName(String viewName) { this.viewName = viewName; } }