[Notice] Announcing the End of Demo Server [Read me]
authentication: Complement OAuth login
- Send mail when local user is created by OAuth Login - Show user Login ID at User setting page - Show 'Lost password link' at user's change password page
@5b5ece9e9b6de21577e0cc0b76a9777fc397f963
--- app/controllers/UserApp.java
+++ app/controllers/UserApp.java
... | ... | @@ -10,6 +10,9 @@ |
10 | 10 |
import com.avaje.ebean.Page; |
11 | 11 |
import com.avaje.ebean.annotation.Transactional; |
12 | 12 |
import com.fasterxml.jackson.databind.node.ObjectNode; |
13 |
+import com.feth.play.module.mail.Mailer; |
|
14 |
+import com.feth.play.module.mail.Mailer.Mail; |
|
15 |
+import com.feth.play.module.mail.Mailer.Mail.Body; |
|
13 | 16 |
import com.feth.play.module.pa.PlayAuthenticate; |
14 | 17 |
import controllers.annotation.AnonymousCheck; |
15 | 18 |
import models.*; |
... | ... | @@ -23,7 +26,6 @@ |
23 | 26 |
import org.apache.shiro.util.ByteSource; |
24 | 27 |
import org.joda.time.LocalDateTime; |
25 | 28 |
import play.Configuration; |
26 |
-import play.GlobalSettings; |
|
27 | 29 |
import play.Logger; |
28 | 30 |
import play.Play; |
29 | 31 |
import play.data.Form; |
... | ... | @@ -39,6 +41,7 @@ |
39 | 41 |
|
40 | 42 |
import java.util.*; |
41 | 43 |
|
44 |
+import static com.feth.play.module.mail.Mailer.getEmailName; |
|
42 | 45 |
import static play.data.Form.form; |
43 | 46 |
import static play.libs.Json.toJson; |
44 | 47 |
import static utils.HtmlUtil.defaultSanitize; |
... | ... | @@ -121,7 +124,7 @@ |
121 | 124 |
UserApp.linkWithExistedOrCreateLocalUser(); |
122 | 125 |
return redirect(redirectUrl); |
123 | 126 |
} else { |
124 |
- return ok(login.render("title.login", form(AuthInfo.class), redirectUrl)); |
|
127 |
+ return ok(views.html.user.login.render("title.login", form(AuthInfo.class), redirectUrl)); |
|
125 | 128 |
} |
126 | 129 |
} |
127 | 130 |
|
... | ... | @@ -349,15 +352,14 @@ |
349 | 352 |
} |
350 | 353 |
|
351 | 354 |
public static void createLocalUserWithOAuth(UserCredential userCredential){ |
352 |
- User user = new User(); |
|
353 | 355 |
String loginIdCandidate = userCredential.email.substring(0, userCredential.email.indexOf("@")); |
354 | 356 |
|
357 |
+ User user = new User(); |
|
355 | 358 |
user.loginId = generateLoginId(user, loginIdCandidate); |
356 | 359 |
user.name = userCredential.name; |
357 | 360 |
user.email = userCredential.email; |
358 | 361 |
|
359 |
- RandomNumberGenerator rng = new SecureRandomNumberGenerator(); |
|
360 |
- user.password = rng.nextBytes().toBase64(); // random password because created with oAuth |
|
362 |
+ user.password = (new SecureRandomNumberGenerator()).nextBytes().toBase64(); // random password because created with OAuth |
|
361 | 363 |
|
362 | 364 |
User created = createNewUser(user); |
363 | 365 |
|
... | ... | @@ -371,10 +373,42 @@ |
371 | 373 |
userCredential.loginId = created.loginId; |
372 | 374 |
userCredential.user = created; |
373 | 375 |
userCredential.update(); |
376 |
+ |
|
377 |
+ sendMailAboutUserCreationByOAuth(userCredential, created); |
|
378 |
+ } |
|
379 |
+ |
|
380 |
+ private static void sendMailAboutUserCreationByOAuth(UserCredential userCredential, User created) { |
|
381 |
+ Mail mail = new Mail("New account for Yona", getNewAccountMailBody(created), new String[] { getEmailName(userCredential.name, userCredential.email) }); |
|
382 |
+ Mailer mailer = Mailer.getCustomMailer(Configuration.root().getConfig("play-easymail")); |
|
383 |
+ mailer.sendMail(mail); |
|
384 |
+ } |
|
385 |
+ |
|
386 |
+ private static Body getNewAccountMailBody(User user){ |
|
387 |
+ String passwordResetUrl = getServeIndexPageUrl() + routes.PasswordResetApp.lostPassword(); |
|
388 |
+ String html = "ID: " + user.loginId + "<br/>\n" |
|
389 |
+ + "PW: " + user.password + "<br/>\n" |
|
390 |
+ + "Email: " + user.email + "<br/>\n<br/>\n<br/>\n" |
|
391 |
+ + "Password reset: <a href='" + passwordResetUrl + "' target='_blank'>" |
|
392 |
+ + passwordResetUrl + "</a><br/>\n<br/>\n"; |
|
393 |
+ String text = "ID: " + user.loginId + "\n" |
|
394 |
+ + "PW: " + user.password + "\n" |
|
395 |
+ + "Email: " + user.email + "\n\n\n" |
|
396 |
+ + "Password reset: " + passwordResetUrl + "\n\n"; |
|
397 |
+ return new Body(text, html); |
|
398 |
+ } |
|
399 |
+ |
|
400 |
+ private static String getServeIndexPageUrl(){ |
|
401 |
+ StringBuilder url = new StringBuilder(); |
|
402 |
+ if(request().secure()){ |
|
403 |
+ url.append("https://"); |
|
404 |
+ } else { |
|
405 |
+ url.append("http://"); |
|
406 |
+ } |
|
407 |
+ url.append(request().host()); |
|
408 |
+ return url.toString(); |
|
374 | 409 |
} |
375 | 410 |
|
376 | 411 |
private static String generateLoginId(User user, String loginIdCandidate) { |
377 |
- String loginId = null; |
|
378 | 412 |
User sameLoginIdUser = User.findByLoginId(loginIdCandidate); |
379 | 413 |
if (sameLoginIdUser.isAnonymous()) { |
380 | 414 |
return loginIdCandidate; |
--- app/views/user/edit.scala.html
+++ app/views/user/edit.scala.html
... | ... | @@ -35,6 +35,10 @@ |
35 | 35 |
|
36 | 36 |
<form id="frmBasic" method="post" action="@routes.UserApp.editUserInfo" class="pull-left"> |
37 | 37 |
<dl> |
38 |
+ <dt>@Messages("user.loginId")</dt> |
|
39 |
+ <dd class="mt10"> |
|
40 |
+ <input type="text" class="text" value="@user.loginId" readonly> |
|
41 |
+ </dd> |
|
38 | 42 |
<dt>@Messages("user.name")</dt> |
39 | 43 |
<dd class="mt10"> |
40 | 44 |
<input type="text" name="name" class="text" value="@user.name"> |
--- app/views/user/edit_password.scala.html
+++ app/views/user/edit_password.scala.html
... | ... | @@ -53,6 +53,15 @@ |
53 | 53 |
</dd> |
54 | 54 |
</dl> |
55 | 55 |
</form> |
56 |
+ <hr/> |
|
57 |
+ <div class="mt10"> |
|
58 |
+ <dl> |
|
59 |
+ <dt>@Messages("site.resetPasswordEmail.desc")</dt> |
|
60 |
+ <dd class="mt10"> |
|
61 |
+ <a href="@routes.PasswordResetApp.lostPassword" class="ybtn ybtn-fail">@Messages("site.resetPasswordEmail.title")</a> |
|
62 |
+ </dd> |
|
63 |
+ </dl> |
|
64 |
+ </div> |
|
56 | 65 |
</div> |
57 | 66 |
</div> |
58 | 67 |
<script type="text/javascript"> |
--- conf/messages
+++ conf/messages
... | ... | @@ -287,7 +287,7 @@ |
287 | 287 |
issue.state.enrolled = Status entered |
288 | 288 |
issue.state.open = Open |
289 | 289 |
issue.template = Issue Template |
290 |
-issue.template.edit = Edit |
|
290 |
+issue.template.edit = Edit |
|
291 | 291 |
issue.template.no.attachment.allow = Issue templates do not support attachments. |
292 | 292 |
issue.unvote.description = Click here if you no longer agree with this issue. |
293 | 293 |
issue.unwatch = Unsubscribe this issue |
... | ... | @@ -828,6 +828,7 @@ |
828 | 828 |
site.resetPasswordEmail.wrongUrl = Wrong url to reset password. |
829 | 829 |
site.resetPasswordEmail.mailContents = Copy the following URL and paste it to browser''s URL bar |
830 | 830 |
site.resetPasswordEmail.title = Password reset request |
831 |
+site.resetPasswordEmail.desc = If you forget current password or generated at first social login... |
|
831 | 832 |
site.search = Site search |
832 | 833 |
site.sidebar = Site management |
833 | 834 |
site.sidebar.data = Data |
--- conf/messages.ko-KR
+++ conf/messages.ko-KR
... | ... | @@ -818,6 +818,7 @@ |
818 | 818 |
site.resetPasswordEmail.wrongUrl = 비밀번호 재설정 URL이 잘못되었습니다. |
819 | 819 |
site.resetPasswordEmail.mailContents = 아래 URL을 브라우저 주소창에 붙여 넣으세요 |
820 | 820 |
site.resetPasswordEmail.title = 비밀번호 재 설정 |
821 |
+site.resetPasswordEmail.desc = 만약 현재 비밀번호가 기억나지 않거나 소셜 로그인을 통해 자동 로그인 된 경우라면.. |
|
821 | 822 |
site.search = 사이트 검색 |
822 | 823 |
site.sidebar = 사이트 관리 |
823 | 824 |
site.sidebar.data = 데이터 |
--- conf/play.plugins
+++ conf/play.plugins
... | ... | @@ -1,3 +1,4 @@ |
1 |
+1500:com.typesafe.plugin.CommonsMailerPlugin |
|
1 | 2 |
10005:service.YonaUserServicePlugin |
2 | 3 |
10010:com.feth.play.module.pa.providers.oauth2.google.GoogleAuthProvider |
3 | 4 |
10020:com.feth.play.module.pa.providers.oauth2.github.GithubAuthProvider |
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?