diff --git a/core/src/main/java/hudson/Functions.java b/core/src/main/java/hudson/Functions.java index 5e118478c8..03c6d937f7 100644 --- a/core/src/main/java/hudson/Functions.java +++ b/core/src/main/java/hudson/Functions.java @@ -32,6 +32,7 @@ import org.kohsuke.stapler.Ancestor; import org.kohsuke.stapler.Stapler; import org.kohsuke.stapler.StaplerRequest; import org.kohsuke.stapler.StaplerResponse; +import org.acegisecurity.providers.anonymous.AnonymousAuthenticationToken; import javax.servlet.ServletException; import javax.servlet.http.Cookie; @@ -723,4 +724,11 @@ public class Functions { } private static final Pattern LINE_END = Pattern.compile("\r?\n"); + + /** + * Checks if the current user is anonymous. + */ + public static boolean isAnonymous() { + return Hudson.getAuthentication() instanceof AnonymousAuthenticationToken; + } } diff --git a/core/src/main/java/hudson/model/Hudson.java b/core/src/main/java/hudson/model/Hudson.java index fc286489fc..0ab6322c51 100644 --- a/core/src/main/java/hudson/model/Hudson.java +++ b/core/src/main/java/hudson/model/Hudson.java @@ -62,6 +62,7 @@ import hudson.widgets.Widget; import net.sf.json.JSONObject; import org.acegisecurity.context.SecurityContextHolder; import org.acegisecurity.ui.AbstractProcessingFilter; +import org.acegisecurity.Authentication; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; @@ -1798,8 +1799,7 @@ public final class Hudson extends View implements ItemGroup, Node if(!Hudson.adminCheck(req,rsp)) return; LOGGER.severe(String.format("Shutting down VM as requested by {0} from {1}", - SecurityContextHolder.getContext().getAuthentication(), - req.getRemoteAddr())); + getAuthentication(), req.getRemoteAddr())); rsp.setStatus(HttpServletResponse.SC_OK); rsp.setContentType("text/plain"); PrintWriter w = rsp.getWriter(); @@ -1809,6 +1809,14 @@ public final class Hudson extends View implements ItemGroup, Node System.exit(0); } + /** + * Gets the {@link Authentication} object that represents the user + * associated with the current request. + */ + public static Authentication getAuthentication() { + return SecurityContextHolder.getContext().getAuthentication(); + } + /** * Configure the logging level. */