AuthenticatedBuilder

play.api.mvc.Security.AuthenticatedBuilder
See theAuthenticatedBuilder companion object
class AuthenticatedBuilder[U](userinfo: RequestHeader => Option[U], defaultParser: BodyParser[AnyContent], onUnauthorized: RequestHeader => Result)(implicit val executionContext: ExecutionContext) extends ActionBuilder[{ type R = AuthenticatedRequest; }#<none>, AnyContent]

An authenticated action builder.

This can be used to create an action builder, like so:

class UserAuthenticatedBuilder (parser: BodyParser[AnyContent])(implicit ec: ExecutionContext)
 extends AuthenticatedBuilder[User]({ req: RequestHeader =>
 req.session.get("user").map(User)
}, parser) {
 @Inject()
 def this(parser: BodyParsers.Default)(implicit ec: ExecutionContext) = {
   this(parser: BodyParser[AnyContent])
 }
}

You can then use the authenticated builder with other action builders, i.e. to use a messagesApi with authentication, you can add:

class AuthMessagesRequest[A](val user: User,
                            messagesApi: MessagesApi,
                            request: Request[A])
extends MessagesRequest[A](request, messagesApi)

class AuthenticatedActionBuilder(val parser: BodyParser[AnyContent],
                                messagesApi: MessagesApi,
                                builder: AuthenticatedBuilder[User])
                               (implicit val executionContext: ExecutionContext)
   extends ActionBuilder[AuthMessagesRequest, AnyContent] {
 type ResultBlock[A] = (AuthMessagesRequest[A]) => Future[Result]

 @Inject
 def this(parser: BodyParsers.Default,
          messagesApi: MessagesApi,
          builder: UserAuthenticatedBuilder)(implicit ec: ExecutionContext) = {
   this(parser: BodyParser[AnyContent], messagesApi, builder)
 }

 def invokeBlock[A](request: Request[A], block: ResultBlock[A]): Future[Result] = {
   builder.authenticate(request, { authRequest: AuthenticatedRequest[A, User] =>
     block(new AuthMessagesRequest[A](authRequest.user, messagesApi, request))
   })
 }
}

Value parameters

onUnauthorized

The function to get the result for when no authenticated user can be found.

userinfo

The function that looks up the user info.

Attributes

Companion
object
Graph
Supertypes
trait ActionBuilder[{ type R = AuthenticatedRequest; }#<none>, AnyContent]
trait ActionFunction[Request, { type R = AuthenticatedRequest; }#<none>]
class Object
trait Matchable
class Any

Members list

Value members

Concrete methods

def authenticate[A](request: Request[A], block: (AuthenticatedRequest[A, U]) => Future[Result]): Future[Result]

Authenticate the given block.

Authenticate the given block.

Attributes

def invokeBlock[A](request: Request[A], block: (AuthenticatedRequest[A, U]) => Future[Result]): Future[Result]

Invoke the block. This is the main method that an ActionBuilder has to implement, at this stage it can wrap it in any other actions, modify the request object or potentially use a different class to represent the request.

Invoke the block. This is the main method that an ActionBuilder has to implement, at this stage it can wrap it in any other actions, modify the request object or potentially use a different class to represent the request.

Value parameters

block

The block of code to invoke

request

The request

Attributes

Returns

A future of the result

Inherited methods

Compose this ActionFunction with another, with this one applied first.

Compose this ActionFunction with another, with this one applied first.

Value parameters

other

ActionFunction with which to compose

Attributes

Returns

The new ActionFunction

Definition Classes
Inherited from:
ActionBuilder
final def apply(block: => Result): Action[AnyContent]

Constructs an Action with default content, and no request parameter.

Constructs an Action with default content, and no request parameter.

For example:

val hello = Action {
 Ok("Hello!")
}

Value parameters

block

the action code

Attributes

Returns

an action

Inherited from:
ActionBuilder

Constructs an Action with default content.

Constructs an Action with default content.

For example:

val echo = Action { request =>
 Ok("Got request [" + request + "]")
}

Value parameters

block

the action code

Attributes

Returns

an action

Inherited from:
ActionBuilder
final def apply[A](bodyParser: BodyParser[A]): ActionBuilder[{ type R = AuthenticatedRequest; }#<none>, A]

Constructs an ActionBuilder with the given BodyParser. The result can then be applied directly to a block.

Constructs an ActionBuilder with the given BodyParser. The result can then be applied directly to a block.

For example:

val echo = Action(parse.anyContent) { request =>
 Ok("Got request [" + request + "]")
}

Type parameters

A

the type of the request body

Value parameters

bodyParser

the BodyParser to use to parse the request body

Attributes

Returns

an action

Inherited from:
ActionBuilder
final def async[A](bodyParser: BodyParser[A])(block: (AuthenticatedRequest[A, U]) => Future[Result]): Action[A]

Constructs an Action with the given BodyParser that returns a future of a result.

Constructs an Action with the given BodyParser that returns a future of a result.

For example:

val hello = Action.async(parse.anyContent) { request =>
 ws.url(request.getQueryString("url").get).get().map { r =>
   if (r.status == 200) Ok("The website is up") else NotFound("The website is down")
 }
}

Value parameters

block

the action code

Attributes

Returns

an action

Inherited from:
ActionBuilder

Constructs an Action that returns a future of a result, with default content.

Constructs an Action that returns a future of a result, with default content.

For example:

val hello = Action.async { request =>
 ws.url(request.getQueryString("url").get).get().map { r =>
   if (r.status == 200) Ok("The website is up") else NotFound("The website is down")
 }
}

Value parameters

block

the action code

Attributes

Returns

an action

Inherited from:
ActionBuilder
final def async(block: => Future[Result]): Action[AnyContent]

Constructs an Action that returns a future of a result, with default content, and no request parameter.

Constructs an Action that returns a future of a result, with default content, and no request parameter.

For example:

val hello = Action.async {
 ws.url("http://www.playframework.com").get().map { r =>
   if (r.status == 200) Ok("The website is up") else NotFound("The website is down")
 }
}

Value parameters

block

the action code

Attributes

Returns

an action

Inherited from:
ActionBuilder
def compose[B](other: ActionBuilder[Request, B]): ActionBuilder[{ type R = AuthenticatedRequest; }#<none>, B]

Attributes

Inherited from:
ActionFunction
def compose[Q[_]](other: ActionFunction[Q, Request]): ActionFunction[Q, { type R = AuthenticatedRequest; }#<none>]

Compose another ActionFunction with this one, with this one applied last.

Compose another ActionFunction with this one, with this one applied last.

Value parameters

other

ActionFunction with which to compose

Attributes

Returns

The new ActionFunction

Inherited from:
ActionFunction

Concrete fields

Attributes

Returns

The BodyParser to be used by this ActionBuilder if no other is specified

Implicits

Implicits

Attributes

Returns

The execution context to run the request in.