1

在 play2 scala zentasks 示例应用程序中,代码片段如下所示

def IsAuthenticated(f: => String => Request[AnyContent] => Result) = 
    Security.Authenticated(username, onUnauthorized) { user =>
    Action(request => f(user)(request))
  }

我需要做的是在模型中调用这个函数

def AddOnline(email: String, contact: Contact) = {
DB.withConnection { implicit connection =>
  SQL(
    """
      update contact
      set online_status = {'online'} //I want to write the value contact.status=online
      where email = {email}
    """
  ).on(
    'email -> contact.email,
    'online_status -> contact.online_status
  ).executeUpdate()
}

}

AddOnline但我在这里的挑战是每次通过上述代码对某个用户进行身份验证时调用此函数。有人可以建议我该怎么做吗?我是这方面的新手,我兜兜转转,没有任何进展

4

1 回答 1

2

您可以像这样调用该addOnline方法IsAuthenticated

 def IsAuthenticated(f: => String => Request[AnyContent] => Result) = 
   Security.Authenticated(username, onUnauthorized) { user =>
   // Do what you need to do here before the action is called
   Contact.addOnline(user)
   Action(request => f(user)(request))
 }

请注意,这user不是用户对象,而只是经过身份验证的用户的电子邮件。

此外,由于您只添加了在线状态,user因此您可以将 AddOnline 方法简化为类似的方法。

def addOnline(email: String) = {
  DB.withConnection { implicit connection =>
    SQL(
      """
        update contact
        set online_status = 'online'
        where email = {email}
      """
    ).on(
      'email -> email,
  ).executeUpdate()
}
于 2012-11-06T07:01:25.753 回答