doortts doortts 2017-02-02
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
+++ app/controllers/UserApp.java
@@ -10,6 +10,9 @@
 import com.avaje.ebean.Page;
 import com.avaje.ebean.annotation.Transactional;
 import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.feth.play.module.mail.Mailer;
+import com.feth.play.module.mail.Mailer.Mail;
+import com.feth.play.module.mail.Mailer.Mail.Body;
 import com.feth.play.module.pa.PlayAuthenticate;
 import controllers.annotation.AnonymousCheck;
 import models.*;
@@ -23,7 +26,6 @@
 import org.apache.shiro.util.ByteSource;
 import org.joda.time.LocalDateTime;
 import play.Configuration;
-import play.GlobalSettings;
 import play.Logger;
 import play.Play;
 import play.data.Form;
@@ -39,6 +41,7 @@
 
 import java.util.*;
 
+import static com.feth.play.module.mail.Mailer.getEmailName;
 import static play.data.Form.form;
 import static play.libs.Json.toJson;
 import static utils.HtmlUtil.defaultSanitize;
@@ -121,7 +124,7 @@
             UserApp.linkWithExistedOrCreateLocalUser();
             return redirect(redirectUrl);
         } else {
-            return ok(login.render("title.login", form(AuthInfo.class), redirectUrl));
+            return ok(views.html.user.login.render("title.login", form(AuthInfo.class), redirectUrl));
         }
     }
 
@@ -349,15 +352,14 @@
     }
 
     public static void createLocalUserWithOAuth(UserCredential userCredential){
-        User user = new User();
         String loginIdCandidate = userCredential.email.substring(0, userCredential.email.indexOf("@"));
 
+        User user = new User();
         user.loginId = generateLoginId(user, loginIdCandidate);
         user.name = userCredential.name;
         user.email = userCredential.email;
 
-        RandomNumberGenerator rng = new SecureRandomNumberGenerator();
-        user.password = rng.nextBytes().toBase64();  // random password because created with oAuth
+        user.password = (new SecureRandomNumberGenerator()).nextBytes().toBase64();  // random password because created with OAuth
 
         User created = createNewUser(user);
 
@@ -371,10 +373,42 @@
         userCredential.loginId = created.loginId;
         userCredential.user = created;
         userCredential.update();
+
+        sendMailAboutUserCreationByOAuth(userCredential, created);
+    }
+
+    private static void sendMailAboutUserCreationByOAuth(UserCredential userCredential, User created) {
+        Mail mail = new Mail("New account for Yona", getNewAccountMailBody(created), new String[] { getEmailName(userCredential.name, userCredential.email) });
+        Mailer mailer = Mailer.getCustomMailer(Configuration.root().getConfig("play-easymail"));
+        mailer.sendMail(mail);
+    }
+
+    private static Body getNewAccountMailBody(User user){
+        String passwordResetUrl = getServeIndexPageUrl() + routes.PasswordResetApp.lostPassword();
+        String html =  "ID: " + user.loginId + "<br/>\n"
+                + "PW: " + user.password + "<br/>\n"
+                + "Email: " + user.email + "<br/>\n<br/>\n<br/>\n"
+                + "Password reset: <a href='" + passwordResetUrl + "' target='_blank'>"
+                + passwordResetUrl + "</a><br/>\n<br/>\n";
+        String text =  "ID: " + user.loginId + "\n"
+                + "PW: " + user.password + "\n"
+                + "Email: " + user.email + "\n\n\n"
+                + "Password reset: " + passwordResetUrl + "\n\n";
+        return new Body(text, html);
+    }
+
+    private static String getServeIndexPageUrl(){
+        StringBuilder url = new StringBuilder();
+        if(request().secure()){
+            url.append("https://");
+        } else {
+            url.append("http://");
+        }
+        url.append(request().host());
+        return url.toString();
     }
 
     private static String generateLoginId(User user, String loginIdCandidate) {
-        String loginId = null;
         User sameLoginIdUser = User.findByLoginId(loginIdCandidate);
         if (sameLoginIdUser.isAnonymous()) {
             return loginIdCandidate;
app/views/user/edit.scala.html
--- app/views/user/edit.scala.html
+++ app/views/user/edit.scala.html
@@ -35,6 +35,10 @@
 
         <form id="frmBasic" method="post" action="@routes.UserApp.editUserInfo" class="pull-left">
         <dl>
+            <dt>@Messages("user.loginId")</dt>
+            <dd class="mt10">
+                <input type="text" class="text" value="@user.loginId" readonly>
+            </dd>
           <dt>@Messages("user.name")</dt>
           <dd class="mt10">
             <input type="text" name="name" class="text" value="@user.name">
app/views/user/edit_password.scala.html
--- app/views/user/edit_password.scala.html
+++ app/views/user/edit_password.scala.html
@@ -53,6 +53,15 @@
         </dd>
       </dl>
     </form>
+    <hr/>
+    <div class="mt10">
+        <dl>
+          <dt>@Messages("site.resetPasswordEmail.desc")</dt>
+          <dd class="mt10">
+            <a href="@routes.PasswordResetApp.lostPassword" class="ybtn ybtn-fail">@Messages("site.resetPasswordEmail.title")</a>
+          </dd>
+        </dl>
+      </div>
   </div>
 </div>
 <script type="text/javascript">
conf/messages
--- conf/messages
+++ conf/messages
@@ -287,7 +287,7 @@
 issue.state.enrolled = Status entered
 issue.state.open = Open
 issue.template = Issue Template
-issue.template.edit = Edit
+issue.template.edit = Edit 
 issue.template.no.attachment.allow = Issue templates do not support attachments.
 issue.unvote.description = Click here if you no longer agree with this issue.
 issue.unwatch = Unsubscribe this issue
@@ -828,6 +828,7 @@
 site.resetPasswordEmail.wrongUrl = Wrong url to reset password.
 site.resetPasswordEmail.mailContents = Copy the following URL and paste it to browser''s URL bar
 site.resetPasswordEmail.title = Password reset request
+site.resetPasswordEmail.desc = If you forget current password or generated at first social login...
 site.search = Site search
 site.sidebar = Site management
 site.sidebar.data = Data
conf/messages.ko-KR
--- conf/messages.ko-KR
+++ conf/messages.ko-KR
@@ -818,6 +818,7 @@
 site.resetPasswordEmail.wrongUrl = 비밀번호 재설정 URL이 잘못되었습니다.
 site.resetPasswordEmail.mailContents = 아래 URL을 브라우저 주소창에 붙여 넣으세요
 site.resetPasswordEmail.title = 비밀번호 재 설정
+site.resetPasswordEmail.desc = 만약 현재 비밀번호가 기억나지 않거나 소셜 로그인을 통해 자동 로그인 된 경우라면..
 site.search = 사이트 검색
 site.sidebar = 사이트 관리
 site.sidebar.data = 데이터
conf/play.plugins
--- conf/play.plugins
+++ conf/play.plugins
@@ -1,3 +1,4 @@
+1500:com.typesafe.plugin.CommonsMailerPlugin
 10005:service.YonaUserServicePlugin
 10010:com.feth.play.module.pa.providers.oauth2.google.GoogleAuthProvider
 10020:com.feth.play.module.pa.providers.oauth2.github.GithubAuthProvider
Add a comment
List