aboutsummaryrefslogtreecommitdiff
path: root/app/controllers
diff options
context:
space:
mode:
authorPacien TRAN-GIRARD2015-02-08 00:17:21 +0100
committerPacien TRAN-GIRARD2015-02-08 00:17:21 +0100
commitcb902da50bd75b623a47727e62c0c69913422bfd (patch)
tree2296df3bc243176f640732fffc80ec553a371367 /app/controllers
parent3d22cb7d1ee6bdf7b484bbf001c76e81ae351dea (diff)
downloadminibay-cb902da50bd75b623a47727e62c0c69913422bfd.tar.gz
Authentication draft
Diffstat (limited to 'app/controllers')
-rw-r--r--app/controllers/Application.scala11
-rw-r--r--app/controllers/Auth.scala84
2 files changed, 89 insertions, 6 deletions
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
2 2
3import play.api._ 3import play.api._
4import play.api.mvc._ 4import play.api.mvc._
5 5import models._
6 6
7object Application extends Controller { 7object Application extends Controller {
8 8
9 def index = Action { 9 def index = Authenticate { implicit request =>
10 Redirect("/ebe").flashing("warning" -> "hey") 10 Ok(views.html.pages.ebeHomepage())
11 //Ok(views.html.pages.index("Your new application is ready."))
12 } 11 }
13 12
14 def ebe = Action { implicit request => 13 def ebe = Authenticate { implicit request =>
15 Ok(views.html.pages.ebeHomepage()) 14 Ok(views.html.pages.ebeHomepage())
16 } 15 }
17 16
18 def pepal = Action { implicit request => 17 def pepal = Authenticate { implicit request =>
19 Ok(views.html.pages.pepalHomepage()) 18 Ok(views.html.pages.pepalHomepage())
20 } 19 }
21 20
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 @@
1package controllers
2
3import controllers.Application._
4import play.api._
5import play.api.data._
6import play.api.data.Forms._
7import play.api.mvc._
8
9import models._
10
11import play.api.db.slick._
12import play.api.db.slick.Config.driver.simple._
13import play.api.Play.current
14
15import scala.concurrent.Future
16
17
18case class AuthRequest[A](account: Option[Views.Account] = None, request: Request[A]) extends WrappedRequest(request)
19
20object Authenticate extends ActionBuilder[AuthRequest] {
21
22 def invokeBlock[A](request: Request[A], block: (AuthRequest[A]) => Future[Result]) = DB.withSession { implicit session =>
23 val uuid = request.session.get(Security.username)
24 var account: Option[Views.Account] = None
25
26 if (uuid.nonEmpty) {
27 val resultSet = Views.Accounts.filter(_.userUuid === uuid).run
28 if (resultSet.nonEmpty) {
29 account = Some(resultSet.head)
30 }
31 }
32
33 block(AuthRequest(account, request))
34 }
35
36}
37
38
39case class LoginData(username: String, password: String)
40
41
42object Auth extends Controller {
43
44 val loginForm = Form(
45 mapping(
46 "username" -> nonEmptyText,
47 "password" -> nonEmptyText
48 )(LoginData.apply)(LoginData.unapply)
49 verifying("Log in failed.", fields => fields match {
50 case loginData => checkLoginData(loginData.username, loginData.password)
51 })
52 )
53
54 def checkLoginData(username: String, password: String) = DB.withSession { implicit session =>
55 Tables.Users.filter(u => u.username === username && u.userPassword === password).length.run > 0
56 }
57
58
59 def login = Action { implicit request =>
60 Ok(views.html.pages.loginForm(loginForm))
61 }
62
63 def loginSubmit = DBAction { implicit request =>
64 loginForm.bindFromRequest.fold(
65 formWithErrors => {
66 BadRequest(views.html.pages.loginForm(formWithErrors))
67 },
68 validForm => {
69 val userUuid: String = Tables.Users.filter(_.username === validForm.username).map(_.uuid).first.run
70
71 Redirect(routes.Application.index())
72 .withSession(Security.username -> userUuid)
73 .flashing(("success", "Welcome, valuable user!"))
74 }
75 )
76 }
77
78 def logout = Action { implicit request =>
79 Redirect(routes.Application.index()).withNewSession.flashing(
80 "success" -> "You are now logged out. Do not go to our competitor's website. Thanks."
81 )
82 }
83
84}