从安全社区的框架中获取视图的用户信息框架
问题描述:
我有一个main.html视图文件夹。我想获取当前登录的用户信息到视图,但我不想通过参数传递它。有没有办法做到这一点?需要从安全社区的框架中获取视图的用户信息框架
main.html中
@(title: String)(content: Html)
<html>
<head></head>
<body>Welcome {User}!</body>
</html>
答
您可以使用从请求中提取用户的方法创建对象。
object UserInfo {
def getCurrentUser(implicit request: RequestHeader): Option[Identity] = {
request match {
case r: SecuredRequest[_] => Some(r.user)
case r: RequestWithUser[_] => r.user
case _ => None
}
}
}
然后在视图中,您可以简单地静态使用此方法。
@(title: String)(content: Html)(implicit request: RequestHeader)
@import app.auth.UserInfo
<html>
<head></head>
<body>Welcome @UserInfo.getCurrentUser !</body>
</html>
答
不,无论你的观点需要在从控制器通过。