package edu.uiowa.icts.spring;

import java.util.Collection;

import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;

public class Security {

	public static boolean hasRole( String role ) {
		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
		if ( authentication != null ) {
			Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
			for ( GrantedAuthority ga : authorities ) {
				if ( ga.getAuthority().equals( role ) ) {
					return true;
				}
			}
		}
		return false;
	}

	public static boolean isAuthenticated() {
		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
		return authentication != null && ! ( authentication instanceof AnonymousAuthenticationToken ) && authentication.isAuthenticated();
	}

}
@@ -55,16 +55,60 @@
 ction;%0A%0A
+import org.apache.commons.lang.StringUtils;%0A
 import o
@@ -180,16 +180,16 @@
 nToken;%0A
-
 import o
@@ -367,16 +367,100 @@
 tHolder;
+%0Aimport org.springframework.security.web.authentication.switchuser.SwitchUserFilter;
 %0A%0Apublic
@@ -794,16 +794,46 @@
 %09%09%09%09if (
+ StringUtils.equalsIgnoreCase(
  ga.getA
@@ -846,16 +846,9 @@
 ty()
-.equals(
+,
  rol
@@ -1041,16 +1041,16 @@
 tion();%0A
-
 %09%09return
@@ -1178,9 +1178,121 @@
 ();%0A%09%7D%0A%0A
+%09public static boolean isUserSwitched() %7B%0A%09%09return hasRole( SwitchUserFilter.ROLE_PREVIOUS_ADMINISTRATOR );%0A%09%7D%0A%0A
 %7D

add is user switched method