package <%=packageName%>.security;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;

/**
 * Utility class for Spring Security.
 */
public final class SecurityUtils {

    private SecurityUtils() {
    }

    /**
     * Get the login of the current user.
     *
     * @return the login of the current user
     */
    public static String getCurrentUserLogin() {
        SecurityContext securityContext = SecurityContextHolder.getContext();
        Authentication authentication = securityContext.getAuthentication();
        String userName = null;
        if (authentication != null) {
            if (authentication.getPrincipal() instanceof UserDetails) {
                UserDetails springSecurityUser = (UserDetails) authentication.getPrincipal();
                userName = springSecurityUser.getUsername();
            } else if (authentication.getPrincipal() instanceof String) {
                userName = (String) authentication.getPrincipal();
            }
        }
        return userName;
    }

    /**
     * Check if a user is authenticated.
     *
     * @return true if the user is authenticated, false otherwise
     */
    public static boolean isAuthenticated() {
        SecurityContext securityContext = SecurityContextHolder.getContext();
        Collection<? extends GrantedAuthority> authorities = securityContext.getAuthentication().getAuthorities();
        if (authorities != null) {
            for (GrantedAuthority authority : authorities) {
                if (authority.getAuthority().equals(AuthoritiesConstants.ANONYMOUS)) {
                    return false;
                }
            }
        }
        return true;
    }

    /**
     * If the current user has a specific authority (security role).
     *
     * <p>The name of this method comes from the isUserInRole() method in the Servlet API</p>
     *
     * @param authority the authority to check
     * @return true if the current user has the authority, false otherwise
     */
    public static boolean isCurrentUserInRole(String authority) {
        SecurityContext securityContext = SecurityContextHolder.getContext();
        Authentication authentication = securityContext.getAuthentication();
        if (authentication != null) {
            if (authentication.getPrincipal() instanceof UserDetails) {
                UserDetails springSecurityUser = (UserDetails) authentication.getPrincipal();
                return springSecurityUser.getAuthorities().contains(new SimpleGrantedAuthority(authority));
            }
        }
        return false;
    }
}

@@ -1628,32 +1628,151 @@
 r.getContext();%0A
+        Authentication authentication = securityContext.getAuthentication();%0A        if (authentication != null) %7B%0A    
         Collecti
@@ -1816,36 +1816,17 @@
 ities = 
-securityContext.getA
+a
 uthentic
@@ -1830,18 +1830,16 @@
 tication
-()
 .getAuth
@@ -1849,32 +1849,36 @@
 ties();%0A        
+    
 if (authorities 
@@ -1900,16 +1900,20 @@
         
+   
+ 
 for (Gra
@@ -1949,24 +1949,28 @@
 horities) %7B%0A
+    
             
@@ -2056,32 +2056,36 @@
                 
+ 
+   
 return false;%0A  
@@ -2078,24 +2078,28 @@
 turn false;%0A
+    
             
@@ -2120,38 +2120,82 @@
     
-%7D%0A        %7D%0A        return tru
+    %7D%0A            %7D%0A            return true;%0A        %7D%0A        return fals
 e;%0A 

SecurityUtils.isAuthenticated() shound return false if no auth