EPUserUtils class fix 26/91526/1
authorDominik Mizyn <d.mizyn@samsung.com>
Tue, 16 Jul 2019 11:00:07 +0000 (13:00 +0200)
committerDominik Mizyn <d.mizyn@samsung.com>
Tue, 16 Jul 2019 11:17:57 +0000 (13:17 +0200)
Sonar errors EPUserUtils class fix

Issue-ID: PORTAL-667
Change-Id: I7a8a39ac52ee2da5d2c2ab0016a8bbea0acb4d21
Signed-off-by: Dominik Mizyn <d.mizyn@samsung.com>
ecomp-portal-BE-common/src/main/java/org/onap/portalapp/portal/service/EPRoleFunctionServiceImpl.java
ecomp-portal-BE-common/src/main/java/org/onap/portalapp/util/EPUserUtils.java
ecomp-portal-BE-common/src/test/java/org/onap/portalapp/util/EPUserUtilsTest.java
ecomp-portal-BE-os/src/main/java/org/onap/portalapp/authentication/OpenIdConnectLoginStrategy.java
ecomp-portal-BE-os/src/main/java/org/onap/portalapp/authentication/SimpleLoginStrategy.java
ecomp-portal-BE-os/src/main/java/org/onap/portalapp/controller/LoginController.java

index 4a3cf63..c088164 100644 (file)
@@ -86,7 +86,7 @@ public class EPRoleFunctionServiceImpl implements EPRoleFunctionService {
                                .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();
index 99a2911..80db8c8 100644 (file)
@@ -2,7 +2,7 @@
  * ============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
@@ -41,18 +41,16 @@ import java.util.ArrayList;
 import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.HashSet;
-import java.util.Iterator;
 import java.util.List;
+import java.util.Map;
 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 org.apache.commons.codec.DecoderException;
 import org.apache.commons.codec.binary.Hex;
 import org.onap.portalapp.portal.domain.EPRole;
@@ -70,18 +68,14 @@ import org.onap.portalsdk.core.web.support.AppUtils;
 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";
-       
-       // 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 Long ACCOUNT_ADMIN_ROLE_ID = 999L;
 
+       private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(EPUserUtils.class);
        private static DataAccessService dataAccessService;
 
        /**
@@ -111,15 +105,13 @@ public class EPUserUtils {
         *            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,
-                       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
@@ -136,9 +128,8 @@ public class EPUserUtils {
                session.setAttribute(SystemProperties.getProperty(SystemProperties.USER_NAME), user.getFullName());
 
                ServletContext context = session.getServletContext();
-               int licenseVerificationFlag = 3;
                try {
-                       licenseVerificationFlag = (Integer) context.getAttribute("licenseVerification");
+                       context.getAttribute("licenseVerification");
                } catch (Exception e) {
                        logger.error(EELFLoggerDelegate.errorLogger, "setUserSession failed to get licenseVerification attribute",
                                        e);
@@ -163,7 +154,7 @@ public class EPUserUtils {
        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()));
                }
@@ -221,8 +212,8 @@ public class EPUserUtils {
         * @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));
@@ -251,11 +242,8 @@ public class EPUserUtils {
        @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);
 
@@ -267,9 +255,8 @@ public class EPUserUtils {
 
                // 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);
@@ -295,10 +282,9 @@ public class EPUserUtils {
        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);
@@ -319,7 +305,7 @@ public class EPUserUtils {
        }
 
        @Autowired
-       public void setDataAccessService(DataAccessService dataAccessService) {
+       public static void setDataAccessService(DataAccessService dataAccessService) {
                EPUserUtils.dataAccessService = dataAccessService;
        }
 
@@ -341,12 +327,10 @@ public class EPUserUtils {
         *            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));
-               if (request != null) {
-                       if (getUserSession(request) != null) {
+               if (request != null && getUserSession(request) != null) {
                                userId = getUserSession(request).getId();
-                       }
                }
                return userId;
        }
@@ -364,7 +348,7 @@ public class EPUserUtils {
                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)) {
@@ -386,7 +370,7 @@ public class EPUserUtils {
         *            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();
@@ -402,7 +386,7 @@ public class EPUserUtils {
 
        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) {
@@ -411,17 +395,13 @@ public class EPUserUtils {
                                        boolean b = true;
                                        if (roleFunctionArray.length == path.length) {
                                                for (int i = 0; i < roleFunctionArray.length; i++) {
-                                                       if (b) {
                                                                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 (b)
-                                                               return b;
+                                               if (b) return true;
                                        }
                                }
                        }
index 7383330..80ca142 100644 (file)
@@ -256,7 +256,7 @@ public class EPUserUtilsTest {
                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);
 
 
@@ -279,7 +279,7 @@ public class EPUserUtilsTest {
                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);
 
        }
@@ -320,7 +320,7 @@ public class EPUserUtilsTest {
                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);
                assertNotNull(roles);
        }
        
@@ -358,7 +358,7 @@ public class EPUserUtilsTest {
                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);
         assertNotNull(session);
 
 
@@ -383,7 +383,7 @@ public class EPUserUtilsTest {
                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);
        }
        
@@ -408,7 +408,7 @@ public class EPUserUtilsTest {
                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);
 
 
index 456f001..f4b8445 100644 (file)
@@ -78,7 +78,7 @@ public class OpenIdConnectLoginStrategy extends org.onap.portalsdk.core.auth.Log
                        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);  
index a5f8790..79ae20f 100644 (file)
@@ -85,7 +85,7 @@ public class SimpleLoginStrategy extends org.onap.portalsdk.core.auth.LoginStrat
                         // 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.");
                        }
                        
index 56064b9..3b0281f 100644 (file)
@@ -149,8 +149,7 @@ public class LoginController extends EPUnRestrictedBaseController implements Log
                } 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");
@@ -265,8 +264,7 @@ public class LoginController extends EPUnRestrictedBaseController implements Log
                                                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);