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/controllers/Auth.scala | |
parent | 3d22cb7d1ee6bdf7b484bbf001c76e81ae351dea (diff) | |
download | minibay-cb902da50bd75b623a47727e62c0c69913422bfd.tar.gz |
Authentication draft
Diffstat (limited to 'app/controllers/Auth.scala')
-rw-r--r-- | app/controllers/Auth.scala | 84 |
1 files changed, 84 insertions, 0 deletions
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 | } | ||