Sonar errors EPUserUtils class fix
Issue-ID: PORTAL-667
Change-Id: I7a8a39ac52ee2da5d2c2ab0016a8bbea0acb4d21
Signed-off-by: Dominik Mizyn <d.mizyn@samsung.com>
.getAttribute(SystemProperties.getProperty(SystemProperties.ROLE_FUNCTIONS_ATTRIBUTE_NAME));
if (roleFunctions == null) {
.getAttribute(SystemProperties.getProperty(SystemProperties.ROLE_FUNCTIONS_ATTRIBUTE_NAME));
if (roleFunctions == null) {
- HashMap roles = EPUserUtils.getRoles(request);
+ HashMap roles = (HashMap) EPUserUtils.getRoles(request);
roleFunctions = new HashSet();
Iterator i = roles.keySet().iterator();
roleFunctions = new HashSet();
Iterator i = roles.keySet().iterator();
* ============LICENSE_START==========================================
* ONAP Portal
* ===================================================================
* ============LICENSE_START==========================================
* ONAP Portal
* ===================================================================
- * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
* ===================================================================
*
* Unless otherwise specified, all software contained herein is licensed
* ===================================================================
*
* Unless otherwise specified, all software contained herein is licensed
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
-import java.util.Iterator;
import java.util.Set;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.Set;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;
import org.onap.portalapp.portal.domain.EPRole;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;
import org.onap.portalapp.portal.domain.EPRole;
import org.springframework.beans.factory.annotation.Autowired;
public class EPUserUtils {
import org.springframework.beans.factory.annotation.Autowired;
public class EPUserUtils {
-
- private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(EPUserUtils.class);
-
- private final static Long ACCOUNT_ADMIN_ROLE_ID = 999L;
-
public static final String ALL_ROLE_FUNCTIONS = "allRoleFunctions";
public static final String ALL_ROLE_FUNCTIONS = "allRoleFunctions";
-
- // These decode values are based on HexDecoder
private static final String decodeValueOfForwardSlash = "2f";
private static final String decodeValueOfHyphen = "2d";
private static final String decodeValueOfAsterisk = "2a";
private static final String decodeValueOfForwardSlash = "2f";
private static final String decodeValueOfHyphen = "2d";
private static final String decodeValueOfAsterisk = "2a";
+ private static final Long ACCOUNT_ADMIN_ROLE_ID = 999L;
+ private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(EPUserUtils.class);
private static DataAccessService dataAccessService;
/**
private static DataAccessService dataAccessService;
/**
* Menu data
* @param businessDirectMenuData
* Menu data
* Menu data
* @param businessDirectMenuData
* Menu data
- * @param loginMethod_ignored
- * How the user authenticated; ignored
* @param ePRoleFunctionService
* role function service
* @throws DecoderException
*/
@SuppressWarnings("rawtypes")
public static void setUserSession(HttpServletRequest request, EPUser user, Set applicationMenuData,
* @param ePRoleFunctionService
* role function service
* @throws DecoderException
*/
@SuppressWarnings("rawtypes")
public static void setUserSession(HttpServletRequest request, EPUser user, Set applicationMenuData,
- Set businessDirectMenuData, String loginMethod_ignored, EPRoleFunctionService ePRoleFunctionService) throws RoleFunctionException {
+ Set businessDirectMenuData, EPRoleFunctionService ePRoleFunctionService) throws RoleFunctionException {
HttpSession session = request.getSession(true);
// clear the current user session to avoid any conflicts
HttpSession session = request.getSession(true);
// clear the current user session to avoid any conflicts
session.setAttribute(SystemProperties.getProperty(SystemProperties.USER_NAME), user.getFullName());
ServletContext context = session.getServletContext();
session.setAttribute(SystemProperties.getProperty(SystemProperties.USER_NAME), user.getFullName());
ServletContext context = session.getServletContext();
- int licenseVerificationFlag = 3;
- licenseVerificationFlag = (Integer) context.getAttribute("licenseVerification");
+ context.getAttribute("licenseVerification");
} catch (Exception e) {
logger.error(EELFLoggerDelegate.errorLogger, "setUserSession failed to get licenseVerification attribute",
e);
} catch (Exception e) {
logger.error(EELFLoggerDelegate.errorLogger, "setUserSession failed to get licenseVerification attribute",
e);
private static void setAllRoleFunctions(List<RoleFunction> allRoleFunctions, HttpSession session) throws RoleFunctionException {
if (allRoleFunctions == null)
return;
private static void setAllRoleFunctions(List<RoleFunction> allRoleFunctions, HttpSession session) throws RoleFunctionException {
if (allRoleFunctions == null)
return;
- Set<String> roleFnSet = new HashSet<String>();
+ Set<String> roleFnSet = new HashSet<>();
for (RoleFunction roleFn : allRoleFunctions){
roleFnSet.add(decodeFunctionCode(roleFn.getCode()));
}
for (RoleFunction roleFn : allRoleFunctions){
roleFnSet.add(decodeFunctionCode(roleFn.getCode()));
}
* @return Map of role ID to role object
*/
@SuppressWarnings("rawtypes")
* @return Map of role ID to role object
*/
@SuppressWarnings("rawtypes")
- public static HashMap getRoles(HttpServletRequest request) {
- HashMap roles = null;
+ public static Map getRoles(HttpServletRequest request) {
+ HashMap roles;
HttpSession session = AppUtils.getSession(request);
roles = (HashMap) session.getAttribute(SystemProperties.getProperty(SystemProperties.ROLES_ATTRIBUTE_NAME));
HttpSession session = AppUtils.getSession(request);
roles = (HashMap) session.getAttribute(SystemProperties.getProperty(SystemProperties.ROLES_ATTRIBUTE_NAME));
@SuppressWarnings({ "rawtypes", "unchecked" })
private static HashMap getAllUserRoles(EPUser user) {
HashMap roles = new HashMap();
@SuppressWarnings({ "rawtypes", "unchecked" })
private static HashMap getAllUserRoles(EPUser user) {
HashMap roles = new HashMap();
- Iterator i = user.getEPRoles().iterator();
-
- while (i.hasNext()) {
- EPRole role = (EPRole) i.next();
+ for (EPRole role : user.getEPRoles()) {
if (role.getActive()) {
roles.put(role.getId(), role);
if (role.getActive()) {
roles.put(role.getId(), role);
// Additionally; the account admin role is overloaded between onap
// portal and partners; lets also include that
// Additionally; the account admin role is overloaded between onap
// portal and partners; lets also include that
- Iterator<EPUserApp> appRolesIterator = user.getEPUserApps().iterator();
- while (appRolesIterator.hasNext()) {
- EPRole role = (EPRole) appRolesIterator.next().getRole();
+ for (EPUserApp epUserApp : user.getEPUserApps()) {
+ EPRole role = epUserApp.getRole();
if (role.getActive() && role.getId().equals(ACCOUNT_ADMIN_ROLE_ID)) {
roles.put(role.getId(), role);
if (role.getActive() && role.getId().equals(ACCOUNT_ADMIN_ROLE_ID)) {
roles.put(role.getId(), role);
private static void addChildRoles(EPRole role, HashMap roles) {
Set childRoles = role.getChildRoles();
private static void addChildRoles(EPRole role, HashMap roles) {
Set childRoles = role.getChildRoles();
- if (childRoles != null && childRoles.size() > 0) {
- Iterator j = childRoles.iterator();
- while (j.hasNext()) {
- EPRole childRole = (EPRole) j.next();
+ if (childRoles != null && !childRoles.isEmpty()) {
+ for (Object o : childRoles) {
+ EPRole childRole = (EPRole) o;
if (childRole.getActive()) {
roles.put(childRole.getId(), childRole);
if (childRole.getActive()) {
roles.put(childRole.getId(), childRole);
- public void setDataAccessService(DataAccessService dataAccessService) {
+ public static void setDataAccessService(DataAccessService dataAccessService) {
EPUserUtils.dataAccessService = dataAccessService;
}
EPUserUtils.dataAccessService = dataAccessService;
}
* HttpServletREquest
* @return Long ID of current user
*/
* HttpServletREquest
* @return Long ID of current user
*/
- public static Long getUserIdAsLong(HttpServletRequest request) {
+ static Long getUserIdAsLong(HttpServletRequest request) {
Long userId = new Long(SystemProperties.getProperty(SystemProperties.APPLICATION_USER_ID));
Long userId = new Long(SystemProperties.getProperty(SystemProperties.APPLICATION_USER_ID));
- if (request != null) {
- if (getUserSession(request) != null) {
+ if (request != null && getUserSession(request) != null) {
userId = getUserSession(request).getId();
userId = getUserSession(request).getId();
String requestId = "";
try {
while (headerNames.hasMoreElements()) {
String requestId = "";
try {
while (headerNames.hasMoreElements()) {
- String headerName = (String) headerNames.nextElement();
+ String headerName = headerNames.nextElement();
logger.debug(EELFLoggerDelegate.debugLogger,
"One header is " + headerName + " : " + request.getHeader(headerName));
if (headerName.equalsIgnoreCase(SystemProperties.ECOMP_REQUEST_ID)) {
logger.debug(EELFLoggerDelegate.debugLogger,
"One header is " + headerName + " : " + request.getHeader(headerName));
if (headerName.equalsIgnoreCase(SystemProperties.ECOMP_REQUEST_ID)) {
* HttpServletRequest
* @return Full URL
*/
* HttpServletRequest
* @return Full URL
*/
- public static String getFullURL(HttpServletRequest request) {
+ static String getFullURL(HttpServletRequest request) {
if (request != null) {
StringBuffer requestURL = request.getRequestURL();
String queryString = request.getQueryString();
if (request != null) {
StringBuffer requestURL = request.getRequestURL();
String queryString = request.getQueryString();
public static Boolean matchRoleFunctions(String portalApiPath, Set<? extends String> roleFunctions) {
String[] path = portalApiPath.split("/");
public static Boolean matchRoleFunctions(String portalApiPath, Set<? extends String> roleFunctions) {
String[] path = portalApiPath.split("/");
- List<String> roleFunList = new ArrayList<>();
+ List<String> roleFunList;
if (path.length > 1) {
roleFunList = roleFunctions.stream().filter(item -> item.startsWith(path[0])).collect(Collectors.toList());
if (roleFunList.size() >= 1) {
if (path.length > 1) {
roleFunList = roleFunctions.stream().filter(item -> item.startsWith(path[0])).collect(Collectors.toList());
if (roleFunList.size() >= 1) {
boolean b = true;
if (roleFunctionArray.length == path.length) {
for (int i = 0; i < roleFunctionArray.length; i++) {
boolean b = true;
if (roleFunctionArray.length == path.length) {
for (int i = 0; i < roleFunctionArray.length; i++) {
if (!roleFunctionArray[i].equals("*")) {
Pattern p = Pattern.compile(Pattern.quote(path[i]), Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(roleFunctionArray[i]);
b = m.matches();
if (!roleFunctionArray[i].equals("*")) {
Pattern p = Pattern.compile(Pattern.quote(path[i]), Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(roleFunctionArray[i]);
b = m.matches();
PowerMockito.when(AppUtils.getSession(mockedRequest)).thenReturn(session);
PowerMockito.when(SystemProperties.getProperty(Matchers.anyString())).thenReturn("12");
Mockito.when(session.getAttribute(Matchers.anyString())).thenReturn(roles);
PowerMockito.when(AppUtils.getSession(mockedRequest)).thenReturn(session);
PowerMockito.when(SystemProperties.getProperty(Matchers.anyString())).thenReturn("12");
Mockito.when(session.getAttribute(Matchers.anyString())).thenReturn(roles);
- roles=EPUserUtils.getRoles(mockedRequest);
+ roles= (HashMap) EPUserUtils.getRoles(mockedRequest);
assertEquals(roles,expected);
assertEquals(roles,expected);
Mockito.when(session.getAttribute("attr_name")).thenReturn(user);
Mockito.when(user.getEPRoles()).thenReturn(role);
Mockito.when(session.getAttribute("12")).thenReturn(null);
Mockito.when(session.getAttribute("attr_name")).thenReturn(user);
Mockito.when(user.getEPRoles()).thenReturn(role);
Mockito.when(session.getAttribute("12")).thenReturn(null);
- roles=EPUserUtils.getRoles(mockedRequest);
+ roles= (HashMap) EPUserUtils.getRoles(mockedRequest);
assertEquals(roles,expected);
}
assertEquals(roles,expected);
}
Mockito.when(epRole.getChildRoles()).thenReturn(childRoles);
Mockito.when(user.getEPUserApps()).thenReturn(epUserApps);
Mockito.when(session.getAttribute("12")).thenReturn(null);
Mockito.when(epRole.getChildRoles()).thenReturn(childRoles);
Mockito.when(user.getEPUserApps()).thenReturn(epUserApps);
Mockito.when(session.getAttribute("12")).thenReturn(null);
- roles= EPUserUtils.getRoles(mockedRequest);
+ roles= (HashMap) EPUserUtils.getRoles(mockedRequest);
Mockito.when(epRoleFunctionService.getRoleFunctions()).thenReturn(roleFunctions);
Mockito.when(MenuBuilder.filterMenu(applicationMenuData, mockedRequest)).thenReturn(applicationMenuData);
PowerMockito.when(SystemProperties.getProperty(SystemProperties.USER_ATTRIBUTE_NAME)).thenReturn("12");
Mockito.when(epRoleFunctionService.getRoleFunctions()).thenReturn(roleFunctions);
Mockito.when(MenuBuilder.filterMenu(applicationMenuData, mockedRequest)).thenReturn(applicationMenuData);
PowerMockito.when(SystemProperties.getProperty(SystemProperties.USER_ATTRIBUTE_NAME)).thenReturn("12");
- EPUserUtils.setUserSession(mockedRequest, user, applicationMenuData, businessDirectMenuData, "login_method", epRoleFunctionService);
+ EPUserUtils.setUserSession(mockedRequest, user, applicationMenuData, businessDirectMenuData, epRoleFunctionService);
Mockito.when(epRoleFunctionService.getRoleFunctions()).thenReturn(null);
Mockito.when(MenuBuilder.filterMenu(applicationMenuData, mockedRequest)).thenReturn(applicationMenuData);
PowerMockito.when(SystemProperties.getProperty(SystemProperties.USER_ATTRIBUTE_NAME)).thenReturn("12");
Mockito.when(epRoleFunctionService.getRoleFunctions()).thenReturn(null);
Mockito.when(MenuBuilder.filterMenu(applicationMenuData, mockedRequest)).thenReturn(applicationMenuData);
PowerMockito.when(SystemProperties.getProperty(SystemProperties.USER_ATTRIBUTE_NAME)).thenReturn("12");
- EPUserUtils.setUserSession(mockedRequest, user, applicationMenuData, businessDirectMenuData, "login_method", epRoleFunctionService);
+ EPUserUtils.setUserSession(mockedRequest, user, applicationMenuData, businessDirectMenuData, epRoleFunctionService);
assertNotNull(session);
}
assertNotNull(session);
}
Mockito.when(epRoleFunctionService.getRoleFunctions()).thenReturn(null);
Mockito.when(MenuBuilder.filterMenu(applicationMenuData, mockedRequest)).thenReturn(applicationMenuData);
PowerMockito.when(SystemProperties.getProperty(SystemProperties.USER_ATTRIBUTE_NAME)).thenReturn("12");
Mockito.when(epRoleFunctionService.getRoleFunctions()).thenReturn(null);
Mockito.when(MenuBuilder.filterMenu(applicationMenuData, mockedRequest)).thenReturn(applicationMenuData);
PowerMockito.when(SystemProperties.getProperty(SystemProperties.USER_ATTRIBUTE_NAME)).thenReturn("12");
- EPUserUtils.setUserSession(mockedRequest, user, applicationMenuData, businessDirectMenuData, "login_method", epRoleFunctionService);
+ EPUserUtils.setUserSession(mockedRequest, user, applicationMenuData, businessDirectMenuData, epRoleFunctionService);
user.setLastName(userInfo.getFamilyName());
//store the currently logged in user's information in the session
user.setLastName(userInfo.getFamilyName());
//store the currently logged in user's information in the session
- EPUserUtils.setUserSession(request, user, new HashSet(), new HashSet(), SystemProperties.getProperty(SystemProperties.AUTHENTICATION_MECHANISM),null);
+ EPUserUtils.setUserSession(request, user, new HashSet(), new HashSet(),null);
logger.info(EELFLoggerDelegate.errorLogger, request.getContextPath());
SessionCookieUtil.preSetUp(request, response);
logger.info(EELFLoggerDelegate.errorLogger, request.getContextPath());
SessionCookieUtil.preSetUp(request, response);
// in case authentication has passed but user is not in the ONAP data base, return a Guest User to the home page.
if (commandBean.getUser() != null) {
// store the currently logged in user's information in the session
// in case authentication has passed but user is not in the ONAP data base, return a Guest User to the home page.
if (commandBean.getUser() != null) {
// store the currently logged in user's information in the session
- EPUserUtils.setUserSession(request, commandBean.getUser(), commandBean.getMenu(), commandBean.getBusinessDirectMenu(), "", ePRoleFunctionService);
+ EPUserUtils.setUserSession(request, commandBean.getUser(), commandBean.getMenu(), commandBean.getBusinessDirectMenu(), ePRoleFunctionService);
logger.info(EELFLoggerDelegate.debugLogger, commandBean.getUser().getOrgUserId() + " exists in the the system.");
}
logger.info(EELFLoggerDelegate.debugLogger, commandBean.getUser().getOrgUserId() + " exists in the the system.");
}
} else {
// store the currently logged in user's information in the session
EPUserUtils.setUserSession(request, commandBean.getUser(), commandBean.getMenu(),
} else {
// store the currently logged in user's information in the session
EPUserUtils.setUserSession(request, commandBean.getUser(), commandBean.getMenu(),
- commandBean.getBusinessDirectMenu(),
- SystemProperties.getProperty(SystemProperties.AUTHENTICATION_MECHANISM), ePRoleFunctionService);
+ commandBean.getBusinessDirectMenu(), ePRoleFunctionService);
try {
logger.info(EELFLoggerDelegate.debugLogger, "loginValidate: store user info into share context begins");
try {
logger.info(EELFLoggerDelegate.debugLogger, "loginValidate: store user info into share context begins");
orgUserId);
EPUserUtils.setUserSession(request, commandBean.getUser(), commandBean.getMenu(),
orgUserId);
EPUserUtils.setUserSession(request, commandBean.getUser(), commandBean.getMenu(),
- commandBean.getBusinessDirectMenu(),
- SystemProperties.getProperty(SystemProperties.AUTHENTICATION_MECHANISM), ePRoleFunctionService);
+ commandBean.getBusinessDirectMenu(), ePRoleFunctionService);
logger.info(EELFLoggerDelegate.debugLogger,
"processSingleSignOn: now set up user session for {} finished", orgUserId);
logger.info(EELFLoggerDelegate.debugLogger,
"processSingleSignOn: now set up user session for {} finished", orgUserId);