请查看以下代码
Application.scala
def Online = Action { implicit request =>
loginForm.bindFromRequest.fold(
formWithErrors => BadRequest(html.login(formWithErrors)),
user => Contact.AddOnline("email" -> user._1)
)
接着是
trait Secured {
/**
* Retrieve the connected user email.
*/
private def username(request: RequestHeader) =
request.session.get("email")
/**
* Redirect to login if the user in not authorized.
*/
private def onUnauthorized(request: RequestHeader) =
Home.flashing("failure"->"You are not logged in");
// --
/**
* Action for authenticated users.
*/
def IsAuthenticated(f: => String => Request[AnyContent] => Result)=
Security.Authenticated(username, onUnauthorized) { user =>
Action(request => f(user)(request))
}
-
我的问题是我试图调用一段名为setOnline(user.email)
的代码。此代码仅在经过身份验证后才将某个用户的状态设置为在线状态。在上面给出的代码中,我想调用我的setOnline(user.email)
函数,但我不确定应该在哪里或如何调用。过去4个小时我一直在努力,没有任何运气。主要问题是我不明白上面的代码是如何完全工作的(因为它不是我的代码)。
答案 0 :(得分:1)
此代码......
def Online = Action { implicit request =>
loginForm.bindFromRequest.fold(
formWithErrors => BadRequest(html.login(formWithErrors)),
user => Contact.AddOnline("email" -> user._1)
)
是一个将请求绑定到名为loginForm的Form对象的操作。它将检查表单是否有错误,如果有,那么它将显示带有这些错误的表单。如果没有,那么它将调用Contact.AddOnline。
这就是......
def IsAuthenticated(f: => String => Request[AnyContent] => Result) =
Security.Authenticated(username, onUnauthorized) { user =>Action(request => f(user)(request))
是一个Action,它围绕另一个操作(action composition)包装,以确定给定的用户名是否经过身份验证。如果未经授权,则会调用“onUnauthorized”功能,该功能将闪烁“您尚未登录”。这实际上是行不通的。你应该把你的“onUnauthorized”写成类似的......
private def onUnauthorized(request: RequestHeader) =
Redirect(routes.Home.url).flashing("You are not logged in")