diff options
Diffstat (limited to 'app/controllers/Profile.scala')
-rw-r--r-- | app/controllers/Profile.scala | 141 |
1 files changed, 141 insertions, 0 deletions
diff --git a/app/controllers/Profile.scala b/app/controllers/Profile.scala new file mode 100644 index 0000000..a41b3e9 --- /dev/null +++ b/app/controllers/Profile.scala | |||
@@ -0,0 +1,141 @@ | |||
1 | package controllers | ||
2 | |||
3 | import play.api._ | ||
4 | import play.api.data._ | ||
5 | import play.api.data.Forms._ | ||
6 | import play.api.mvc._ | ||
7 | |||
8 | import play.api.db.slick._ | ||
9 | import play.api.db.slick.Config.driver.simple._ | ||
10 | import play.api.Play.current | ||
11 | |||
12 | import scala.concurrent.Future | ||
13 | |||
14 | import models._ | ||
15 | |||
16 | |||
17 | case class SignupData(username: String, | ||
18 | email: String, | ||
19 | password: String, | ||
20 | passwordCheck: String, | ||
21 | firstName: String, | ||
22 | lastName: String, | ||
23 | country: String, | ||
24 | postalCode: String, | ||
25 | address: String, | ||
26 | phone: String, | ||
27 | birthdate: java.sql.Date) | ||
28 | |||
29 | case class ProfileData(email: String, | ||
30 | password: String, | ||
31 | passwordCheck: String, | ||
32 | country: String, | ||
33 | postalCode: String, | ||
34 | address: String, | ||
35 | phone: String) | ||
36 | |||
37 | |||
38 | object Profile extends Controller { | ||
39 | |||
40 | val signupForm = Form( | ||
41 | mapping( | ||
42 | "username" -> nonEmptyText(minLength = 3, maxLength = 20), | ||
43 | "email" -> email, | ||
44 | "password" -> nonEmptyText(minLength = 5, maxLength = 255), | ||
45 | "passwordCheck" -> nonEmptyText(minLength = 5, maxLength = 255), | ||
46 | "firstName" -> nonEmptyText(minLength = 1, maxLength = 255), | ||
47 | "lastName" -> nonEmptyText(minLength = 1, maxLength = 255), | ||
48 | "country" -> nonEmptyText(minLength = 2, maxLength = 2), | ||
49 | "postalCode" -> nonEmptyText(minLength = 4, maxLength = 20), | ||
50 | "address" -> nonEmptyText(minLength = 5, maxLength = 255), | ||
51 | "phone" -> nonEmptyText(minLength = 5, maxLength = 20), | ||
52 | "birthdate" -> sqlDate | ||
53 | )(SignupData.apply)(SignupData.unapply) | ||
54 | .verifying("Password mismatch", fields => fields match { | ||
55 | case profileData => checkPasswordCoherence(profileData.password, profileData.passwordCheck) | ||
56 | }) | ||
57 | .verifying("Username already registered", fields => fields match { | ||
58 | case profileData => !checkUsernameUse(profileData.username) | ||
59 | }) | ||
60 | .verifying("Email address already in use", fields => fields match { | ||
61 | case profileData => !checkEmailUse(profileData.email) | ||
62 | }) | ||
63 | ) | ||
64 | |||
65 | val profileForm = Form( | ||
66 | mapping( | ||
67 | "email" -> email, | ||
68 | "password" -> text(maxLength = 255), | ||
69 | "passwordCheck" -> text(maxLength = 255), | ||
70 | "country" -> nonEmptyText(minLength = 2, maxLength = 2), | ||
71 | "postalCode" -> nonEmptyText(minLength = 4, maxLength = 20), | ||
72 | "address" -> nonEmptyText(minLength = 5, maxLength = 255), | ||
73 | "phone" -> nonEmptyText(minLength = 5, maxLength = 20) | ||
74 | )(ProfileData.apply)(ProfileData.unapply) | ||
75 | .verifying("Password mismatch", fields => fields match { | ||
76 | case profileData => checkPasswordCoherence(profileData.password, profileData.passwordCheck) | ||
77 | }) | ||
78 | .verifying("Email address already in use", fields => fields match { | ||
79 | case profileData => !checkEmailUse(profileData.email) | ||
80 | }) | ||
81 | ) | ||
82 | |||
83 | |||
84 | def checkPasswordCoherence(p1: String, p2: String) = { | ||
85 | p1 == p2 | ||
86 | } | ||
87 | |||
88 | def checkUsernameUse(username: String) = DB.withSession { implicit session => | ||
89 | Tables.Users.filter(_.username === username).length.run > 0 | ||
90 | } | ||
91 | |||
92 | def checkEmailUse(email: String) = DB.withSession { implicit session => | ||
93 | Tables.Users.filter(_.email === email).length.run > 0 | ||
94 | } | ||
95 | |||
96 | |||
97 | def signup = Auth { implicit request => | ||
98 | if (request.account.nonEmpty) { | ||
99 | Redirect(routes.Application.index()) | ||
100 | } else { | ||
101 | Ok(views.html.pages.signupForm(signupForm)) | ||
102 | } | ||
103 | } | ||
104 | |||
105 | def signupSubmit = Auth { implicit request => | ||
106 | if (request.account.nonEmpty) { | ||
107 | Redirect(routes.Application.index()) | ||
108 | } else { | ||
109 | DB.withSession { implicit session => | ||
110 | signupForm.bindFromRequest.fold( | ||
111 | formWithErrors => { | ||
112 | BadRequest(views.html.pages.signupForm(formWithErrors)) | ||
113 | }, | ||
114 | validForm => { | ||
115 | |||
116 | val users = Tables.Users returning Tables.Users.map(_.uuid) | ||
117 | val uuid = users += Tables.User( | ||
118 | username = validForm.username, | ||
119 | email = validForm.email, | ||
120 | userPassword = validForm.password, | ||
121 | creationDate = new java.sql.Timestamp(new java.util.Date().getTime), | ||
122 | firstName = validForm.firstName, | ||
123 | lastName = validForm.lastName, | ||
124 | countryCode = validForm.country, | ||
125 | postalCode = validForm.postalCode, | ||
126 | address = validForm.address, | ||
127 | phone = validForm.phone, | ||
128 | birthdate = new java.sql.Timestamp(validForm.birthdate.getTime) | ||
129 | ) | ||
130 | |||
131 | Redirect(routes.Application.index()) | ||
132 | .withSession(Security.username -> uuid) | ||
133 | .flashing("success" -> "You are now registered. Welcome! You may now spend your money.") | ||
134 | |||
135 | } | ||
136 | ) | ||
137 | } | ||
138 | } | ||
139 | } | ||
140 | |||
141 | } | ||