diff options
author | Pacien TRAN-GIRARD | 2015-02-08 11:09:57 +0100 |
---|---|---|
committer | Pacien TRAN-GIRARD | 2015-02-08 11:09:57 +0100 |
commit | 9079a273501a916262bc50e52f722a9311f12825 (patch) | |
tree | e4aeb2c9e7df0e48657c511bf3eea90643f82207 /app | |
parent | 10f857f6107fc8cebde8b39a04a07bc1945aac38 (diff) | |
download | minibay-9079a273501a916262bc50e52f722a9311f12825.tar.gz |
Refactor Auth
Diffstat (limited to 'app')
-rw-r--r-- | app/controllers/Application.scala | 15 | ||||
-rw-r--r-- | app/controllers/Authentication.scala (renamed from app/controllers/Auth.scala) | 58 | ||||
-rw-r--r-- | app/views/fragments/accountInfos.scala.html | 6 | ||||
-rw-r--r-- | app/views/pages/loginForm.scala.html | 2 |
4 files changed, 49 insertions, 32 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 @@ | |||
1 | package controllers | 1 | package controllers |
2 | 2 | ||
3 | import play.api._ | 3 | import play.api._ |
4 | import play.api.data._ | ||
5 | import play.api.data.Forms._ | ||
4 | import play.api.mvc._ | 6 | import play.api.mvc._ |
7 | |||
8 | import play.api.db.slick._ | ||
9 | import play.api.db.slick.Config.driver.simple._ | ||
10 | import play.api.Play.current | ||
11 | |||
12 | import scala.concurrent.Future | ||
13 | |||
5 | import models._ | 14 | import models._ |
6 | 15 | ||
7 | object Application extends Controller { | 16 | object 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 @@ | |||
1 | package controllers | 1 | package controllers |
2 | 2 | ||
3 | import controllers.Application._ | ||
4 | import play.api._ | 3 | import play.api._ |
5 | import play.api.data._ | 4 | import play.api.data._ |
6 | import play.api.data.Forms._ | 5 | import play.api.data.Forms._ |
7 | import play.api.mvc._ | 6 | import play.api.mvc._ |
8 | 7 | ||
9 | import models._ | ||
10 | |||
11 | import play.api.db.slick._ | 8 | import play.api.db.slick._ |
12 | import play.api.db.slick.Config.driver.simple._ | 9 | import play.api.db.slick.Config.driver.simple._ |
13 | import play.api.Play.current | 10 | import play.api.Play.current |
14 | 11 | ||
15 | import scala.concurrent.Future | 12 | import scala.concurrent.Future |
16 | 13 | ||
14 | import models._ | ||
15 | |||
17 | 16 | ||
18 | case class AuthRequest[A](account: Option[Views.Account] = None, request: Request[A]) extends WrappedRequest(request) | 17 | case class AuthRequest[A](account: Option[Views.Account] = None, request: Request[A]) extends WrappedRequest(request) |
19 | 18 | ||
20 | object Authenticate extends ActionBuilder[AuthRequest] { | 19 | object 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 | ||
39 | case class LoginData(username: String, password: String) | 38 | case class LoginData(username: String, password: String) |
40 | 39 | ||
41 | 40 | object Authentication extends Controller { | |
42 | object 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 | ||