inject the current user into the javascript context

pull/485/merge
Justin Richer 11 years ago
parent 5c10eef8b7
commit be6179d1ac

@ -31,6 +31,8 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import com.google.gson.Gson;
/**
* Injects the UserInfo object for the current user into the current model's context, if both exist. Allows JSPs and the like to call "userInfo.name" and other fields.
*
@ -38,6 +40,8 @@ import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
*
*/
public class UserInfoInterceptor extends HandlerInterceptorAdapter {
private Gson gson = new Gson();
@Autowired
private UserInfoService userInfoService;
@ -53,6 +57,8 @@ public class UserInfoInterceptor extends HandlerInterceptorAdapter {
// if they're logging into this server from a remote OIDC server, pass through their user info
OIDCAuthenticationToken oidc = (OIDCAuthenticationToken) p;
modelAndView.addObject("userInfo", oidc.getUserInfo());
// TODO: this should use the same serializer as UserInfoView (#488)
modelAndView.addObject("userInfoJson", gson.toJson(oidc.getUserInfo()));
} else {
if (p != null && p.getName() != null) { // don't bother checking if we don't have a principal
@ -62,6 +68,8 @@ public class UserInfoInterceptor extends HandlerInterceptorAdapter {
// if we have one, inject it so views can use it
if (user != null) {
modelAndView.addObject("userInfo", user);
// TODO: this should use the same serializer as UserInfoView (#488)
modelAndView.addObject("userInfoJson", gson.toJson(user));
}
}
}

@ -1,6 +1,7 @@
<%@attribute name="title" required="false"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ tag import="com.google.gson.Gson" %>
<!DOCTYPE html>
<html lang="en">
<head>
@ -107,6 +108,11 @@
// safely set the title of the application
function setPageTitle(title) {
document.title = "${config.topbarTitle} - " + title;
}
// get the info of the current user, if available (null otherwise)
function getUserInfo() {
return ${userInfoJson};
}
</script>
</head>

Loading…
Cancel
Save