3
votes

My current custom action is this

class UserRequest[A](val user: Option[models.UserProfile],
                     request: Request[A]) extends WrappedRequest[A](request)

object UserAction extends ActionBuilder[UserRequest] with ActionTransformer[Request, UserRequest] {

  def transform[A](request: Request[A]) = Future.successful {
    val user = ... get user from session
      new UserRequest(user, request)
  }
}

Additionally, I want to pass a parameter to UserAction (e.g. role to validate). So in controller I can use it in this way:

def admin = UserAction("admin") { Ok("granted") }
2

2 Answers

1
votes

I don't know why you want to do such thing, but you can probably achieve it this way :

class UserRequest[A](val user: Option[models.UserProfile],
                 request: Request[A]) extends WrappedRequest[A](request)

object UserActionInner extends ActionBuilder[UserRequest] with ActionTransformer[Request, UserRequest] {

  def transform[A](request: Request[A]) = Future.successful {
    val user = ... get user from session
    new UserRequest(user, request)
  }

}

object UserAction {

  def apply[ A ]( str: String )( block: Request[ A ] => Future[ Result ] ) {
    // do something with your str
    // Now let UserActionInner do the job
    UserActionInner( block  )
  }

}

// Now you can use it like this.
def admin = UserAction("admin") { Ok("granted") }
0
votes

Using a case class with a role parameter, instead of an object, should give you the desired effect:

case class UserAction(role: String)  extends ActionBuilder[UserRequest] with ActionTransformer[Request, UserRequest] {
  override def transform[A](request: Request[A]) = ...
}