In the zentasks example for Play2 we have the method
def isAuthenticated(f: => String => Request[AnyContent] => Result) = {
Security.Authenticated(username, onUnauthorized) { user =>
Action(request => f(user)(request))
}
}
What I would like to do is add another method that I could use if I wanted to get the user directly from the database.
It gets a little boring having to add a wrapper in all methods
def method() = isAuthenticated { username => implicit request =>
UserDAO.findOneByEmail(username).map { user =>
Ok(html.user.view(user))
}.getOrElse(Forbidden)
}
I'm new to functional programming and all these =>
is making my head spin :)
Any suggestions?
You can define another method, for example
IsAuthenticatedUser
, which would take a parameter of typeUser => Request[AnyContent] => Result
:You can then use it like as follows:
Semi-solution is to encapsulate fetching user data steps into some function and calling it from template level (not on each action), as every template is just a scala function.
Thanks to this approach if you have several actions using the same view (or even layout) you don't have to fetch logged user everytime ie:
in the
user view
:This was the final solution
Usage:
As you can see, if the session doesn't exist, the user gets redirected to the login page as specified in the trait.
And same goes if the username can't be found in the DAO.