Here my problem:
I need to check in a controller the user permission using deadbolt and then add something to the request (using an ActionBuilder). Normally using Play Action Builders would be (action1 andThen action2) but this doesn't work with DeadboltActions.
Here some code:
ActionBuilder
import javax.inject.Inject
import models.Item
import modules.item.services.ItemServiceClient
import play.api.mvc._
import scala.concurrent.{ExecutionContext, Future}
class ItemRequest[A](val items: Seq[Item], request: Request[A]) extends WrappedRequest[A](request)
class ItemAction @Inject()(val parser: BodyParsers.Default)(implicit val executionContext: ExecutionContext)
extends ActionBuilder[ItemRequest, AnyContent] with ActionTransformer[Request, ItemRequest] {
def transform[A](request: Request[A]): Future[ItemRequest[A]] = {
ItemServiceClient.getItems.map{
new ItemRequest(_, request)
}recover{
case _ => new ItemRequest(Seq(), request)
}
}
}
Controller:
@Singleton
class ItemController @Inject()(cc: ControllerComponents, deadbolt: DeadboltActions, itemAction: ItemAction) extends AbstractController(cc) with I18nSupport {
def createSomething: Action[AnyContent] = (deadbolt.Pattern("Create_Something", PatternType.EQUALITY) andThen itemAction) { implicit request: ItemRequest[AnyContent] =>
Ok(modules.item.views.html.createSomething(Something.form, request.items))
}
}
[error] Unapplied methods are only converted to functions when a function type is expected. You can make this conversion explicit by writing
Pattern _orPattern(_,_,_,_,_)(_)(_)instead ofPattern.[error] def createSomething: Action[AnyContent] = (deadbolt.Pattern("Create_Deck", PatternType.EQUALITY)() andThen itemAction).synchronized() { implicit request: ItemRequest[AnyContent] =>
Anyone who already dealt with this?
Since
DeadboltActions#Patternreturns anAction, I don't think you can use it for Action composition. Instead, at least with Deadbolt 2.5.1, you may be looking forbe.objectify.deadbolt.scala.SubjectActionBuilder, which is anActionBuilder[AuthenticatedRequest], then you can compose that with yourItemAction.Here's a simple example:
Here's an example that accomplishes what I think you want to do, but does NOT use action composition (at least in the sense I think you mean):