aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--app/controllers/Application.scala15
-rw-r--r--app/controllers/Authentication.scala (renamed from app/controllers/Auth.scala)58
-rw-r--r--app/views/fragments/accountInfos.scala.html6
-rw-r--r--app/views/pages/loginForm.scala.html2
-rw-r--r--conf/routes6
5 files changed, 52 insertions, 35 deletions
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 @@
1package controllers 1package controllers
2 2
3import play.api._ 3import play.api._
4import play.api.data._
5import play.api.data.Forms._
4import play.api.mvc._ 6import play.api.mvc._
7
8import play.api.db.slick._
9import play.api.db.slick.Config.driver.simple._
10import play.api.Play.current
11
12import scala.concurrent.Future
13
5import models._ 14import models._
6 15
7object Application extends Controller { 16object Application extends Controller {
8 17
9 def index = Authenticate { implicit request => 18 def index = Auth { implicit request =>
10 Ok(views.html.pages.ebeHomepage()) 19 Ok(views.html.pages.ebeHomepage())
11 } 20 }
12 21
13 def ebe = Authenticate { implicit request => 22 def ebe = Auth { implicit request =>
14 Ok(views.html.pages.ebeHomepage()) 23 Ok(views.html.pages.ebeHomepage())
15 } 24 }
16 25
17 def pepal = Authenticate { implicit request => 26 def pepal = Auth { implicit request =>
18 Ok(views.html.pages.pepalHomepage()) 27 Ok(views.html.pages.pepalHomepage())
19 } 28 }
20 29
diff --git a/app/controllers/Auth.scala b/app/controllers/Authentication.scala
index 090259e..f9772e2 100644
--- a/app/controllers/Auth.scala
+++ b/app/controllers/Authentication.scala
@@ -1,23 +1,22 @@
1package controllers 1package controllers
2 2
3import controllers.Application._
4import play.api._ 3import play.api._
5import play.api.data._ 4import play.api.data._
6import play.api.data.Forms._ 5import play.api.data.Forms._
7import play.api.mvc._ 6import play.api.mvc._
8 7
9import models._
10
11import play.api.db.slick._ 8import play.api.db.slick._
12import play.api.db.slick.Config.driver.simple._ 9import play.api.db.slick.Config.driver.simple._
13import play.api.Play.current 10import play.api.Play.current
14 11
15import scala.concurrent.Future 12import scala.concurrent.Future
16 13
14import models._
15
17 16
18case class AuthRequest[A](account: Option[Views.Account] = None, request: Request[A]) extends WrappedRequest(request) 17case class AuthRequest[A](account: Option[Views.Account] = None, request: Request[A]) extends WrappedRequest(request)
19 18
20object Authenticate extends ActionBuilder[AuthRequest] { 19object Auth extends ActionBuilder[AuthRequest] {
21 20
22 def invokeBlock[A](request: Request[A], block: (AuthRequest[A]) => Future[Result]) = DB.withSession { implicit session => 21 def invokeBlock[A](request: Request[A], block: (AuthRequest[A]) => Future[Result]) = DB.withSession { implicit session =>
23 val uuid = request.session.get(Security.username) 22 val uuid = request.session.get(Security.username)
@@ -38,8 +37,7 @@ object Authenticate extends ActionBuilder[AuthRequest] {
38 37
39case class LoginData(username: String, password: String) 38case class LoginData(username: String, password: String)
40 39
41 40object Authentication extends Controller {
42object Auth extends Controller {
43 41
44 val loginForm = Form( 42 val loginForm = Form(
45 mapping( 43 mapping(
@@ -56,29 +54,39 @@ object Auth extends Controller {
56 } 54 }
57 55
58 56
59 def login = Action { implicit request => 57 def login = Auth { implicit request =>
60 Ok(views.html.pages.loginForm(loginForm)) 58 if (request.account.isEmpty) {
59 Ok(views.html.pages.loginForm(loginForm))
60 } else {
61 Redirect(routes.Application.index())
62 }
61 } 63 }
62 64
63 def loginSubmit = DBAction { implicit request => 65 def loginSubmit = Auth { implicit request =>
64 loginForm.bindFromRequest.fold( 66 DB.withSession { implicit session =>
65 formWithErrors => { 67 loginForm.bindFromRequest.fold(
66 BadRequest(views.html.pages.loginForm(formWithErrors)) 68 formWithErrors => {
67 }, 69 BadRequest(views.html.pages.loginForm(formWithErrors))
68 validForm => { 70 },
69 val userUuid: String = Tables.Users.filter(_.username === validForm.username).map(_.uuid).first.run 71 validForm => {
70 72 val userUuid: String = Tables.Users.filter(_.username === validForm.username).map(_.uuid).first.run
71 Redirect(routes.Application.index()) 73
72 .withSession(Security.username -> userUuid) 74 Redirect(routes.Application.index())
73 .flashing(("success", "Welcome, valuable user!")) 75 .withSession(Security.username -> userUuid)
74 } 76 .flashing(("success", "Welcome, valuable user!"))
75 ) 77 }
78 )
79 }
76 } 80 }
77 81
78 def logout = Action { implicit request => 82 def logout = Auth { implicit request =>
79 Redirect(routes.Application.index()).withNewSession.flashing( 83 if (request.account.nonEmpty) {
80 "success" -> "You are now logged out. Do not go to our competitor's website. Thanks." 84 Redirect(routes.Application.index())
81 ) 85 .withNewSession
86 .flashing("success" -> "You are now logged out. Do not go to our competitor's website. Thanks.")
87 } else {
88 Redirect(routes.Application.index())
89 }
82 } 90 }
83 91
84} 92}
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 @@
2 2
3@accountData match { 3@accountData match {
4 case Some(account) => { 4 case Some(account) => {
5 <a class="pure-button" href="@routes.Auth.logout()"> 5 <a class="pure-button" href="@routes.Authentication.logout()">
6 <i class="fa fa-sign-out fa-lg"></i> 6 <i class="fa fa-sign-out fa-lg"></i>
7 Logout 7 Logout
8 </a> 8 </a>
@@ -19,12 +19,12 @@
19 } 19 }
20 20
21 case None => { 21 case None => {
22 <a class="pure-button" href="@routes.Auth.logout()"> 22 <a class="pure-button" href="@routes.Authentication.logout()">
23 <i class="fa fa-pencil-square-o fa-lg"></i> 23 <i class="fa fa-pencil-square-o fa-lg"></i>
24 Sign up 24 Sign up
25 </a> 25 </a>
26 26
27 <a class="pure-button" href="@routes.Auth.login()"> 27 <a class="pure-button" href="@routes.Authentication.login()">
28 <i class="fa fa-sign-in fa-lg"></i> 28 <i class="fa fa-sign-in fa-lg"></i>
29 Log in 29 Log in
30 </a> 30 </a>
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 @@
9 9
10 @views.html.fragments.forms.globalErrors(loginForm) 10 @views.html.fragments.forms.globalErrors(loginForm)
11 11
12 @helper.form(action = routes.Auth.loginSubmit(), 'class -> "pure-form") { 12 @helper.form(action = routes.Authentication.loginSubmit(), 'class -> "pure-form") {
13 13
14 @helper.CSRF.formField 14 @helper.CSRF.formField
15 15
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
12GET /privacy controllers.Application.index 12GET /privacy controllers.Application.index
13 13
14# User account 14# User account
15GET /login controllers.Auth.login 15GET /login controllers.Authentication.login
16POST /login controllers.Auth.loginSubmit 16POST /login controllers.Authentication.loginSubmit
17 17
18GET /logout controllers.Auth.logout 18GET /logout controllers.Authentication.logout
19# 19#
20#GET /signup controllers.Application.index 20#GET /signup controllers.Application.index
21#POST /signup controllers.Application.index 21#POST /signup controllers.Application.index