nexus site path corrected
[portal.git] / ecomp-portal-BE / src / main / java / org / openecomp / portalapp / util / EPUserUtils.java
1 /*-
2  * ================================================================================
3  * eCOMP Portal
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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  * ================================================================================
19  */
20 package org.openecomp.portalapp.util;
21
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;
28 import java.util.Map;
29 import java.util.Set;
30 import java.util.UUID;
31
32 import javax.servlet.http.HttpServletRequest;
33 import javax.servlet.http.HttpSession;
34
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;
48
49 public class EPUserUtils implements Serializable, FusionObject {
50         private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(EPUserUtils.class);
51
52         public static final String KEY_USER_ROLES_CACHE = "userRoles";
53
54         public static final String WJ_HEADER_USER_NAME = "iv-user";
55         public static final String WJ_HEADER_USER_GROUP = "iv-groups";
56
57         private static DataAccessService dataAccessService;
58
59         private static final long serialVersionUID = 1L;
60
61         public static EPUser getUserSession(HttpServletRequest request) {
62                 HttpSession session = AppUtils.getSession(request);
63
64                 if (session == null) {
65                         throw new SessionExpiredException();
66                 }
67
68                 return (EPUser) session.getAttribute(SystemProperties.getProperty(SystemProperties.USER_ATTRIBUTE_NAME));
69         }
70
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);
75
76                 EPUserUtils.clearUserSession(request); // let's clear the current user
77                                                                                                 // session to avoid any
78                                                                                                 // conflicts during the set
79
80                 session.setAttribute(SystemProperties.getProperty(SystemProperties.USER_ATTRIBUTE_NAME), user);
81
82                 getRoleFunctions(request);
83
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));
93         }
94
95         public static void clearUserSession(HttpServletRequest request) {
96                 HttpSession session = AppUtils.getSession(request);
97
98                 if (session == null) {
99                         throw new SessionExpiredException();
100                 }
101
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));
108         }
109
110         @SuppressWarnings({ "rawtypes", "unchecked" })
111         public static Set getRoleFunctions(HttpServletRequest request) {
112                 HashSet roleFunctions = null;
113
114                 HttpSession session = request.getSession();
115                 roleFunctions = (HashSet) session
116                                 .getAttribute(SystemProperties.getProperty(SystemProperties.ROLE_FUNCTIONS_ATTRIBUTE_NAME));
117
118                 if (roleFunctions == null) {
119                         HashMap roles = getRoles(request);
120                         roleFunctions = new HashSet();
121
122                         Iterator i = roles.keySet().iterator();
123
124                         while (i.hasNext()) {
125                                 Long roleKey = (Long) i.next();
126                                 EPRole role = (EPRole) roles.get(roleKey);
127
128                                 Iterator j = role.getRoleFunctions().iterator();
129
130                                 while (j.hasNext()) {
131                                         RoleFunction function = (RoleFunction) j.next();
132                                         roleFunctions.add(function.getCode());
133                                 }
134                         }
135
136                         session.setAttribute(SystemProperties.getProperty(SystemProperties.ROLE_FUNCTIONS_ATTRIBUTE_NAME),
137                                         roleFunctions);
138                 }
139
140                 return roleFunctions;
141         }
142
143         @SuppressWarnings("rawtypes")
144         public static HashMap getRoles(HttpServletRequest request) {
145                 HashMap roles = null;
146
147                 // HttpSession session = request.getSession();
148                 HttpSession session = AppUtils.getSession(request);
149                 roles = (HashMap) session.getAttribute(SystemProperties.getProperty(SystemProperties.ROLES_ATTRIBUTE_NAME));
150
151                 // if roles are not already cached, let's grab them from the user
152                 // session
153                 if (roles == null) {
154                         EPUser user = getUserSession(request);
155
156                         // get all user roles (including the tree of child roles)
157                         roles = getAllUserRoles(user);
158
159                         session.setAttribute(SystemProperties.getProperty(SystemProperties.ROLES_ATTRIBUTE_NAME),
160                                         getAllUserRoles(user));
161                 }
162
163                 return roles;
164         }
165
166         @SuppressWarnings({ "rawtypes", "unchecked" })
167         public static HashMap getAllUserRoles(EPUser user) {
168                 HashMap roles = new HashMap();
169                 Iterator i = user.getEPRoles().iterator();
170
171                 while (i.hasNext()) {
172                         EPRole role = (EPRole) i.next();
173
174                         if (role.getActive()) {
175                                 roles.put(role.getId(), role);
176
177                                 // let's take a recursive trip down the tree to add all child
178                                 // roles
179                                 addChildRoles(role, roles);
180                         }
181                 }
182
183                 return roles;
184         }
185
186         @SuppressWarnings({ "rawtypes", "unchecked" })
187         private static void addChildRoles(EPRole role, HashMap roles) {
188                 Set childRoles = role.getChildRoles();
189
190                 if (childRoles != null && childRoles.size() > 0) {
191                         Iterator j = childRoles.iterator();
192                         while (j.hasNext()) {
193                                 EPRole childRole = (EPRole) j.next();
194
195                                 if (childRole.getActive()) {
196                                         roles.put(childRole.getId(), childRole);
197
198                                         addChildRoles(childRole, roles);
199                                 }
200                         }
201                 }
202
203         }
204
205         @SuppressWarnings({ "unchecked", "rawtypes", "unused" })
206         public static boolean isUrlAccessible(HttpServletRequest request, String currentUrl) {
207                 boolean isAccessible = false;
208
209                 Map params = new HashMap();
210                 params.put("current_url", currentUrl);
211
212                 List list = getDataAccessService().executeNamedQuery("restrictedUrls", params, null);
213
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++) {
217                                 /*
218                                  * Object[] restrictedUrl = (Object[])list.get(i);
219                                  * 
220                                  * String url = (String)restrictedUrl[0]; String functionCd =
221                                  * (String)restrictedUrl[1];
222                                  */
223                                 UrlsAccessible urlFunctions = (UrlsAccessible) list.get(i);
224
225                                 String url = (String) urlFunctions.getUrl();
226                                 String functionCd = (String) urlFunctions.getFunctionCd();
227
228                                 if (EPUserUtils.isAccessible(request, functionCd)) {
229                                         isAccessible = true;
230                                 }
231                         }
232                         return isAccessible;
233                 }
234
235                 return true;
236         }
237
238         public static boolean hasRole(HttpServletRequest request, String roleKey) {
239                 return getRoles(request).keySet().contains(new Long(roleKey));
240         }
241
242         public static boolean hasRole(EPUser user, String roleKey) {
243                 return getAllUserRoles(user).keySet().contains(new Long(roleKey));
244         }
245
246         public static boolean isAccessible(HttpServletRequest request, String functionKey) {
247                 return getRoleFunctions(request).contains(functionKey);
248         }
249
250         public static DataAccessService getDataAccessService() {
251                 return dataAccessService;
252         }
253
254         @Autowired
255         public void setDataAccessService(DataAccessService dataAccessService) {
256                 EPUserUtils.dataAccessService = dataAccessService;
257         }
258
259         public static int getUserId(HttpServletRequest request) {
260                 return getUserIdAsLong(request).intValue();
261         }
262
263         public static Long getUserIdAsLong(HttpServletRequest request) {
264                 Long userId = new Long(SystemProperties.getProperty(SystemProperties.APPLICATION_USER_ID));
265
266                 if (request != null) {
267                         if (getUserSession(request) != null) {
268                                 userId = getUserSession(request).getId();
269                         }
270                 }
271
272                 return userId;
273         }
274         
275         public static String getRequestId(HttpServletRequest request) {
276                 Enumeration<String> headerNames = request.getHeaderNames();
277
278                 String requestId = "";
279                 try {
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);
285                                         break;
286                                 }
287                         }
288                 } catch (Exception e) {
289                         logger.error(EELFLoggerDelegate.errorLogger, "HEADER!!!! Exception : " + EcompPortalUtils.getStackTrace(e));
290                 }
291   
292                 return (requestId.isEmpty() ? UUID.randomUUID().toString() : requestId);
293         }
294         
295         public static String getFullURL(HttpServletRequest request) {
296                 if (request != null) {
297                         StringBuffer requestURL = request.getRequestURL();
298                         String queryString = request.getQueryString();
299
300                         if (queryString == null) {
301                                 return requestURL.toString();
302                         } else {
303                                 return requestURL.append('?').append(queryString).toString();
304                         }
305                 }
306                 return "";
307         }
308 }