1

次のコードを見てください

アプリケーション.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時間、何の運もありませんでした。主な問題は、上記のコードがどのように機能するかを完全に理解していないことです (私のコードではないため)。

4

1 に答える 1

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))


指定されたユーザー名が認証されているかどうかを判断するために、 別のアクション (アクション コンポジション) をラップするアクションです。承認されていない場合は、「onUnauthorized」関数が呼び出され、「You are not login」と表示されます。これは実際には機能しません。これに似た「onUnauthorized」を書く必要があります...

 private def onUnauthorized(request: RequestHeader) = 
  Redirect(routes.Home.url).flashing("You    are not logged in")
于 2012-11-06T20:54:39.333 に答える