【问题标题】:get user information from views with securesocial in play framework使用securesocial in play框架从视图中获取用户信息
【发布时间】:2014-09-24 15:57:27
【问题描述】:

我在views 文件夹中有一个main.html。我想将当前登录的用户信息获取到视图,但我不想通过参数传递它。有办法吗?

main.html

 @(title: String)(content: Html)
 <html>
    <head></head>
    <body>Welcome {User}!</body>
 </html>

【问题讨论】:

    标签: playframework securesocial


    【解决方案1】:

    您可以使用从请求中提取用户的方法创建对象。

    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>
    

    【讨论】:

      【解决方案2】:

      不,视图中需要的任何内容都需要从控制器传入。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多