Scala 将ActionBuilder与securesocial SecuredAction一起使用
我正在将play 2.2.1与securesocial一起使用,我的许多操作都使用securesocial提供的Scala 将ActionBuilder与securesocial SecuredAction一起使用,scala,playframework,playframework-2.2,securesocial,Scala,Playframework,Playframework 2.2,Securesocial,我正在将play 2.2.1与securesocial一起使用,我的许多操作都使用securesocial提供的SecuredAction进行验证 现在,我想为特定类型的请求创建一个ActionBuilder,比如文档中的ItemAction 我试着做这样的事情 /** * ActionBuilder for use with actions that use Item objects. */ def ItemAction() = new ActionBuilder[Requ
SecuredAction
进行验证
现在,我想为特定类型的请求创建一个ActionBuilder
,比如文档中的ItemAction
我试着做这样的事情
/**
* ActionBuilder for use with actions that use Item objects.
*/
def ItemAction() = new ActionBuilder[RequestWithItem] {
def invokeBlock[A](request: SecuredRequest[A], block: (RequestWithItem[A]) => Future[SimpleResult]) = {
val futureItem = itemsCollection.
find(Json.obj("identityId" -> request.user.identityId)).one[Item]
futureItem.flatMap { maybeItem =>
maybeItem match {
case Some(item) => {
block(new RequestWithItem(item, request))
}
case _ => Future(BadRequest("could not get a item for this user."))
}
}
}
}
我得到了这个编译错误
object creation impossible, since method invokeBlock in trait ActionBuilder of type [A](request: play.api.mvc.Request[A], block: models.RequestWithitem[A] => scala.concurrent.Future[play.api.mvc.SimpleResult])scala.concurrent.Future[play.api.mvc.SimpleResult] is not defined
我知道invokeBlock应该采用
play.api.mvc.Request[a]
而不是securesocial.core.SecuredRequest[a]
,但我无法让它工作。假设您想用SecuredAction(通过securesocial)编写ItemAction,这将是一种方法:
case class Item(id: Long) // replace with your real world item
def SecuredItemAction(f: => Item => Request[AnyContent] => Result) =
SecuredAction { implicit request =>
val item = Some(new Item(7)) // replace with your real world item fetch
item.map { item =>
f(item)(request)
}.getOrElse(NotFound)
}
然后您可以按以下方式使用它:
def index = SecuredItemAction { item =>
implicit request =>
Ok(item.id.toString)
}
假设您希望使用SecuredAction(通过SecureSocial)编写ItemAction,这将是一种方法:
case class Item(id: Long) // replace with your real world item
def SecuredItemAction(f: => Item => Request[AnyContent] => Result) =
SecuredAction { implicit request =>
val item = Some(new Item(7)) // replace with your real world item fetch
item.map { item =>
f(item)(request)
}.getOrElse(NotFound)
}
然后您可以按以下方式使用它:
def index = SecuredItemAction { item =>
implicit request =>
Ok(item.id.toString)
}
是否可以将SecuredItemAction作为一个动作对象?我不太确定您想做什么。您可以更新您的问题,说明您希望如何使用您的操作吗?我正在尝试创建一些我可以在不同操作的不同正文分析器中重复使用的内容,并且也可以处理异步结果。@yzernik您是否想出了一些可以使用不同正文分析器的内容?是否可以将SecuredItemAction作为动作对象?我不太确定你想做什么。你能用你想如何使用你的动作来更新你的问题吗?我正在尝试创建一些我可以在不同的主体解析器中为不同的动作重复使用的东西,并且也可以处理异步结果。@yzernik你有没有想出一些可以使用不同主体解析器的东西?