Przeglądaj źródła

adjust ExternalAccount class to 'isTreatUsernameMatchingAsIdentical'

Yuki Takei 8 lat temu
rodzic
commit
b8b7e1132f
1 zmienionych plików z 33 dodań i 30 usunięć
  1. 33 30
      lib/models/external-account.js

+ 33 - 30
lib/models/external-account.js

@@ -70,39 +70,42 @@ class ExternalAccount {
    */
    */
   static findOrRegister(providerType, accountId, usernameToBeRegistered) {
   static findOrRegister(providerType, accountId, usernameToBeRegistered) {
 
 
-    return this.findOne({ providerType, accountId }).then( account => {
-      // found
-      if (account != null) {
-        debug(`ExternalAccount '${accountId}' is found `, account);
-        return account;
-      }
-
-      const User = ExternalAccount.crowi.model('User');
-      const Config = ExternalAccount.crowi.model('Config');
-
-      const treatExternalUserAsLocalUser = Config.shouldTreatExternalAccountAsLocal(ExternalAccount.crowi.getConfig());
-
-      return User.find({username: usernameToBeRegistered}).then( users => {
-        // throw Exception when count is not zero
-        const maxDuplicateUserCount = treatExternalUserAsLocalUser ? 1 : 0;
-        if (users.length > maxDuplicateUserCount) {
-          throw new DuplicatedUsernameException(`username '${usernameToBeRegistered}' has already been existed`);
+    return this.findOne({ providerType, accountId })
+      .then(account => {
+        // ExternalAccount is found
+        if (account != null) {
+          debug(`ExternalAccount '${accountId}' is found `, account);
+          return account;
         }
         }
 
 
-        if (users.length === 0) {
-          debug(`ExternalAccount '${accountId}' is not found, it is going to be registered.`);
-          // create user with STATUS_ACTIVE
-          return User.createUser('', usernameToBeRegistered, undefined, undefined, undefined, User.STATUS_ACTIVE);
-        }
-
-        debug(`ExternalAccount '${accountId}' will be linked to an exisiting account`);
-        return users[0];
+        const User = ExternalAccount.crowi.model('User');
+        const Config = ExternalAccount.crowi.model('Config');
+
+        // get option
+        const isTreatUsernameMatchingAsIdentical = Config.isTreatUsernameMatchingAsIdentical(ExternalAccount.crowi.getConfig());
+
+        return User.findOne({username: usernameToBeRegistered})
+          .then(user => {
+            // when the User that have the same `username` exists
+            if (user != null) {
+              if (isTreatUsernameMatchingAsIdentical) {
+                debug(`ExternalAccount '${accountId}' will be bind to an exisiting account`);
+                return user;
+              }
+              else {
+                throw new DuplicatedUsernameException(`Userr '${usernameToBeRegistered}' has already been existed`);
+              }
+            }
+
+            // create a new User with STATUS_ACTIVE
+            debug(`ExternalAccount '${accountId}' is not found, it is going to be registered.`);
+            return User.createUser('', usernameToBeRegistered, undefined, undefined, undefined, User.STATUS_ACTIVE);
+          })
+          .then(newUser => {
+            return this.create({ providerType: 'ldap', accountId, user: newUser._id });
+          });
 
 
-      }).then( newUser => {
-        return this.create({ providerType: 'ldap', accountId, user: newUser._id });
-      })
-
-    });
+      });
   }
   }
 
 
   /**
   /**