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 ++++++++++++++++++++++ app/views/fragments/accountInfos.scala.html | 46 ++++++++---- app/views/fragments/branding/ebeLogo.scala.html | 1 + app/views/fragments/branding/ebeSlogan.scala.html | 1 + app/views/fragments/branding/pepalLogo.scala.html | 1 + .../fragments/branding/pepalSlogan.scala.html | 1 + app/views/fragments/ebeLogo.scala.html | 1 - app/views/fragments/ebeSlogan.scala.html | 1 - app/views/fragments/forms/globalErrors.scala.html | 7 ++ app/views/fragments/forms/inputField.scala.html | 10 +++ app/views/fragments/forms/textarea.scala.html | 8 +++ app/views/fragments/pepalLogo.scala.html | 1 - app/views/fragments/pepalSlogan.scala.html | 1 - app/views/pages/ebeHomepage.scala.html | 10 +-- app/views/pages/loginForm.scala.html | 27 +++++++ app/views/pages/pepalHomepage.scala.html | 8 +-- app/views/templates/ebe.scala.html | 4 +- app/views/templates/main.scala.html | 20 +++--- app/views/templates/pepal.scala.html | 4 +- 20 files changed, 201 insertions(+), 46 deletions(-) create mode 100644 app/controllers/Auth.scala create mode 100644 app/views/fragments/branding/ebeLogo.scala.html create mode 100644 app/views/fragments/branding/ebeSlogan.scala.html create mode 100644 app/views/fragments/branding/pepalLogo.scala.html create mode 100644 app/views/fragments/branding/pepalSlogan.scala.html delete mode 100644 app/views/fragments/ebeLogo.scala.html delete mode 100644 app/views/fragments/ebeSlogan.scala.html create mode 100644 app/views/fragments/forms/globalErrors.scala.html create mode 100644 app/views/fragments/forms/inputField.scala.html create mode 100644 app/views/fragments/forms/textarea.scala.html delete mode 100644 app/views/fragments/pepalLogo.scala.html delete mode 100644 app/views/fragments/pepalSlogan.scala.html create mode 100644 app/views/pages/loginForm.scala.html (limited to 'app') 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." + ) + } + +} diff --git a/app/views/fragments/accountInfos.scala.html b/app/views/fragments/accountInfos.scala.html index 711fb40..8fb0aab 100644 --- a/app/views/fragments/accountInfos.scala.html +++ b/app/views/fragments/accountInfos.scala.html @@ -1,14 +1,32 @@ - - - Logout - - - - - -400 000 € - - - - - Mr. Kornada - +@(accountData: Option[models.Views.Account] = None) + +@accountData match { + case Some(account) => { + + + Logout + + + + + @account.equity € + + + + + @account.username + + } + + case None => { + + + Sign up + + + + + Log in + + } +} diff --git a/app/views/fragments/branding/ebeLogo.scala.html b/app/views/fragments/branding/ebeLogo.scala.html new file mode 100644 index 0000000..f2b552a --- /dev/null +++ b/app/views/fragments/branding/ebeLogo.scala.html @@ -0,0 +1 @@ +ebé \ No newline at end of file diff --git a/app/views/fragments/branding/ebeSlogan.scala.html b/app/views/fragments/branding/ebeSlogan.scala.html new file mode 100644 index 0000000..c0b5332 --- /dev/null +++ b/app/views/fragments/branding/ebeSlogan.scala.html @@ -0,0 +1 @@ +Le marketplace \ No newline at end of file diff --git a/app/views/fragments/branding/pepalLogo.scala.html b/app/views/fragments/branding/pepalLogo.scala.html new file mode 100644 index 0000000..a4b75ae --- /dev/null +++ b/app/views/fragments/branding/pepalLogo.scala.html @@ -0,0 +1 @@ +PéPal \ No newline at end of file diff --git a/app/views/fragments/branding/pepalSlogan.scala.html b/app/views/fragments/branding/pepalSlogan.scala.html new file mode 100644 index 0000000..a6d6e69 --- /dev/null +++ b/app/views/fragments/branding/pepalSlogan.scala.html @@ -0,0 +1 @@ +Le money \ No newline at end of file diff --git a/app/views/fragments/ebeLogo.scala.html b/app/views/fragments/ebeLogo.scala.html deleted file mode 100644 index f2b552a..0000000 --- a/app/views/fragments/ebeLogo.scala.html +++ /dev/null @@ -1 +0,0 @@ -ebé \ No newline at end of file diff --git a/app/views/fragments/ebeSlogan.scala.html b/app/views/fragments/ebeSlogan.scala.html deleted file mode 100644 index c0b5332..0000000 --- a/app/views/fragments/ebeSlogan.scala.html +++ /dev/null @@ -1 +0,0 @@ -Le marketplace \ No newline at end of file diff --git a/app/views/fragments/forms/globalErrors.scala.html b/app/views/fragments/forms/globalErrors.scala.html new file mode 100644 index 0000000..98dad24 --- /dev/null +++ b/app/views/fragments/forms/globalErrors.scala.html @@ -0,0 +1,7 @@ +@(form: Form[_ <: Product]) + +
diff --git a/app/views/fragments/forms/inputField.scala.html b/app/views/fragments/forms/inputField.scala.html new file mode 100644 index 0000000..7de0e3b --- /dev/null +++ b/app/views/fragments/forms/inputField.scala.html @@ -0,0 +1,10 @@ +@(field: Field, inputType: String, label: String) + + diff --git a/app/views/fragments/forms/textarea.scala.html b/app/views/fragments/forms/textarea.scala.html new file mode 100644 index 0000000..44904ca --- /dev/null +++ b/app/views/fragments/forms/textarea.scala.html @@ -0,0 +1,8 @@ +@(field: Field, label: String) + + diff --git a/app/views/fragments/pepalLogo.scala.html b/app/views/fragments/pepalLogo.scala.html deleted file mode 100644 index a4b75ae..0000000 --- a/app/views/fragments/pepalLogo.scala.html +++ /dev/null @@ -1 +0,0 @@ -PéPal \ No newline at end of file diff --git a/app/views/fragments/pepalSlogan.scala.html b/app/views/fragments/pepalSlogan.scala.html deleted file mode 100644 index a6d6e69..0000000 --- a/app/views/fragments/pepalSlogan.scala.html +++ /dev/null @@ -1 +0,0 @@ -Le money \ No newline at end of file diff --git a/app/views/pages/ebeHomepage.scala.html b/app/views/pages/ebeHomepage.scala.html index da1fb08..d4fe80a 100644 --- a/app/views/pages/ebeHomepage.scala.html +++ b/app/views/pages/ebeHomepage.scala.html @@ -1,6 +1,6 @@ -@()(implicit flash: Flash) +@()(implicit request : AuthRequest[AnyContent], flash: Flash, token: play.filters.csrf.CSRF.Token) -@templates.ebe("eBé") { +@templates.ebe("eBé")(request.account) { @@ -22,7 +22,7 @@- @fragments.ebeLogo() guarantees all transactions for both buyers and sellers. + @fragments.branding.ebeLogo() guarantees all transactions for both buyers and sellers.
- With @fragments.ebeLogo(), you are not required to give any personal information to any buyer or seller. + With @fragments.branding.ebeLogo(), you are not required to give any personal information to any buyer or seller.
- With @fragments.pepalLogo(), you are not required to give any personal information to any buyer or seller. + With @fragments.branding.pepalLogo(), you are not required to give any personal information to any buyer or seller.