kjkmadness 2014-04-10
user: fix the problem that user can create an account with existing group name
@dfe7ceb83f1e6e39ed102076b35db6708412e403
app/controllers/UserApp.java
--- app/controllers/UserApp.java
+++ app/controllers/UserApp.java
@@ -656,15 +656,19 @@
     }
 
     /**
-     * 로그인ID 존재 여부, 로그인ID 예약어 여부
+     * check the given {@code loginId} is being used by someone else's logindId or group name,
+     * and whether {@code loginId} is a reserved word or not.
      *
-     * @param loginId 로그인ID
+     * @param name
      * @return
+     * @see User#isLoginIdExist(String)
+     * @see Organization#isNameExist(String)
+     * @see ReservedWordsValidator#isReserved(String)
      */
-    public static Result isUserExist(String loginId) {
+    public static Result isUsed(String name) {
         ObjectNode result = Json.newObject();
-        result.put("isExist", User.isLoginIdExist(loginId));
-        result.put("isReserved", ReservedWordsValidator.isReserved(loginId));
+        result.put("isExist", User.isLoginIdExist(name) || Organization.isNameExist(name));
+        result.put("isReserved", ReservedWordsValidator.isReserved(name));
         return ok(result);
     }
 
@@ -899,7 +903,8 @@
         }
 
         // 중복된 loginId로 가입할 수 없다.
-        if (User.isLoginIdExist(newUserForm.field("loginId").value())) {
+        if (User.isLoginIdExist(newUserForm.field("loginId").value())
+            || Organization.isNameExist(newUserForm.field("loginId").value())) {
             newUserForm.reject("loginId", "user.loginId.duplicate");
         }
 
app/views/user/signup.scala.html
--- app/views/user/signup.scala.html
+++ app/views/user/signup.scala.html
@@ -63,7 +63,10 @@
 <script type="text/javascript" src="@routes.Assets.at("javascripts/lib/validate.js")"></script>
 <script type="text/javascript">
     $(document).ready(function() {
-        $yobi.loadModule("user.SignUp");
+        $yobi.loadModule("user.SignUp", {
+            "sLogindIdCheckUrl" : "@routes.UserApp.isUsed("")",
+            "sEmailCheckUrl" : "@routes.UserApp.isEmailExist("")"
+        });
     });
 </script>
 }
conf/routes
--- conf/routes
+++ conf/routes
@@ -47,8 +47,8 @@
 GET            /user/editform                                                         controllers.UserApp.editUserInfoForm()
 POST           /user/edit                                                             controllers.UserApp.editUserInfo()
 POST           /user/resetPassword                                                    controllers.UserApp.resetUserPassword()
-GET            /user/isExist/:loginId                                                 controllers.UserApp.isUserExist(loginId)
-GET            /user/isEmailExist/:email                                              controllers.UserApp.isEmailExist(email)
+GET            /user/isUsed                                                           controllers.UserApp.isUsed(name:String)
+GET            /user/isEmailExist                                                     controllers.UserApp.isEmailExist(email:String)
 GET            /info/leave/:user/:project                                             controllers.UserApp.leave(user, project)
 POST           /user/email                                                            controllers.UserApp.addEmail()
 DELETE         /user/email/delete/:emailId                                            controllers.UserApp.deleteEmail(emailId:Long)
conf/test-data.yml
--- conf/test-data.yml
+++ conf/test-data.yml
@@ -457,3 +457,20 @@
                             id: 1
         notificationType:   NEW_ISSUE
         allowed:            true
+
+# Organization
+organization:
+    - !!models.Organization
+        id:              1
+        name:        labs
+        created:     2014-04-10 15:00:00
+
+# OrganizationUser
+organizationUsers:
+    - !!models.OrganizationUser
+        user:                    !!models.User
+                                     id: 2
+        organization:       !!models.Organization
+                                     id: 1
+        role:                    !!models.Role
+                                     id: 6
public/javascripts/service/yobi.user.SignUp.js
--- public/javascripts/service/yobi.user.SignUp.js
+++ public/javascripts/service/yobi.user.SignUp.js
@@ -10,7 +10,7 @@
 (function(ns){
 
     var oNS = $yobi.createNamespace(ns);
-    oNS.container[oNS.name] = function(){
+    oNS.container[oNS.name] = function(htOptions){
 
         var htVar = {};
         var htElement = {};
@@ -18,9 +18,9 @@
         /**
          * initialize
          */
-        function _init(){
+        function _init(htOptions){
             _initElement();
-            _initVar();
+            _initVar(htOptions);
 
             _initFormValidator();
             _attachEvent();
@@ -43,8 +43,10 @@
         /**
          * initialize variables
          */
-        function _initVar(){
+        function _initVar(htOptions){
             htVar.rxLoginId = /^[a-zA-Z0-9-]+([_.][a-zA-Z0-9-]+)*$/;
+            htVar.sLogindIdCheckUrl = htOptions.sLogindIdCheckUrl;
+            htVar.sEmailCheckUrl = htOptions.sEmailCheckUrl;
         }
 
         /**
@@ -72,7 +74,7 @@
             }
 
             if(sLoginId != ""){
-                doesExists($(this), "/user/isExist/");
+                doesExists($(this), htVar.sLogindIdCheckUrl);
             }
         }
 
@@ -84,7 +86,7 @@
             var welInput = $(this);
 
             if(welInput.val() !== ""){
-                doesExists(welInput, "/user/isEmailExist/");
+                doesExists(welInput, htVar.sEmailCheckUrl);
             }
         }
 
@@ -101,10 +103,6 @@
          * @param {String} sURL
          */
         function doesExists(welInput, sURL){
-            if(sURL.substr(-1) != "/"){
-                sURL += "/";
-            }
-
             $.ajax({
                 "url": sURL + welInput.val()
             }).done(function(htData){
@@ -207,6 +205,6 @@
             } catch(e){} // to avoid bootstrap bug
         }
 
-        _init();
+        _init(htOptions || {});
     };
 })("yobi.user.SignUp");
test/controllers/UserAppTest.java
--- test/controllers/UserAppTest.java
+++ test/controllers/UserAppTest.java
@@ -36,7 +36,7 @@
 
                 //When
                 Result result = callAction(
-                        controllers.routes.ref.UserApp.isUserExist("nekure"),
+                        controllers.routes.ref.UserApp.isUsed("nekure"),
                         fakeRequest().withFormUrlEncodedBody(data)
                 );  // fakeRequest doesn't need here, but remains for example
 
@@ -59,9 +59,28 @@
 
                 //When
                 Result result = callAction(
-                        controllers.routes.ref.UserApp.isUserExist("yobi"),
+                        controllers.routes.ref.UserApp.isUsed("yobi"),
                         fakeRequest().withFormUrlEncodedBody(data)
                 ); // fakeRequest doesn't need here, but remains for example
+
+                //Then
+                assertThat(status(result)).isEqualTo(OK);
+                assertThat(contentAsString(result)).contains("\"isExist\":true");
+                assertThat(contentType(result)).contains("json");
+            }
+        });
+    }
+
+    @Test
+    public void findById_alreadyExistGroupName() {
+        running(support.Helpers.makeTestApplication(), new Runnable() {
+            @Override
+            public void run() {
+                //Given
+                String loginId = "labs";
+
+                //When
+                Result result = callAction(controllers.routes.ref.UserApp.isUsed(loginId));
 
                 //Then
                 assertThat(status(result)).isEqualTo(OK);
@@ -124,6 +143,30 @@
     }
 
     @Test
+    public void newUser_AlreadyExistGroupName() {
+        running(support.Helpers.makeTestApplication(), new Runnable() {
+            @Override
+            public void run() {
+                //Given
+                Map<String, String> data = new HashMap<>();
+                data.put("loginId", "labs");
+                data.put("password", "somefakepassword");
+                data.put("email", "labs@fake.com");
+                data.put("name", "labs");
+
+                //When
+                Result result = callAction(
+                        controllers.routes.ref.UserApp.newUser(),
+                        fakeRequest().withFormUrlEncodedBody(data)
+                );
+
+                //Then
+                assertThat(status(result)).describedAs("result status should '400 bad request'").isEqualTo(BAD_REQUEST);
+            }
+        });
+    }
+
+    @Test
     public void newUser_confirmSignUpMode() {
         Map<String, String> config = support.Helpers.makeTestConfig();
         config.put("signup.require.confirm", "true");
@@ -161,7 +204,7 @@
                 data.put("loginId", "messages.js");
 
                 //When
-                Result result = callAction(controllers.routes.ref.UserApp.isUserExist("messages.js"));
+                Result result = callAction(controllers.routes.ref.UserApp.isUsed("messages.js"));
 
                 //Then
                 assertThat(status(result)).isEqualTo(OK);
test/support/Helpers.java
--- test/support/Helpers.java
+++ test/support/Helpers.java
@@ -63,7 +63,7 @@
         YamlUtil.insertDataFromYaml("test-data.yml", new String[] {
                 "users", "projects", "pullRequests", "milestones",
                 "issues", "issueComments", "postings",
-                "postingComments", "projectUsers" });
+                "postingComments", "projectUsers", "organization", "organizationUsers"});
         // Do numbering for issues and postings.
         for (Project project : Project.find.findList()) {
             List<Issue> issues = Issue.finder.where()
Add a comment
List