在Play 2.5 ActionRefiner中使用implicits

时间:2016-07-18 14:10:08

标签: scala playframework-2.0

我正在尝试创建自己的ActionRefiner以适应身份验证,但由于某种原因编译器不允许我在refine[A]函数中使用隐式变量...我有以下代码:

trait Auth {
  object AuthenticatedAction extends ActionBuilder[AuthRequest] with ActionRefiner[Request, AuthRequest] {

    def refine[A](request: Request[A])(implicit userCollection: UserCollection, ex: ExecutionContext): Future[Either[Result, AuthRequest[A]]] = {
      request.session.get("username") match {
        case Some(username) => userCollection.findByEmail(username).map { userOpt =>
          userOpt.map(new AuthRequest(_, request)).toRight(Results.Redirect(routes.LoginController.login()))
        }
        case None => Future.successful(Left(Results.Redirect(routes.LoginController.login())))
      }
    }
  }
}

class AuthRequest[A](val user: User, request: Request[A]) extends WrappedRequest[A](request)

Scala编译器告诉我方法refine[A](request: R[A]): Future[Either[Result, P[A]]]未定义。当我删除它注册的隐式变量时,但这让我没有UserCollection ...

那么,我如何正确使用ActionRefiner?

1 个答案:

答案 0 :(得分:0)

感谢this topic我找到了一种方法让它发挥作用。而不是使用隐式,我可以在特征中定义UserCollection并从我的控制器传递它,如下所示:

trait Auth {
  def userCollection: UserCollection

  object AuthenticatedAction extends ActionBuilder[AuthRequest] with ActionRefiner[Request, AuthRequest] {
    def refine[A](request: Request[A]): Future[Either[Result, AuthRequest[A]]] = {
      ...
    }
  }
}

class HomeController @Inject()(val userCollection: UserCollection)(implicit executionContext: ExecutionContext) extends Controller with Auth {
  def index = AuthenticatedAction { implicit request =>
    Ok(s"Hello ${request.user.name}")
  }
}

我已经习惯了使用implicits,我完全忘记了这一点。