【发布时间】:2013-11-15 10:01:16
【问题描述】:
我正在尝试创建一个自定义 play.api.mvc.Action,它可用于根据请求填充 CustomerAccount 并将 CustomerAccount 传递给控制器。
在documentation for Play 2.2.x 之后,我创建了Action 和ActionBuilder,但我似乎无法从操作中返回CustomerAccount。
我当前的代码是:
case class AccountWrappedRequest[A](account: CustomerAccount, request: Request[A]) extends WrappedRequest[A](request)
case class Account[A](action: Action[A]) extends Action[A] {
lazy val parser = action.parser
def apply(request: Request[A]): Future[SimpleResult] = {
AccountService.getBySubdomain(request.host).map { account =>
// Do something to return the account like return a new AccountWrappedRequest?
action(AccountWrappedRequest(account, request))
} getOrElse {
Future.successful(NotFound(views.html.account_not_found()))
}
}
}
object AccountAction extends ActionBuilder[AccountWrappedRequest] {
def invokeBlock[A](request: Request[A], block: (AccountWrappedRequest[A]) => Future[SimpleResult]) = {
// Or here to pass it to the next request?
block(request) // block(AccountWrappedRequest(account??, request))
}
override def composeAction[A](action: Action[A]) = Account(action)
}
注意:这不会编译,因为 block(request) 函数需要一种我无法填充的 AccountWrappedRequest 类型。使用直接Request
另外...
最终,我希望能够将此帐户操作与身份验证操作结合起来,以便可以将CustomerAccount 传递到身份验证操作中,并且可以根据该客户的帐户提供用户身份验证。然后我想将客户帐户和用户传递给控制器。
例如:
Account(Authenticated(Action))) { request => request.account; request.user ... } 或更好的是作为不需要自定义请求对象的单个对象。
【问题讨论】:
-
我正在努力解决同样的问题,到目前为止发现以下高度相关:mariussoutier.com/blog/2013/09/17/…
-
@AndrewE 谢谢你,我以前看过那个页面,它确实有一些有用的信息。我想我想出了一个解决方案,我在下面发布了答案。希望这会有所帮助。
标签: scala playframework action before-filter playframework-2.2