From cb902da50bd75b623a47727e62c0c69913422bfd Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Sun, 8 Feb 2015 00:17:21 +0100 Subject: Authentication draft --- app/controllers/Application.scala | 11 +++-- app/controllers/Auth.scala | 84 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 6 deletions(-) create mode 100644 app/controllers/Auth.scala (limited to 'app/controllers') diff --git a/app/controllers/Application.scala b/app/controllers/Application.scala index 12375df..417ebda 100644 --- a/app/controllers/Application.scala +++ b/app/controllers/Application.scala @@ -2,20 +2,19 @@ package controllers import play.api._ import play.api.mvc._ - +import models._ object Application extends Controller { - def index = Action { - Redirect("/ebe").flashing("warning" -> "hey") - //Ok(views.html.pages.index("Your new application is ready.")) + def index = Authenticate { implicit request => + Ok(views.html.pages.ebeHomepage()) } - def ebe = Action { implicit request => + def ebe = Authenticate { implicit request => Ok(views.html.pages.ebeHomepage()) } - def pepal = Action { implicit request => + def pepal = Authenticate { implicit request => Ok(views.html.pages.pepalHomepage()) } diff --git a/app/controllers/Auth.scala b/app/controllers/Auth.scala new file mode 100644 index 0000000..090259e --- /dev/null +++ b/app/controllers/Auth.scala @@ -0,0 +1,84 @@ +package controllers + +import controllers.Application._ +import play.api._ +import play.api.data._ +import play.api.data.Forms._ +import play.api.mvc._ + +import models._ + +import play.api.db.slick._ +import play.api.db.slick.Config.driver.simple._ +import play.api.Play.current + +import scala.concurrent.Future + + +case class AuthRequest[A](account: Option[Views.Account] = None, request: Request[A]) extends WrappedRequest(request) + +object Authenticate extends ActionBuilder[AuthRequest] { + + def invokeBlock[A](request: Request[A], block: (AuthRequest[A]) => Future[Result]) = DB.withSession { implicit session => + val uuid = request.session.get(Security.username) + var account: Option[Views.Account] = None + + if (uuid.nonEmpty) { + val resultSet = Views.Accounts.filter(_.userUuid === uuid).run + if (resultSet.nonEmpty) { + account = Some(resultSet.head) + } + } + + block(AuthRequest(account, request)) + } + +} + + +case class LoginData(username: String, password: String) + + +object Auth extends Controller { + + val loginForm = Form( + mapping( + "username" -> nonEmptyText, + "password" -> nonEmptyText + )(LoginData.apply)(LoginData.unapply) + verifying("Log in failed.", fields => fields match { + case loginData => checkLoginData(loginData.username, loginData.password) + }) + ) + + def checkLoginData(username: String, password: String) = DB.withSession { implicit session => + Tables.Users.filter(u => u.username === username && u.userPassword === password).length.run > 0 + } + + + def login = Action { implicit request => + Ok(views.html.pages.loginForm(loginForm)) + } + + def loginSubmit = DBAction { implicit request => + loginForm.bindFromRequest.fold( + formWithErrors => { + BadRequest(views.html.pages.loginForm(formWithErrors)) + }, + validForm => { + val userUuid: String = Tables.Users.filter(_.username === validForm.username).map(_.uuid).first.run + + Redirect(routes.Application.index()) + .withSession(Security.username -> userUuid) + .flashing(("success", "Welcome, valuable user!")) + } + ) + } + + def logout = Action { implicit request => + Redirect(routes.Application.index()).withNewSession.flashing( + "success" -> "You are now logged out. Do not go to our competitor's website. Thanks." + ) + } + +} -- cgit v1.2.3