Skip to content

Instantly share code, notes, and snippets.

@irumiha
Created July 29, 2012 21:12
Show Gist options
  • Save irumiha/3201859 to your computer and use it in GitHub Desktop.
Save irumiha/3201859 to your computer and use it in GitHub Desktop.
trait Security {
/**
* Retrieve the connected user email.
*/
private def username(request: RequestHeader) =
request.session.get("username")
/**
* Redirect to login if the user in not authorized.
*/
private def onUnauthorized(request: RequestHeader) =
Results.Redirect(routes.Application.login())
/**
* Action for authenticated users.
*/
def ifAuthenticated(f: String => Request[AnyContent] => Result) =
Action { req =>
username(req).map(f(_)(req)) getOrElse(onUnauthorized(req))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment