0

def edit(id: String) = SecuredAction(WithService("master")).async {隐式请求 =>

    val futureEmp = collection.flatMap(_.find(Json.obj("_id" -> Json.obj("$oid" -> id))).cursor[Employee]().collect[List]())

futureEmp.map { emps: List[Employee] =>
  implicit val message = messagesApi.preferred(request)

  Ok(html.editForm(id, employeeForm.fill(emps.head)))
}.recover {
  case t: TimeoutException =>
    Logger.error("Problem found in employee edit process")
    InternalServerError(t.getMessage)
}

}

4

1 回答 1

1

一旦你在 play 模块中连接了 Silhouette,并将你的用户模型注入到 Environment trait 中:

trait YourCustomEnv extends Env {
  type I = User // <- Whatever user model your app uses
  type A = JWTAuthenticator
}

...您将能够通过访问身份成员来访问安全端点上的身份:

def index() = silhouette.SecuredAction.async { implicit request =>
    // The model of the authenticated user available here
    val user: User = request.identity
    Future.successful(Ok)
}

假设您使用 Twirl 作为模板引擎,您可以将此对象作为参数添加到模板中,从而将其传递给视图:

@(user: models.User)

...并从您的控制器中调用模板:

def index() = silhouette.SecuredAction.async { implicit request =>
    val user: User = request.identity
    Future.successful(Ok(views.html.home(user))))
}
于 2018-02-15T21:53:00.723 回答