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 @@ + \ 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 @@ + \ 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 @@ - \ 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]) + +
+ @for(error <- form.globalErrors) { +
@error.message
+ } +
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 @@ - \ 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) {
@fragments.ebeMainActions() @@ -13,7 +13,7 @@

Get started today

- With @fragments.ebeLogo(), the leading online auction platform, buy or sell items in a few clicks! + With @fragments.branding.ebeLogo(), the leading online auction platform, buy or sell items in a few clicks!

@@ -22,7 +22,7 @@

Buy and sell with confidence

- @fragments.ebeLogo() guarantees all transactions for both buyers and sellers. + @fragments.branding.ebeLogo() guarantees all transactions for both buyers and sellers.

@@ -40,7 +40,7 @@

Secure transactions

- 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.

diff --git a/app/views/pages/loginForm.scala.html b/app/views/pages/loginForm.scala.html new file mode 100644 index 0000000..5e4d8e5 --- /dev/null +++ b/app/views/pages/loginForm.scala.html @@ -0,0 +1,27 @@ +@(loginForm: Form[LoginData])(implicit flash: Flash, token: play.filters.csrf.CSRF.Token) + +@templates.ebe("Log in")() { + +
+
+ +

Log in

+ + @views.html.fragments.forms.globalErrors(loginForm) + + @helper.form(action = routes.Auth.loginSubmit(), 'class -> "pure-form") { + + @helper.CSRF.formField + +
+ @views.html.fragments.forms.inputField(loginForm("username"), "text", "Username") + @views.html.fragments.forms.inputField(loginForm("password"), "password", "Password") +
+ + + } + +
+
+ +} diff --git a/app/views/pages/pepalHomepage.scala.html b/app/views/pages/pepalHomepage.scala.html index b4a8e9c..9f4b227 100644 --- a/app/views/pages/pepalHomepage.scala.html +++ b/app/views/pages/pepalHomepage.scala.html @@ -1,6 +1,6 @@ -@()(implicit flash: Flash) +@()(implicit request : AuthRequest[AnyContent], flash: Flash, token: play.filters.csrf.CSRF.Token) -@templates.pepal("PéPal") { +@templates.pepal("PéPal")(request.account) {
@fragments.pepalMainActions() @@ -13,7 +13,7 @@

Get started today

- With @fragments.pepalLogo(), the leading online payment solution, buy or sell items on the web in a few clicks! + With @fragments.branding.pepalLogo(), the leading online payment solution, buy or sell items on the web in a few clicks!

@@ -40,7 +40,7 @@

Secure transactions

- 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.

diff --git a/app/views/templates/ebe.scala.html b/app/views/templates/ebe.scala.html index 2bab310..8e8fc8e 100644 --- a/app/views/templates/ebe.scala.html +++ b/app/views/templates/ebe.scala.html @@ -1,3 +1,3 @@ -@(title: String)(content: Html)(implicit flash: Flash) +@(title: String)(accountData: Option[models.Views.Account] = None)(content: Html)(implicit flash: Flash, token: play.filters.csrf.CSRF.Token) -@templates.main{@fragments.ebeLogo()}{@fragments.ebeSlogan()}(title)(content) +@templates.main{@fragments.branding.ebeLogo()}{@fragments.branding.ebeSlogan()}(title)(accountData)(content) diff --git a/app/views/templates/main.scala.html b/app/views/templates/main.scala.html index 278b205..5ec9da2 100644 --- a/app/views/templates/main.scala.html +++ b/app/views/templates/main.scala.html @@ -1,4 +1,4 @@ -@(logo: Html)(slogan: Html)(title: String)(content: Html)(implicit flash: Flash) +@(logo: Html)(slogan: Html)(title: String)(accountData: Option[models.Views.Account] = None)(content: Html)(implicit flash: Flash, token: play.filters.csrf.CSRF.Token) @@ -22,12 +22,14 @@ - - + @if(accountData.isDefined) { + + + } - +
This is a dummy scholar project! Do not use for real serious business stuff! @@ -39,14 +41,14 @@
- +
@@ -58,9 +60,9 @@ © @logo Inc. All rights reserved. - Terms of use + Terms of use - - Privacy policy + Privacy policy diff --git a/app/views/templates/pepal.scala.html b/app/views/templates/pepal.scala.html index ec6d5dd..6f5d5ae 100644 --- a/app/views/templates/pepal.scala.html +++ b/app/views/templates/pepal.scala.html @@ -1,3 +1,3 @@ -@(title: String)(content: Html)(implicit flash: Flash) +@(title: String)(accountData: Option[models.Views.Account] = None)(content: Html)(implicit flash: Flash, token: play.filters.csrf.CSRF.Token) -@templates.main{@fragments.pepalLogo()}{@fragments.pepalSlogan()}(title)(content) +@templates.main{@fragments.branding.pepalLogo()}{@fragments.branding.pepalSlogan()}(title)(accountData)(content) -- cgit v1.2.3