From 9079a273501a916262bc50e52f722a9311f12825 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Sun, 8 Feb 2015 11:09:57 +0100 Subject: Refactor Auth --- app/controllers/Application.scala | 15 ++++- app/controllers/Auth.scala | 84 -------------------------- app/controllers/Authentication.scala | 92 +++++++++++++++++++++++++++++ app/views/fragments/accountInfos.scala.html | 6 +- app/views/pages/loginForm.scala.html | 2 +- conf/routes | 6 +- 6 files changed, 111 insertions(+), 94 deletions(-) delete mode 100644 app/controllers/Auth.scala create mode 100644 app/controllers/Authentication.scala diff --git a/app/controllers/Application.scala b/app/controllers/Application.scala index 417ebda..6e6acd2 100644 --- a/app/controllers/Application.scala +++ b/app/controllers/Application.scala @@ -1,20 +1,29 @@ package controllers import play.api._ +import play.api.data._ +import play.api.data.Forms._ import play.api.mvc._ + +import play.api.db.slick._ +import play.api.db.slick.Config.driver.simple._ +import play.api.Play.current + +import scala.concurrent.Future + import models._ object Application extends Controller { - def index = Authenticate { implicit request => + def index = Auth { implicit request => Ok(views.html.pages.ebeHomepage()) } - def ebe = Authenticate { implicit request => + def ebe = Auth { implicit request => Ok(views.html.pages.ebeHomepage()) } - def pepal = Authenticate { implicit request => + def pepal = Auth { implicit request => Ok(views.html.pages.pepalHomepage()) } diff --git a/app/controllers/Auth.scala b/app/controllers/Auth.scala deleted file mode 100644 index 090259e..0000000 --- a/app/controllers/Auth.scala +++ /dev/null @@ -1,84 +0,0 @@ -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." - ) - } - -} diff --git a/app/controllers/Authentication.scala b/app/controllers/Authentication.scala new file mode 100644 index 0000000..f9772e2 --- /dev/null +++ b/app/controllers/Authentication.scala @@ -0,0 +1,92 @@ +package controllers + +import play.api._ +import play.api.data._ +import play.api.data.Forms._ +import play.api.mvc._ + +import play.api.db.slick._ +import play.api.db.slick.Config.driver.simple._ +import play.api.Play.current + +import scala.concurrent.Future + +import models._ + + +case class AuthRequest[A](account: Option[Views.Account] = None, request: Request[A]) extends WrappedRequest(request) + +object Auth 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 Authentication 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 = Auth { implicit request => + if (request.account.isEmpty) { + Ok(views.html.pages.loginForm(loginForm)) + } else { + Redirect(routes.Application.index()) + } + } + + def loginSubmit = Auth { implicit request => + DB.withSession { implicit session => + 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 = Auth { implicit request => + if (request.account.nonEmpty) { + Redirect(routes.Application.index()) + .withNewSession + .flashing("success" -> "You are now logged out. Do not go to our competitor's website. Thanks.") + } else { + Redirect(routes.Application.index()) + } + } + +} diff --git a/app/views/fragments/accountInfos.scala.html b/app/views/fragments/accountInfos.scala.html index 8fb0aab..6bf8655 100644 --- a/app/views/fragments/accountInfos.scala.html +++ b/app/views/fragments/accountInfos.scala.html @@ -2,7 +2,7 @@ @accountData match { case Some(account) => { - + Logout @@ -19,12 +19,12 @@ } case None => { - + Sign up - + Log in diff --git a/app/views/pages/loginForm.scala.html b/app/views/pages/loginForm.scala.html index 5e4d8e5..f359082 100644 --- a/app/views/pages/loginForm.scala.html +++ b/app/views/pages/loginForm.scala.html @@ -9,7 +9,7 @@ @views.html.fragments.forms.globalErrors(loginForm) - @helper.form(action = routes.Auth.loginSubmit(), 'class -> "pure-form") { + @helper.form(action = routes.Authentication.loginSubmit(), 'class -> "pure-form") { @helper.CSRF.formField diff --git a/conf/routes b/conf/routes index 095c886..f35a99b 100644 --- a/conf/routes +++ b/conf/routes @@ -12,10 +12,10 @@ GET /terms controllers.Application.index GET /privacy controllers.Application.index # User account -GET /login controllers.Auth.login -POST /login controllers.Auth.loginSubmit +GET /login controllers.Authentication.login +POST /login controllers.Authentication.loginSubmit -GET /logout controllers.Auth.logout +GET /logout controllers.Authentication.logout # #GET /signup controllers.Application.index #POST /signup controllers.Application.index -- cgit v1.2.3