Created
June 20, 2013 15:07
-
-
Save francoisblarel/5823550 to your computer and use it in GitHub Desktop.
Exemple de petit module d'authentication simple et fonctionnel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Trait décrivant l'action protègée' | |
| **/ | |
| import play.api.mvc._ | |
| import play.api.mvc.AnyContent | |
| import play.api.mvc.Result | |
| import play.api.mvc.Security._ | |
| /** | |
| * User: fblarel | |
| * Date: 29/03/13 | |
| */ | |
| trait SecurityTrait { | |
| /* | |
| def isAuthenticated(f: => String => Request[AnyContent] => Result) = { | |
| Authenticated { user => | |
| Action(request => f(user)(request)) | |
| } | |
| } | |
| */ | |
| //in a Security trait | |
| def username(request: RequestHeader) = request.session.get("userConnected") | |
| def onUnauthorized(request: RequestHeader) = Results.Redirect(routes.LoginController.login) | |
| def isAuthenticated(f: => String => Request[AnyContent] => Result) = { | |
| Authenticated(username, onUnauthorized) { user => | |
| Action(request => f(user)(request)) | |
| } | |
| } | |
| } | |
| /** | |
| * Gestion de la phase de login | |
| * | |
| **/ | |
| import play.api.mvc.{Flash, Action, Controller} | |
| import play.api.data.Form | |
| import play.api.data.Forms._ | |
| import models.User | |
| import play.api.data.validation.{Constraints, Constraint} | |
| import play.api.i18n.Messages | |
| /** | |
| * User: fblarel | |
| * Date: 29/03/13 | |
| */ | |
| object LoginController extends Controller{ | |
| private val loginForm : Form[User] = Form( | |
| mapping( | |
| "login" -> nonEmptyText.verifying("login.validation.user.doesnt.exist", User.userExists(_)), | |
| "password" -> nonEmptyText | |
| )(User.apply)(User.unapply).verifying("login.validation.user.password.invalid", User.checkPassword(_)) | |
| ) | |
| def login = Action{ implicit request => | |
| // val theForm = loginForm.bind(flash.data) | |
| Ok(views.html.authenticate(loginForm)) | |
| } | |
| def doLogin() = Action{ implicit request => | |
| this.loginForm.bindFromRequest().fold( | |
| hasErrors = { | |
| form => | |
| val newlogin = form.error("login").isDefined | |
| println(newlogin) | |
| val errors : String = form.globalError.map(err => Messages(err.message)).getOrElse("") | |
| Redirect(routes.LoginController.login()) | |
| .flashing( | |
| "error" -> ("Login error :" + errors), | |
| "newLogin" -> newlogin.toString | |
| ) | |
| }, | |
| success = { | |
| userConnected => | |
| Redirect(routes.Products.list()) | |
| .flashing("success" -> ("Bienvenue " +userConnected.login)) | |
| .withSession(request.session + ("userConnected" -> userConnected.login)) | |
| } | |
| ) | |
| } | |
| def logout = Action{ | |
| Redirect(routes.LoginController.login()).withNewSession | |
| } | |
| } | |
| /** | |
| * Exemple d'utilisation' | |
| **/ | |
| object Application extends Controller with SecurityTrait{ | |
| def index = isAuthenticated{ username => implicit request => | |
| Logger.info("hello "+username) | |
| Redirect(routes.Products.list()) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment