Using deadbolt2 I have the following controller function implementation:
def restricted: Action = deadbolt.Restrict(List(Array(USER_ROLE)))() { request =>
Future {
val localUser = userProvider.getUser(request.session) // <<< expects a play.mvc.Http.Session
Ok(views.html.restricted(userProvider, localUser))
}
}
but it results in the following compiler error:
[error] /home/bravegag/code/play-authenticate-usage-scala/app/controllers/Application.scala:26: type mismatch;
[error] found : play.api.mvc.Session
[error] required: play.mvc.Http.Session
[error] val localUser = userProvider.getUser(request.session)
[error] ^
Basically the current request
is giving me a play.api.mvc.Session
but the library I depend on (play-authenticate) is expecting a play.mvc.Http.Session
. Is there a way to convert between the two? or another way to get hold of the needed Http one?
I found how to convert from play.api.mvc.Session
to play.mvc.Http.Session
:
import scala.collection.JavaConversions
val session : Http.Session =
new Http.Session(JavaConversions.mapAsJavaMap(request.session.data))
thouhg I will have to keep redoing this everywhere ... or write an implicit conversion Helper object.
UPDATE Defined my own implicit conversion helper:
package utils
import scala.collection.JavaConversions
object PlayConversions {
/**
* Returns the result conversion from a play.api.mvc.Session to a play.mvc.Http.Session
* @param session play.api.mvc.Session instance
* @return the result conversion from a play.api.mvc.Session to a play.mvc.Http.Session
*/
implicit def toHttpSession(session: play.api.mvc.Session) = new play.mvc.Http.Session(JavaConversions.mapAsJavaMap(session.data))
}
UPDATE Actually this is the Play institutional/preferred way to do it:
import play.core.j.JavaHelpers
val context = JavaHelpers.createJavaContext(request)
// and now access the Java Http.Session
context.session