你能为Actions添加参数吗?

时间:2014-09-16 20:22:27

标签: scala playframework

说我有一个动作,我希望它可以选择强制使用https。

如何将参数添加到自定义操作?

import play.api.mvc._

def onlyHttps[A](action: Action[A]) = Action.async(action.parser) { request =>
  request.headers.get("X-Forwarded-Proto").collect {
    case "https" => action(request)
  } getOrElse {
    Future.successful(Forbidden("Only HTTPS requests allowed"))
  }
}

所以在我的控制器中:

def index = onlyHttps(false) {
  // ..
}

另一个方面是我想检查当前登录的用户是否具有一定级别的权限,因此我想将权限类型作为参数传递给我的自定义操作。

1 个答案:

答案 0 :(得分:2)

只需添加another parameter list

def onlyHttps[A](limitToHttps: Boolean)(action: Action[A]) = /* 
    Your implementation here.  You can access `limitToHttps` here. */