本文介绍了如何扩展Play2 scala zentasks身份验证以自动获取用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Play2的zentasks示例中,我们有方法

In the zentasks example for Play2 we have the method

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

我想做的是添加另一种方法,如果我想直接从数据库中获取用户,可以使用该方法.

What I would like to do is add another method that I could use if I wanted to get the user directly from the database.

在所有方法中都必须添加包装器有点无聊

It gets a little boring having to add a wrapper in all methods

def method() = isAuthenticated { username => implicit request =>
  UserDAO.findOneByEmail(username).map { user =>
    Ok(html.user.view(user))
  }.getOrElse(Forbidden)
}

我是函数式编程的新手,所有这些=>都使我头昏脑胀:)

I'm new to functional programming and all these => is making my head spin :)

有什么建议吗?

推荐答案

您可以定义另一个方法,例如IsAuthenticatedUser,它将采用类型为User => Request[AnyContent] => Result的参数:

You can define another method, for example IsAuthenticatedUser, which would take a parameter of type User => Request[AnyContent] => Result:

def IsAuthenticatedUser(f: User => Request[AnyContent] => Result) = IsAuthenticated { email => request =>
  UserDAO.findOneByEmail(email).map { user =>
    f(user)(request)
  }.getOrElse(Forbidden)
}

然后可以按如下方式使用它:

You can then use it like as follows:

def method = IsAuthenticatedUser { user => request =>
  Ok(html.user.view(user))
}

这篇关于如何扩展Play2 scala zentasks身份验证以自动获取用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 13:57