-
-
Save nraychaudhuri/fd77e4877b3af4622331 to your computer and use it in GitHub Desktop.
//Trait that bridges between slick and Play2-auth. Just mixin with controllers | |
trait Auth2SlickBridge { | |
self: StackableController => | |
def AuthAction[A](p: BodyParser[A], params: (RequestAttributeKey[_], Any)*)(f: RequestWithAttributes[A] => Action[A]): Action[A] = { | |
AsyncStack(p, params:_*) { implicit rs => | |
f(rs).apply(rs) | |
} | |
} | |
def AuthAction(params: (RequestAttributeKey[_], Any)*)(f: RequestWithAttributes[AnyContent] => Action[AnyContent]): Action[AnyContent] = { | |
AsyncStack(params:_*) { implicit rs => | |
f(rs).apply(rs) | |
} | |
} | |
} | |
//exmaple usage | |
object Message extends Controller with AuthElement with Auth2SlickBridge with AuthConfigImpl { | |
def main = AuthAction(AuthorityKey -> "user") { implicit play2AuthRequest => | |
DBAction { implicit slickRequest => | |
Computers.findById(1L).map { computer => | |
Ok(html.editForm(1L, Application.computerForm.fill(computer), Companies.options)) | |
}.getOrElse(NotFound) | |
} | |
} | |
} |
Hi!
I agree with @marius-carp
I've started migration from Play-Framework 2.3 to Play-Framework 2.4 and I got error:
Error:Play 2 Compiler:
(f: jp.t2v.lab.play2.stackc.RequestWithAttributes[play.api.mvc.AnyContent] => scala.concurrent.Future[play.api.mvc.Result])play.api.mvc.Action[play.api.mvc.AnyContent]
(params: jp.t2v.lab.play2.stackc.Attribute[]*)(f: jp.t2v.lab.play2.stackc.RequestWithAttributes[play.api.mvc.AnyContent] => scala.concurrent.Future[play.api.mvc.Result])play.api.mvc.Action[play.api.mvc.AnyContent]
[A](p: play.api.mvc.BodyParser[A], params: jp.t2v.lab.play2.stackc.Attribute[]*)(f: jp.t2v.lab.play2.stackc.RequestWithAttributes[A] => scala.concurrent.Future[play.api.mvc.Result])play.api.mvc.Action[A]
I've successfully migrated ).
And here updated version of this example:
https://gist.github.com/Bunyod/c84d7cac0528594d050b
Hi @Bunyod there is no more DBAction in Play-Slick 1.x https://www.playframework.com/documentation/2.4.x/PlaySlickMigrationGuide#DBAction-and-DBSessionRequest-were-removed
The actions look good.
This is the code that completes yours actions. It is adjusted to my code, I send Json as result
def getUser(idUser: Int) = AuthAction(AuthorityKey -> NormalUser) { implicit rs =>
Action.async { implicit slickRequest =>
dao.findById(idUser).map{ user =>
Ok(Json.toJson(user))
}
}
}
}
Hi,
Can you please update this example to the last versions of Play2-auth (0.14.1) and Play-Slick (1.1.0-M2) on Play-Framework 2.4
Thanks!