UserContextUtil.java

  1. /*
  2.  * Copyright 2010-2025 James Pether Sörling
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *   http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  *
  16.  *  $Id$
  17.  *  $HeadURL$
  18. */
  19. package com.hack23.cia.web.impl.ui.application.util;

  20. import java.util.Collection;

  21. import javax.servlet.http.HttpServletRequest;

  22. import org.springframework.security.core.Authentication;
  23. import org.springframework.security.core.GrantedAuthority;
  24. import org.springframework.security.core.context.SecurityContext;
  25. import org.springframework.security.core.context.SecurityContextHolder;
  26. import org.springframework.web.context.request.RequestContextHolder;
  27. import org.springframework.web.context.request.ServletRequestAttributes;

  28. import com.vaadin.server.Page;

  29. /**
  30.  * The Class UserContextUtil.
  31.  */
  32. public final class UserContextUtil {

  33.     /**
  34.      * Default constructor for UserContextUtil.
  35.      */
  36.     public UserContextUtil() {
  37.         // Default constructor
  38.     }

  39.     /**
  40.      * Allow role in security context.
  41.      *
  42.      * @param role
  43.      *            the role
  44.      * @return true, if successful
  45.      */
  46.     public static boolean allowRoleInSecurityContext(final String role) {
  47.         boolean result = false;
  48.         final SecurityContext context = SecurityContextHolder.getContext();
  49.         if (context != null && context.getAuthentication() != null) {
  50.             final Collection<? extends GrantedAuthority> authorities = context.getAuthentication().getAuthorities();

  51.             for (final GrantedAuthority grantedAuthority : authorities) {
  52.                 if (role.equalsIgnoreCase(grantedAuthority.getAuthority())) {
  53.                     result = true;
  54.                 }
  55.             }
  56.         }
  57.         return result;
  58.     }

  59.     /**
  60.      * Gets the request url.
  61.      *
  62.      * @param current
  63.      *            the current
  64.      * @return the request url
  65.      */
  66.     public static String getRequestUrl(final Page current) {
  67.         if (current != null) {
  68.             return current.getLocation().toString();

  69.         } else {
  70.             final HttpServletRequest httpRequest=((ServletRequestAttributes)RequestContextHolder.currentRequestAttributes()).getRequest();
  71.             return httpRequest.getRequestURL().toString();
  72.         }
  73.     }

  74.     /**
  75.      * Gets the user id from security context.
  76.      *
  77.      * @return the user id from security context
  78.      */
  79.     public static String getUserIdFromSecurityContext() {
  80.         final SecurityContext context = SecurityContextHolder.getContext();
  81.         if (context != null) {
  82.             final Authentication authentication = context.getAuthentication();
  83.             if (authentication != null) {
  84.                 return authentication.getPrincipal().toString();
  85.             }
  86.         }
  87.         return null;
  88.     }

  89. }