inject the current user into the javascript context
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.ModelAndView;
|
||||||
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
|
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.
|
* 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.
|
||||||
*
|
*
|
||||||
|
@ -39,6 +41,8 @@ import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
|
||||||
*/
|
*/
|
||||||
public class UserInfoInterceptor extends HandlerInterceptorAdapter {
|
public class UserInfoInterceptor extends HandlerInterceptorAdapter {
|
||||||
|
|
||||||
|
private Gson gson = new Gson();
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private UserInfoService userInfoService;
|
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
|
// if they're logging into this server from a remote OIDC server, pass through their user info
|
||||||
OIDCAuthenticationToken oidc = (OIDCAuthenticationToken) p;
|
OIDCAuthenticationToken oidc = (OIDCAuthenticationToken) p;
|
||||||
modelAndView.addObject("userInfo", oidc.getUserInfo());
|
modelAndView.addObject("userInfo", oidc.getUserInfo());
|
||||||
|
// TODO: this should use the same serializer as UserInfoView (#488)
|
||||||
|
modelAndView.addObject("userInfoJson", gson.toJson(oidc.getUserInfo()));
|
||||||
} else {
|
} else {
|
||||||
if (p != null && p.getName() != null) { // don't bother checking if we don't have a principal
|
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 we have one, inject it so views can use it
|
||||||
if (user != null) {
|
if (user != null) {
|
||||||
modelAndView.addObject("userInfo", user);
|
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"%>
|
<%@attribute name="title" required="false"%>
|
||||||
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
|
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
|
||||||
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
|
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
|
||||||
|
<%@ tag import="com.google.gson.Gson" %>
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
<head>
|
<head>
|
||||||
|
@ -107,6 +108,11 @@
|
||||||
// safely set the title of the application
|
// safely set the title of the application
|
||||||
function setPageTitle(title) {
|
function setPageTitle(title) {
|
||||||
document.title = "${config.topbarTitle} - " + title;
|
document.title = "${config.topbarTitle} - " + title;
|
||||||
|
}
|
||||||
|
|
||||||
|
// get the info of the current user, if available (null otherwise)
|
||||||
|
function getUserInfo() {
|
||||||
|
return ${userInfoJson};
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
</head>
|
</head>
|
||||||
|
|
Loading…
Reference in New Issue