1

我正在使用带有securesocial的play 2.2.1,并且我的许多操作都使用SecuredActionsecuresocial提供的身份验证。

现在我想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 应该采用 aplay.api.mvc.Request[A]而不是 a securesocial.core.SecuredRequest[A],但我无法让它工作。

4

1 回答 1

4

假设您想用 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)
}
于 2013-11-22T20:05:13.920 回答