aboutsummaryrefslogtreecommitdiff
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
parent3d22cb7d1ee6bdf7b484bbf001c76e81ae351dea (diff)
downloadminibay-cb902da50bd75b623a47727e62c0c69913422bfd.tar.gz
Authentication draft
-rw-r--r--app/controllers/Application.scala11
-rw-r--r--app/controllers/Auth.scala84
-rw-r--r--app/views/fragments/accountInfos.scala.html46
-rw-r--r--app/views/fragments/branding/ebeLogo.scala.html (renamed from app/views/fragments/ebeLogo.scala.html)0
-rw-r--r--app/views/fragments/branding/ebeSlogan.scala.html (renamed from app/views/fragments/ebeSlogan.scala.html)0
-rw-r--r--app/views/fragments/branding/pepalLogo.scala.html (renamed from app/views/fragments/pepalLogo.scala.html)0
-rw-r--r--app/views/fragments/branding/pepalSlogan.scala.html (renamed from app/views/fragments/pepalSlogan.scala.html)0
-rw-r--r--app/views/fragments/forms/globalErrors.scala.html7
-rw-r--r--app/views/fragments/forms/inputField.scala.html10
-rw-r--r--app/views/fragments/forms/textarea.scala.html8
-rw-r--r--app/views/pages/ebeHomepage.scala.html10
-rw-r--r--app/views/pages/loginForm.scala.html27
-rw-r--r--app/views/pages/pepalHomepage.scala.html8
-rw-r--r--app/views/templates/ebe.scala.html4
-rw-r--r--app/views/templates/main.scala.html20
-rw-r--r--app/views/templates/pepal.scala.html4
-rw-r--r--conf/application.conf4
-rw-r--r--conf/routes22
-rw-r--r--project/activator-sbt-echo-shim.sbt1
-rw-r--r--project/activator-sbt-eclipse-shim.sbt1
-rw-r--r--project/activator-sbt-idea-shim.sbt1
-rw-r--r--public/stylesheets/main.css12
22 files changed, 225 insertions, 55 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}
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 @@
1<a class="pure-button" href="#"> 1@(accountData: Option[models.Views.Account] = None)
2 <i class="fa fa-sign-out fa-lg"></i> 2
3 Logout 3@accountData match {
4</a> 4 case Some(account) => {
5 5 <a class="pure-button" href="@routes.Auth.logout()">
6<a class="pure-button" href="#"> 6 <i class="fa fa-sign-out fa-lg"></i>
7 <i class="fa fa-money fa-lg"></i> 7 Logout
8 -400 000 € 8 </a>
9</a> 9
10 10 <a class="pure-button" href="#">
11<a class="pure-button" href="#"> 11 <i class="fa fa-money fa-lg"></i>
12 <i class="fa fa-user fa-lg"></i> 12 @account.equity €
13 Mr. Kornada 13 </a>
14</a> 14
15 <a class="pure-button" href="#">
16 <i class="fa fa-user fa-lg"></i>
17 @account.username
18 </a>
19 }
20
21 case None => {
22 <a class="pure-button" href="@routes.Auth.logout()">
23 <i class="fa fa-pencil-square-o fa-lg"></i>
24 Sign up
25 </a>
26
27 <a class="pure-button" href="@routes.Auth.login()">
28 <i class="fa fa-sign-in fa-lg"></i>
29 Log in
30 </a>
31 }
32}
diff --git a/app/views/fragments/ebeLogo.scala.html b/app/views/fragments/branding/ebeLogo.scala.html
index f2b552a..f2b552a 100644
--- a/app/views/fragments/ebeLogo.scala.html
+++ b/app/views/fragments/branding/ebeLogo.scala.html
diff --git a/app/views/fragments/ebeSlogan.scala.html b/app/views/fragments/branding/ebeSlogan.scala.html
index c0b5332..c0b5332 100644
--- a/app/views/fragments/ebeSlogan.scala.html
+++ b/app/views/fragments/branding/ebeSlogan.scala.html
diff --git a/app/views/fragments/pepalLogo.scala.html b/app/views/fragments/branding/pepalLogo.scala.html
index a4b75ae..a4b75ae 100644
--- a/app/views/fragments/pepalLogo.scala.html
+++ b/app/views/fragments/branding/pepalLogo.scala.html
diff --git a/app/views/fragments/pepalSlogan.scala.html b/app/views/fragments/branding/pepalSlogan.scala.html
index a6d6e69..a6d6e69 100644
--- a/app/views/fragments/pepalSlogan.scala.html
+++ b/app/views/fragments/branding/pepalSlogan.scala.html
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 @@
1@(form: Form[_ <: Product])
2
3<div class="flash-message">
4 @for(error <- form.globalErrors) {
5 <div class="error">@error.message</div>
6 }
7</div>
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 @@
1@(field: Field, inputType: String, label: String)
2
3<input
4class="pure-input-1 @if(field.hasErrors) {input-invalid}"
5type="@inputType"
6placeholder="@label"
7name="@field.name"
8id="@field.name"
9value="@field.value"
10>
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 @@
1@(field: Field, label: String)
2
3<textarea
4class="pure-input-1 @if(field.hasErrors) {input-invalid}"
5placeholder="@label"
6name="@field.name"
7id="@field.name"
8>@field.value</textarea>