2 * ================================================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property
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 * ================================================================================
20 package org.openecomp.portalapp.util;
22 import java.io.Serializable;
23 import java.util.Enumeration;
24 import java.util.HashMap;
25 import java.util.HashSet;
26 import java.util.Iterator;
27 import java.util.List;
30 import java.util.UUID;
32 import javax.servlet.http.HttpServletRequest;
33 import javax.servlet.http.HttpSession;
35 import org.openecomp.portalapp.portal.domain.EPRole;
36 import org.openecomp.portalapp.portal.domain.EPUser;
37 import org.openecomp.portalapp.portal.utils.EcompPortalUtils;
38 import org.openecomp.portalsdk.core.FusionObject;
39 import org.openecomp.portalsdk.core.domain.RoleFunction;
40 import org.openecomp.portalsdk.core.domain.UrlsAccessible;
41 import org.openecomp.portalsdk.core.exception.SessionExpiredException;
42 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
43 import org.openecomp.portalsdk.core.menu.MenuBuilder;
44 import org.openecomp.portalsdk.core.service.DataAccessService;
45 import org.openecomp.portalsdk.core.util.SystemProperties;
46 import org.openecomp.portalsdk.core.web.support.AppUtils;
47 import org.springframework.beans.factory.annotation.Autowired;
49 public class EPUserUtils implements Serializable, FusionObject {
50 private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(EPUserUtils.class);
52 public static final String KEY_USER_ROLES_CACHE = "userRoles";
54 public static final String WJ_HEADER_USER_NAME = "iv-user";
55 public static final String WJ_HEADER_USER_GROUP = "iv-groups";
57 private static DataAccessService dataAccessService;
59 private static final long serialVersionUID = 1L;
61 public static EPUser getUserSession(HttpServletRequest request) {
62 HttpSession session = AppUtils.getSession(request);
64 if (session == null) {
65 throw new SessionExpiredException();
68 return (EPUser) session.getAttribute(SystemProperties.getProperty(SystemProperties.USER_ATTRIBUTE_NAME));
71 @SuppressWarnings("rawtypes")
72 public static void setUserSession(HttpServletRequest request, EPUser user, Set applicationMenuData,
73 Set businessDirectMenuData, String loginMethod) {
74 HttpSession session = request.getSession(true);
76 EPUserUtils.clearUserSession(request); // let's clear the current user
77 // session to avoid any
78 // conflicts during the set
80 session.setAttribute(SystemProperties.getProperty(SystemProperties.USER_ATTRIBUTE_NAME), user);
82 getRoleFunctions(request);
84 // truncate the role (and therefore the role function) data to save
85 // memory in the session
86 user.setEPRoles(null);
87 session.setAttribute(SystemProperties.getProperty(SystemProperties.USER_NAME), user.getFullName());
88 session.setAttribute(SystemProperties.getProperty(SystemProperties.APP_DISPLAY_NAME), "My Portal");
89 session.setAttribute(SystemProperties.getProperty(SystemProperties.APPLICATION_MENU_ATTRIBUTE_NAME),
90 MenuBuilder.filterMenu(applicationMenuData, request));
91 session.setAttribute(SystemProperties.getProperty(SystemProperties.BUSINESS_DIRECT_MENU_ATTRIBUTE_NAME),
92 MenuBuilder.filterMenu(businessDirectMenuData, request));
95 public static void clearUserSession(HttpServletRequest request) {
96 HttpSession session = AppUtils.getSession(request);
98 if (session == null) {
99 throw new SessionExpiredException();
102 // removes all stored attributes from the current user's session
103 session.removeAttribute(SystemProperties.getProperty(SystemProperties.USER_ATTRIBUTE_NAME));
104 session.removeAttribute(SystemProperties.getProperty(SystemProperties.APPLICATION_MENU_ATTRIBUTE_NAME));
105 session.removeAttribute(SystemProperties.getProperty(SystemProperties.BUSINESS_DIRECT_MENU_ATTRIBUTE_NAME));
106 session.removeAttribute(SystemProperties.getProperty(SystemProperties.ROLES_ATTRIBUTE_NAME));
107 session.removeAttribute(SystemProperties.getProperty(SystemProperties.ROLE_FUNCTIONS_ATTRIBUTE_NAME));
110 @SuppressWarnings({ "rawtypes", "unchecked" })
111 public static Set getRoleFunctions(HttpServletRequest request) {
112 HashSet roleFunctions = null;
114 HttpSession session = request.getSession();
115 roleFunctions = (HashSet) session
116 .getAttribute(SystemProperties.getProperty(SystemProperties.ROLE_FUNCTIONS_ATTRIBUTE_NAME));
118 if (roleFunctions == null) {
119 HashMap roles = getRoles(request);
120 roleFunctions = new HashSet();
122 Iterator i = roles.keySet().iterator();
124 while (i.hasNext()) {
125 Long roleKey = (Long) i.next();
126 EPRole role = (EPRole) roles.get(roleKey);
128 Iterator j = role.getRoleFunctions().iterator();
130 while (j.hasNext()) {
131 RoleFunction function = (RoleFunction) j.next();
132 roleFunctions.add(function.getCode());
136 session.setAttribute(SystemProperties.getProperty(SystemProperties.ROLE_FUNCTIONS_ATTRIBUTE_NAME),
140 return roleFunctions;
143 @SuppressWarnings("rawtypes")
144 public static HashMap getRoles(HttpServletRequest request) {
145 HashMap roles = null;
147 // HttpSession session = request.getSession();
148 HttpSession session = AppUtils.getSession(request);
149 roles = (HashMap) session.getAttribute(SystemProperties.getProperty(SystemProperties.ROLES_ATTRIBUTE_NAME));
151 // if roles are not already cached, let's grab them from the user
154 EPUser user = getUserSession(request);
156 // get all user roles (including the tree of child roles)
157 roles = getAllUserRoles(user);
159 session.setAttribute(SystemProperties.getProperty(SystemProperties.ROLES_ATTRIBUTE_NAME),
160 getAllUserRoles(user));
166 @SuppressWarnings({ "rawtypes", "unchecked" })
167 public static HashMap getAllUserRoles(EPUser user) {
168 HashMap roles = new HashMap();
169 Iterator i = user.getEPRoles().iterator();
171 while (i.hasNext()) {
172 EPRole role = (EPRole) i.next();
174 if (role.getActive()) {
175 roles.put(role.getId(), role);
177 // let's take a recursive trip down the tree to add all child
179 addChildRoles(role, roles);
186 @SuppressWarnings({ "rawtypes", "unchecked" })
187 private static void addChildRoles(EPRole role, HashMap roles) {
188 Set childRoles = role.getChildRoles();
190 if (childRoles != null && childRoles.size() > 0) {
191 Iterator j = childRoles.iterator();
192 while (j.hasNext()) {
193 EPRole childRole = (EPRole) j.next();
195 if (childRole.getActive()) {
196 roles.put(childRole.getId(), childRole);
198 addChildRoles(childRole, roles);
205 @SuppressWarnings({ "unchecked", "rawtypes", "unused" })
206 public static boolean isUrlAccessible(HttpServletRequest request, String currentUrl) {
207 boolean isAccessible = false;
209 Map params = new HashMap();
210 params.put("current_url", currentUrl);
212 List list = getDataAccessService().executeNamedQuery("restrictedUrls", params, null);
214 // loop through the list of restricted URL's
215 if (list != null && list.size() > 0) {
216 for (int i = 0; i < list.size(); i++) {
218 * Object[] restrictedUrl = (Object[])list.get(i);
220 * String url = (String)restrictedUrl[0]; String functionCd =
221 * (String)restrictedUrl[1];
223 UrlsAccessible urlFunctions = (UrlsAccessible) list.get(i);
225 String url = (String) urlFunctions.getUrl();
226 String functionCd = (String) urlFunctions.getFunctionCd();
228 if (EPUserUtils.isAccessible(request, functionCd)) {
238 public static boolean hasRole(HttpServletRequest request, String roleKey) {
239 return getRoles(request).keySet().contains(new Long(roleKey));
242 public static boolean hasRole(EPUser user, String roleKey) {
243 return getAllUserRoles(user).keySet().contains(new Long(roleKey));
246 public static boolean isAccessible(HttpServletRequest request, String functionKey) {
247 return getRoleFunctions(request).contains(functionKey);
250 public static DataAccessService getDataAccessService() {
251 return dataAccessService;
255 public void setDataAccessService(DataAccessService dataAccessService) {
256 EPUserUtils.dataAccessService = dataAccessService;
259 public static int getUserId(HttpServletRequest request) {
260 return getUserIdAsLong(request).intValue();
263 public static Long getUserIdAsLong(HttpServletRequest request) {
264 Long userId = new Long(SystemProperties.getProperty(SystemProperties.APPLICATION_USER_ID));
266 if (request != null) {
267 if (getUserSession(request) != null) {
268 userId = getUserSession(request).getId();
275 public static String getRequestId(HttpServletRequest request) {
276 Enumeration<String> headerNames = request.getHeaderNames();
278 String requestId = "";
280 while (headerNames.hasMoreElements()) {
281 String headerName = (String) headerNames.nextElement();
282 logger.debug(EELFLoggerDelegate.debugLogger, "One header is " + headerName + " : " + request.getHeader(headerName));
283 if (headerName.equalsIgnoreCase(SystemProperties.ECOMP_REQUEST_ID)) {
284 requestId = request.getHeader(headerName);
288 } catch (Exception e) {
289 logger.error(EELFLoggerDelegate.errorLogger, "HEADER!!!! Exception : " + EcompPortalUtils.getStackTrace(e));
292 return (requestId.isEmpty() ? UUID.randomUUID().toString() : requestId);
295 public static String getFullURL(HttpServletRequest request) {
296 if (request != null) {
297 StringBuffer requestURL = request.getRequestURL();
298 String queryString = request.getQueryString();
300 if (queryString == null) {
301 return requestURL.toString();
303 return requestURL.append('?').append(queryString).toString();