[Notice] Announcing the End of Demo Server [Read me]

add change password on userinfo page
@6140bcfc2ea92c5a5f50a2e5c8c8c49dae907105
--- app/controllers/UserApp.java
+++ app/controllers/UserApp.java
... | ... | @@ -25,6 +25,8 @@ |
25 | 25 |
import java.util.ArrayList; |
26 | 26 |
import java.util.List; |
27 | 27 |
|
28 |
+import javax.persistence.Transient; |
|
29 |
+ |
|
28 | 30 |
import static play.data.Form.form; |
29 | 31 |
import static play.libs.Json.toJson; |
30 | 32 |
|
... | ... | @@ -198,6 +200,40 @@ |
198 | 200 |
return user; |
199 | 201 |
} |
200 | 202 |
|
203 |
+ public static Result resetUserPassword() { |
|
204 |
+ Form<User> userForm = form(User.class).bindFromRequest(); |
|
205 |
+ |
|
206 |
+ if(userForm.hasErrors()) { |
|
207 |
+ return badRequest(); |
|
208 |
+ } |
|
209 |
+ |
|
210 |
+ User currentUser = currentUser(); |
|
211 |
+ User user = userForm.get(); |
|
212 |
+ |
|
213 |
+ if(!isValidPassword(currentUser, user.oldPassword)) { |
|
214 |
+ Form<User> currentUserForm = new Form<User>(User.class); |
|
215 |
+ currentUserForm = currentUserForm.fill(currentUser); |
|
216 |
+ |
|
217 |
+ flash(Constants.WARNING, "user.wrongPassword.alert"); |
|
218 |
+ return badRequest(edit.render(currentUserForm, currentUser)); |
|
219 |
+ } |
|
220 |
+ |
|
221 |
+ resetPassword(currentUser, user.password); |
|
222 |
+ |
|
223 |
+ //go to login page |
|
224 |
+ session().clear(); |
|
225 |
+ response().discardCookie(TOKEN); |
|
226 |
+ |
|
227 |
+ flash(Constants.WARNING, "user.loginWithNewPassword"); |
|
228 |
+ return ok(login.render("title.login", form(User.class))); |
|
229 |
+ |
|
230 |
+ } |
|
231 |
+ |
|
232 |
+ public static boolean isValidPassword(User currentUser, String password) { |
|
233 |
+ String hashedOldPassword = hashedPassword(password, currentUser.passwordSalt); |
|
234 |
+ return currentUser.password.equals(hashedOldPassword); |
|
235 |
+ } |
|
236 |
+ |
|
201 | 237 |
public static void resetPassword(User user, String newPassword) { |
202 | 238 |
user.password = new Sha256Hash(newPassword, |
203 | 239 |
ByteSource.Util.bytes(user.passwordSalt), 1024).toBase64(); |
--- app/models/User.java
+++ app/models/User.java
... | ... | @@ -43,6 +43,9 @@ |
43 | 43 |
|
44 | 44 |
@Pattern(value = "^[a-zA-Z0-9-]+([_.][a-zA-Z0-9-]+)*$", message = "user.wrongloginId.alert") @Required |
45 | 45 |
public String loginId; |
46 |
+ |
|
47 |
+ @Transient |
|
48 |
+ public String oldPassword; |
|
46 | 49 |
public String password; |
47 | 50 |
public String passwordSalt; |
48 | 51 |
|
... | ... | @@ -175,7 +178,6 @@ |
175 | 178 |
ByteSource.Util.bytes(user.passwordSalt), 1024).toBase64(); |
176 | 179 |
user.save(); |
177 | 180 |
} |
178 |
- |
|
179 | 181 |
|
180 | 182 |
public Resource asResource() { |
181 | 183 |
return new Resource() { |
--- app/views/user/edit.scala.html
+++ app/views/user/edit.scala.html
... | ... | @@ -34,7 +34,7 @@ |
34 | 34 |
@helper.inputText(userForm("email"),'_showConstraints -> false,'_label -> null, 'class -> "span2")</p> |
35 | 35 |
</div> |
36 | 36 |
|
37 |
- <input class="btn" type="submit" value="저장"/> |
|
37 |
+ <input class="btn" type="submit" value="@Messages("button.save")"/> |
|
38 | 38 |
} |
39 | 39 |
<hr/> |
40 | 40 |
<div class="user-other-info info-box"> |
... | ... | @@ -79,11 +79,34 @@ |
79 | 79 |
</li> |
80 | 80 |
} |
81 | 81 |
</ul> |
82 |
+ <div> |
|
83 |
+ <form name="passwordReset" action="@routes.UserApp.resetUserPassword()" method="post"> |
|
84 |
+ <input type="hidden" name="loginId" value="@user.loginId" /> |
|
85 |
+ <hr /> |
|
86 |
+ <dl> |
|
87 |
+ <dt>@Messages("user.password")</dt> |
|
88 |
+ <dd><input type="password" name="oldPassword" value="" autocomplete="off" /></dd> |
|
89 |
+ <dt>@Messages("user.newPassword")</dt> |
|
90 |
+ <dd><input type="password" id="password" name="password" value="" autocomplete="off" /></dd> |
|
91 |
+ <dt>@Messages("validation.retypePassword")</dt> |
|
92 |
+ <dd><input type="password" id="retypedPassword" name="retypedPassword" value="" autocomplete="off" /></dd> |
|
93 |
+ </dl> |
|
94 |
+ <input class="btn" type="submit" value="@Messages("button.save")" /> |
|
95 |
+ </form> |
|
96 |
+ <hr /> |
|
97 |
+ </div> |
|
82 | 98 |
</div> |
83 | 99 |
</section> |
84 | 100 |
</div> |
85 | 101 |
</div> |
86 | 102 |
|
103 |
+ <script type="text/javascript" src="@getJSLink("lib/validate")"></script> |
|
104 |
+ <script type="text/javascript"> |
|
105 |
+ $(document).ready(function(){ |
|
106 |
+ $hive.loadModule("user.ResetPassword"); |
|
107 |
+ }); |
|
108 |
+ </script> |
|
109 |
+ |
|
87 | 110 |
<script src="@getJSLink("lib/jquery/jquery.form")" type="text/javascript"></script> |
88 | 111 |
<script type="text/javascript"> |
89 | 112 |
(function(){ |
--- conf/messages.en
+++ conf/messages.en
... | ... | @@ -297,6 +297,7 @@ |
297 | 297 |
user.loginId.duplicate = Already existing ID |
298 | 298 |
user.login.alert = Please, login. |
299 | 299 |
user.password = Password |
300 |
+user.newPassword = New Password |
|
300 | 301 |
user.confirmPassword = Confirm Password |
301 | 302 |
user.confirmPassword.alert = Password doesn't match the confirmation |
302 | 303 |
user.wrongPassword.alert = Wrong password! |
--- conf/messages.ko
+++ conf/messages.ko
... | ... | @@ -303,6 +303,7 @@ |
303 | 303 |
user.loginId.duplicate = 이미 존재하는 아이디입니다. |
304 | 304 |
user.login.alert = 로그인이 필요합니다. |
305 | 305 |
user.password = 비밀번호 |
306 |
+user.newPassword = 신규 비밀번호 |
|
306 | 307 |
user.confirmPassword = 비밀번호 확인 |
307 | 308 |
user.confirmPassword.alert = 입력한 두 비밀번호가 서로 일치하지 않습니다 |
308 | 309 |
user.wrongPassword.alert = 잘못된 비밀번호입니다! |
--- conf/routes
+++ conf/routes
... | ... | @@ -23,6 +23,7 @@ |
23 | 23 |
POST /users/signup controllers.UserApp.newUser() |
24 | 24 |
GET /user/editform controllers.UserApp.editUserInfoForm() |
25 | 25 |
POST /user/edit controllers.UserApp.editUserInfo() |
26 |
+POST /user/resetPassword controllers.UserApp.resetUserPassword() |
|
26 | 27 |
GET /user/isExist/:loginId controllers.UserApp.isUserExist(loginId) |
27 | 28 |
GET /user/isEmailExist/:email controllers.UserApp.isEmailExist(email) |
28 | 29 |
|
+++ public/javascripts/service/hive.user.ResetPassword.js
... | ... | @@ -0,0 +1,134 @@ |
1 | +/** | |
2 | + * @(#)hive.user.Reset.js 2013.04.25 | |
3 | + * | |
4 | + * Copyright NHN Corporation. | |
5 | + * Released under the MIT license | |
6 | + * | |
7 | + * http://hive.dev.naver.com/license | |
8 | + */ | |
9 | +(function(ns){ | |
10 | + | |
11 | + var oNS = $hive.createNamespace(ns); | |
12 | + oNS.container[oNS.name] = function(){ | |
13 | + | |
14 | + var htVar = {}; | |
15 | + var htElement = {}; | |
16 | + | |
17 | + /** | |
18 | + * initialize | |
19 | + */ | |
20 | + function _init(){ | |
21 | + _initElement(); | |
22 | + _initFormValidator(); | |
23 | + _attachEvent(); | |
24 | + } | |
25 | + | |
26 | + /** | |
27 | + * initialize elements | |
28 | + */ | |
29 | + function _initElement(){ | |
30 | + htElement.welInputOldPassword = $('#oldPassword'); | |
31 | + htElement.welInputPassword = $('#password'); | |
32 | + htElement.welInputRetypedPassword = $('#retypedPassword'); | |
33 | + | |
34 | + htElement.welForm = $("form[name=passwordReset]"); | |
35 | + } | |
36 | + | |
37 | + /** | |
38 | + * attach event | |
39 | + */ | |
40 | + function _attachEvent(){ | |
41 | + htElement.welInputOldPassword.focusout(_onBlurInputPassword); | |
42 | + htElement.welInputPassword.focusout(_onBlurInputPassword); | |
43 | + htElement.welInputRetypedPassword.focusout(_onBlurInputPassword); | |
44 | + } | |
45 | + | |
46 | + | |
47 | + /** | |
48 | + * 비밀번호 확인 입력란 벗어날 때 이벤트 핸들러 | |
49 | + * 마지막 입력란이므로 전체 폼 유효성 검사 | |
50 | + */ | |
51 | + function _onBlurInputPassword(){ | |
52 | + htVar.oValidator._validateForm(); | |
53 | + } | |
54 | + | |
55 | + /** | |
56 | + * initialize FormValidator | |
57 | + * @require validate.js | |
58 | + */ | |
59 | + function _initFormValidator(){ | |
60 | + var aRules = [ | |
61 | + {"name": 'oldPassword', "rules": 'required'}, | |
62 | + {"name": 'password', "rules": 'required|min_length[4]'}, | |
63 | + {"name": 'retypedPassword', "rules": 'required|matches[password]'} | |
64 | + ]; | |
65 | + | |
66 | + htVar.oValidator = new FormValidator('passwordReset', aRules, _onFormValidate); | |
67 | + | |
68 | + // set error message | |
69 | + htVar.oValidator.setMessage('required', Messages("validation.required")); | |
70 | + htVar.oValidator.setMessage('min_length', Messages("validation.tooShortPassword")); | |
71 | + htVar.oValidator.setMessage('matches', Messages("validation.passwordMismatch")); | |
72 | + } | |
73 | + | |
74 | + /** | |
75 | + * on validate form | |
76 | + * @param {Array} aErrors | |
77 | + */ | |
78 | + function _onFormValidate(aErrors){ | |
79 | + _clearTooltips(); | |
80 | + // to avoid bootstrap bug | |
81 | + if (aErrors.length <= 0) { | |
82 | + return _clearTooltips(); | |
83 | + } | |
84 | + | |
85 | + var welTarget; | |
86 | + aErrors.forEach(function(htError){ | |
87 | + welTarget = htElement.welForm.find("input[name=" + htError.name + "]"); | |
88 | + if(welTarget){ | |
89 | + showErrorMessage(welTarget, htError.message); | |
90 | + } | |
91 | + }); | |
92 | + } | |
93 | + | |
94 | + /** | |
95 | + * 폼 영역에 있는 jquery.tooltip 모두 제거하는 함수 | |
96 | + */ | |
97 | + function _clearTooltips(){ | |
98 | + try { | |
99 | + htElement.welForm.find("input").each(function(i, v){ | |
100 | + $(v).tooltip("destroy"); | |
101 | + }); | |
102 | + } catch(e){} | |
103 | + } | |
104 | + | |
105 | + /** | |
106 | + * Bootstrap toolTip function has some limitation. | |
107 | + * In this case, toolTip doesn't provide easy way to change title and contents. | |
108 | + * So, unfortunately I had to change data value in directly. | |
109 | + * @param {Wrapped Element} welInput | |
110 | + * @param {String} sMessage | |
111 | + */ | |
112 | + function showErrorMessage(welInput, sMessage){ | |
113 | + welInput.tooltip({"trigger": "manual", "placement": "right"}); | |
114 | + | |
115 | + var oToolTip = welInput.data('tooltip'); | |
116 | + oToolTip.options.placement = 'right'; | |
117 | + oToolTip.options.trigger = 'manual'; | |
118 | + oToolTip.options.title = sMessage; | |
119 | + oToolTip.options.content = sMessage; | |
120 | + | |
121 | + welInput.tooltip('show'); | |
122 | + } | |
123 | + | |
124 | + function hideErrorMessage(welInput){ | |
125 | + welInput.tooltip("hide"); | |
126 | + | |
127 | + try{ | |
128 | + welInput.tooltip("destroy"); | |
129 | + } catch(e){} // to avoid bootstrap bug | |
130 | + } | |
131 | + | |
132 | + _init(); | |
133 | + }; | |
134 | +})("hive.user.ResetPassword");(No newline at end of file) |
Add a comment
Delete comment
Once you delete this comment, you won't be able to recover it. Are you sure you want to delete this comment?