Skip to content

Instantly share code, notes, and snippets.

@nraychaudhuri
Created May 14, 2014 18:51
Show Gist options
  • Save nraychaudhuri/fd77e4877b3af4622331 to your computer and use it in GitHub Desktop.
Save nraychaudhuri/fd77e4877b3af4622331 to your computer and use it in GitHub Desktop.
Play2-Auth and Slick together
//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)
}
}
}
@marius-carp
Copy link

@marius-carp
Copy link

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))
      }
    }
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment