aboutsummaryrefslogtreecommitdiff
path: root/app/controllers
diff options
context:
space:
mode:
authorPacien TRAN-GIRARD2015-02-08 11:09:57 +0100
committerPacien TRAN-GIRARD2015-02-08 11:09:57 +0100
commit9079a273501a916262bc50e52f722a9311f12825 (patch)
treee4aeb2c9e7df0e48657c511bf3eea90643f82207 /app/controllers
parent10f857f6107fc8cebde8b39a04a07bc1945aac38 (diff)
downloadminibay-9079a273501a916262bc50e52f722a9311f12825.tar.gz
Refactor Auth
Diffstat (limited to 'app/controllers')
-rw-r--r--app/controllers/Application.scala15
-rw-r--r--app/controllers/Authentication.scala (renamed from app/controllers/Auth.scala)58
2 files changed, 45 insertions, 28 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}