Przeglądaj źródła

Migrate from original user when ldap auth succeeds

Otsuki Hitoshi 8 lat temu
rodzic
commit
de382d0948
1 zmienionych plików z 12 dodań i 7 usunięć
  1. 12 7
      lib/models/external-account.js

+ 12 - 7
lib/models/external-account.js

@@ -79,19 +79,24 @@ class ExternalAccount {
         }
         }
         // not found
         // not found
         else {
         else {
-          debug(`ExternalAccount '${accountId}' is not found, it is going to be registered.`);
-
           const User = ExternalAccount.crowi.model('User');
           const User = ExternalAccount.crowi.model('User');
 
 
-          return User.count({username: usernameToBeRegistered})
-            .then((count) => {
+          return User.find({username: usernameToBeRegistered})
+            .then(users => {
               // throw Exception when count is not zero
               // throw Exception when count is not zero
-              if (count > 0) {
+              if (users.length > 1) {
                 throw new DuplicatedUsernameException(`username '${usernameToBeRegistered}' has already been existed`);
                 throw new DuplicatedUsernameException(`username '${usernameToBeRegistered}' has already been existed`);
               }
               }
+              else 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);
+              }
+              else {
+                debug(`ExternalAccount '${accountId}' will be linked to an exisiting account`);
+                return users[0];
+              }
 
 
-              // create user with STATUS_ACTIVE
-              return User.createUser('', usernameToBeRegistered, undefined, undefined, undefined, User.STATUS_ACTIVE);
             })
             })
             .then((user) => {
             .then((user) => {
               return this.create({ providerType: 'ldap', accountId, user: user._id });
               return this.create({ providerType: 'ldap', accountId, user: user._id });