김창근 2014-07-28
Merge branch 'login-email-feature' of dlab/hive
from pull request 1138
@743b746fc5c6869e84abceb64d9a5a9970d71124
app/controllers/PasswordResetApp.java
--- app/controllers/PasswordResetApp.java
+++ app/controllers/PasswordResetApp.java
@@ -21,6 +21,7 @@
 package controllers;
 
 import info.schleichardt.play2.mailplugin.Mailer;
+import models.AuthInfo;
 import utils.PasswordReset;
 import models.User;
 import org.apache.commons.mail.EmailException;
@@ -111,6 +112,6 @@
             Logger.debug("Not a valid request!");
         }
         flash(Constants.WARNING, "user.loginWithNewPassword");
-        return ok(login.render("title.login", form(User.class), null));
+        return ok(login.render("title.login", form(AuthInfo.class), null));
     }
 }
app/controllers/UserApp.java
--- app/controllers/UserApp.java
+++ app/controllers/UserApp.java
@@ -116,7 +116,7 @@
         if(StringUtils.isEmpty(redirectUrl) && !StringUtils.equals(loginFormUrl, referer)) {
             redirectUrl = request().getHeader("Referer");
         }
-        return ok(login.render("title.login", form(User.class), redirectUrl));
+        return ok(login.render("title.login", form(AuthInfo.class), redirectUrl));
     }
 
     public static Result logout() {
@@ -156,14 +156,14 @@
      *
      * @return
      */
-    private static Result loginByFormRequest(){
-        Form<User> userForm = form(User.class).bindFromRequest();
+    private static Result loginByFormRequest() {
+        Form<AuthInfo> authInfoForm = form(AuthInfo.class).bindFromRequest();
 
-        if(userForm.hasErrors()) {
-            return badRequest(login.render("title.login", userForm, null));
+        if(authInfoForm.hasErrors()) {
+            return badRequest(login.render("title.login", authInfoForm, null));
         }
 
-        User sourceUser = form(User.class).bindFromRequest().get();
+        User sourceUser = User.findByLoginKey(authInfoForm.get().loginIdOrEmail);
 
         if (isUseSignUpConfirm()) {
             if (User.findByLoginId(sourceUser.loginId).state == UserState.LOCKED) {
@@ -177,7 +177,7 @@
             return redirect(getLoginFormURLWithRedirectURL());
         }
 
-        User authenticate = authenticateWithPlainPassword(sourceUser.loginId, sourceUser.password);
+        User authenticate = authenticateWithPlainPassword(sourceUser.loginId, authInfoForm.get().password);
 
         if (!authenticate.isAnonymous()) {
             addUserInfoToSession(authenticate);
@@ -214,14 +214,14 @@
      *
      * @return
      */
-    private static Result loginByAjaxRequest(){
-        Form<User> userForm = form(User.class).bindFromRequest();
+    private static Result loginByAjaxRequest() {
+        Form<AuthInfo> authInfoForm = form(AuthInfo.class).bindFromRequest();
 
-        if(userForm.hasErrors()) {
+        if(authInfoForm.hasErrors()) {
             return badRequest(getObjectNodeWithMessage("validation.required"));
         }
 
-        User sourceUser = form(User.class).bindFromRequest().get();
+        User sourceUser = User.findByLoginKey(authInfoForm.get().loginIdOrEmail);
 
         if (isUseSignUpConfirm()) {
             if (User.findByLoginId(sourceUser.loginId).state == UserState.LOCKED) {
@@ -233,7 +233,7 @@
             return notFound(getObjectNodeWithMessage("user.deleted"));
         }
 
-        User authenticate = authenticateWithPlainPassword(sourceUser.loginId, sourceUser.password);
+        User authenticate = authenticateWithPlainPassword(sourceUser.loginId, authInfoForm.get().password);
 
         if (!authenticate.isAnonymous()) {
             addUserInfoToSession(authenticate);
 
app/models/AuthInfo.java (added)
+++ app/models/AuthInfo.java
@@ -0,0 +1,31 @@
+/**
+ * Yobi, Project Hosting SW
+ *
+ * Copyright 2014 NAVER Corp.
+ * http://yobi.io
+ *
+ * @Author Changgun Kim
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package models;
+
+import play.data.validation.Constraints;
+import utils.ReservedWordsValidator;
+
+public class AuthInfo {
+    @Constraints.Required
+    public String loginIdOrEmail;
+    @Constraints.Required
+    public String password;
+}
app/models/User.java
--- app/models/User.java
+++ app/models/User.java
@@ -247,6 +247,16 @@
         return anonymous;
     }
 
+    public static User findByLoginKey(String loginIdOrEmail) {
+        User user = find.where().ieq("loginId", loginIdOrEmail).findUnique();
+
+        if (user == null) {
+            user = find.where().eq("email", loginIdOrEmail).findUnique();
+        }
+
+        return (user == null) ? anonymous : user;
+    }
+
     /**
      * check is existed user
      *
app/views/common/loginDialog.scala.html
--- app/views/common/loginDialog.scala.html
+++ app/views/common/loginDialog.scala.html
@@ -28,10 +28,10 @@
     <form action="@routes.UserApp.login()" method="post" class="frm-wrap login-form-wrap">
       <dl>
         <dt>
-          <label for="loginIdD">@Messages("user.loginId")</label>
+          <label for="loginIdOrEmailD">@Messages("user.login.key")</label>
         </dt>
         <dd>
-          <input id="loginIdD" name="loginId" type="text" class="text email" autocomplete="off">
+          <input id="loginIdOrEmailD" name="loginIdOrEmail" type="text" class="text email" autocomplete="off">
         </dd>
 
         <dt>
app/views/user/login.scala.html
--- app/views/user/login.scala.html
+++ app/views/user/login.scala.html
@@ -18,7 +18,7 @@
 * See the License for the specific language governing permissions and
 * limitations under the License.
 **@
-@(message:String, userForm:Form[User], redirectUrl:String)
+@(message:String, authInfoForm:Form[AuthInfo], redirectUrl:String)
 
 @siteLayout(message, utils.MenuType.NONE) {
 <div class="page full">
@@ -33,10 +33,10 @@
       <input type="hidden" name="redirectUrl" value="@redirectUrl" />
       <dl>
         <dt>
-          <label for="loginId">@Messages("user.loginId")</label>
+          <label for="loginIdOrEmailD">@Messages("user.login.key")</label>
         </dt>
         <dd>
-          <input id="loginId" name="loginId" type="text" class="text email" autocomplete="off">
+          <input id="loginIdOrEmailD" name="loginIdOrEmail" type="text" class="text email" autocomplete="off">
         </dd>
 
         <dt class="mt10">
@@ -66,7 +66,7 @@
 </div>
 <script type="text/javascript">
     $(function() {
-        $('#loginId').focus();
+        $('#loginIdOrEmail').focus();
     });
 </script>
 }
conf/messages
--- conf/messages
+++ conf/messages
@@ -755,8 +755,9 @@
 user.isAlreadySignupUser = Already signed up?
 user.isLocked = Is the user account locked out?
 user.locked = This user account is locked
+user.login.key = Login ID or E-mail
 user.login.alert = Please log in.
-user.login.failed = Your log in ID or password is not valid.
+user.login.failed = Your log in ID, E-mail or password is not valid.
 user.loginId = Login ID
 user.loginId.duplicate = Same log in ID already exists.
 user.loginWithNewPassword = Please log in with new password!
conf/messages.ko
--- conf/messages.ko
+++ conf/messages.ko
@@ -755,8 +755,9 @@
 user.isAlreadySignupUser = 이미 가입하셨나요?
 user.isLocked = 잠김여부
 user.locked = 잠긴 사용자 계정입니다.
+user.login.key = 아이디 또는 이메일
 user.login.alert = 로그인이 필요합니다.
-user.login.failed = 아이디 또는 비밀번호가 잘못되었습니다.
+user.login.failed = 아이디, 이메일 또는 비밀번호가 잘못되었습니다.
 user.loginId = 아이디
 user.loginId.duplicate = 이미 존재하는 아이디입니다.
 user.loginWithNewPassword = 새로 설정한 비밀번호로 로그인 하세요
public/javascripts/common/yobi.LoginDialog.js
--- public/javascripts/common/yobi.LoginDialog.js
+++ public/javascripts/common/yobi.LoginDialog.js
@@ -36,7 +36,7 @@
     function _initElement(){
         htElement.welDialog = $("#loginDialog");
         htElement.welForm = htElement.welDialog.find("form");
-        htElement.welInputId = htElement.welDialog.find("input[name='loginId']");
+        htElement.welInputId = htElement.welDialog.find("input[name='loginIdOrEmail']");
         htElement.welInputPw = htElement.welDialog.find("input[name='password']");
         htElement.welLoginError = htElement.welDialog.find(".error");
         htElement.welLoginErrorMsg = htElement.welLoginError.find(".error-message");
@@ -73,7 +73,7 @@
             "type": "post",
             "dataType": "json",
             "data": {
-                "loginId" : htElement.welInputId.val(),
+                "loginIdOrEmail" : htElement.welInputId.val(),
                 "password": htElement.welInputPw.val()
             }
         }).done(function(){
Add a comment
List