diff options
author | Pacien TRAN-GIRARD | 2015-02-08 00:17:21 +0100 |
---|---|---|
committer | Pacien TRAN-GIRARD | 2015-02-08 00:17:21 +0100 |
commit | cb902da50bd75b623a47727e62c0c69913422bfd (patch) | |
tree | 2296df3bc243176f640732fffc80ec553a371367 /app | |
parent | 3d22cb7d1ee6bdf7b484bbf001c76e81ae351dea (diff) | |
download | minibay-cb902da50bd75b623a47727e62c0c69913422bfd.tar.gz |
Authentication draft
Diffstat (limited to 'app')
16 files changed, 197 insertions, 42 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 | ||
3 | import play.api._ | 3 | import play.api._ |
4 | import play.api.mvc._ | 4 | import play.api.mvc._ |
5 | 5 | import models._ | |
6 | 6 | ||
7 | object Application extends Controller { | 7 | object 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 @@ | |||
1 | package controllers | ||
2 | |||
3 | import controllers.Application._ | ||
4 | import play.api._ | ||
5 | import play.api.data._ | ||
6 | import play.api.data.Forms._ | ||
7 | import play.api.mvc._ | ||
8 | |||
9 | import models._ | ||
10 | |||
11 | import play.api.db.slick._ | ||
12 | import play.api.db.slick.Config.driver.simple._ | ||
13 | import play.api.Play.current | ||
14 | |||
15 | import scala.concurrent.Future | ||
16 | |||
17 | |||
18 | case class AuthRequest[A](account: Option[Views.Account] = None, request: Request[A]) extends WrappedRequest(request) | ||
19 | |||
20 | object 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 | |||
39 | case class LoginData(username: String, password: String) | ||
40 | |||
41 | |||
42 | object 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 | ||
4 | class="pure-input-1 @if(field.hasErrors) {input-invalid}" | ||
5 | type="@inputType" | ||
6 | placeholder="@label" | ||
7 | name="@field.name" | ||
8 | id="@field.name" | ||
9 | value="@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 | ||
4 | class="pure-input-1 @if(field.hasErrors) {input-invalid}" | ||
5 | placeholder="@label" | ||
6 | name="@field.name" | ||
7 | id="@field.name" | ||
8 | >@field.value</textarea> | ||
diff --git a/app/views/pages/ebeHomepage.scala.html b/app/views/pages/ebeHomepage.scala.html index da1fb08..d4fe80a 100644 --- a/app/views/pages/ebeHomepage.scala.html +++ b/app/views/pages/ebeHomepage.scala.html | |||
@@ -1,6 +1,6 @@ | |||
1 | @()(implicit flash: Flash) | 1 | @()(implicit request : AuthRequest[AnyContent], flash: Flash, token: play.filters.csrf.CSRF.Token) |
2 | 2 | ||
3 | @templates.ebe("eBé") { | 3 | @templates.ebe("eBé")(request.account) { |
4 |