Explorar o código

Merge pull request #349 from weseek/imprv/process-new-ldap-option-in-passport-ldap-router

imprv/process-new-ldap-option-in-passport-ldap-router
Yuki Takei %!s(int64=8) %!d(string=hai) anos
pai
achega
7856b0a36f
Modificáronse 3 ficheiros con 28 adicións e 14 borrados
  1. 15 13
      lib/models/external-account.js
  2. 12 0
      lib/routes/login-passport.js
  3. 1 1
      lib/routes/me.js

+ 15 - 13
lib/models/external-account.js

@@ -79,22 +79,12 @@ class ExternalAccount {
         }
 
         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 bound to an exisiting account`);
-                return user;
-              }
-              else {
-                throw new DuplicatedUsernameException(`User '${usernameToBeRegistered}' has already been existed`);
-              }
+              throw new DuplicatedUsernameException(`User '${usernameToBeRegistered}' has already been existed`, user);
             }
 
             // create a new User with STATUS_ACTIVE
@@ -102,12 +92,23 @@ class ExternalAccount {
             return User.createUser('', usernameToBeRegistered, undefined, undefined, undefined, User.STATUS_ACTIVE);
           })
           .then(newUser => {
-            return this.create({ providerType: 'ldap', accountId, user: newUser._id });
+            return this.associate(providerType, accountId, newUser._id);
           });
 
       });
   }
 
+  /**
+   * Create ExternalAccount document and associate to existing User
+   *
+   * @param {string} providerType
+   * @param {string} accountId
+   * @param {object} user
+   */
+  static associate(providerType, accountId, user) {
+    return this.create({ providerType, accountId, user: user._id });
+  }
+
   /**
    * find all entities with pagination
    *
@@ -142,9 +143,10 @@ class ExternalAccount {
  * @class DuplicatedUsernameException
  */
 class DuplicatedUsernameException {
-  constructor(message) {
+  constructor(message, user) {
     this.name = this.constructor.name;
     this.message = message;
+    this.user = user;
   }
 }
 

+ 12 - 0
lib/routes/login-passport.js

@@ -112,6 +112,18 @@ module.exports = function(crowi, app) {
 
       // find or register(create) user
       ExternalAccount.findOrRegister('ldap', ldapAccountId, usernameToBeRegistered)
+        .catch((err) => {
+          if (err.name != null && err.name === 'DuplicatedUsernameException') {
+            // get option
+            const isTreatUsernameMatchingAsIdentical = Config.isTreatUsernameMatchingAsIdentical(config);
+            if (isTreatUsernameMatchingAsIdentical) {
+              // associate to existing user
+              debug(`ExternalAccount '${ldapAccountId}' will be created and bound to the exisiting User account`);
+              return ExternalAccount.associate('ldap', ldapAccountId, err.user);
+            }
+          }
+          throw err;  // throw again
+        })
         .then((externalAccount) => {
           return externalAccount.getPopulatedUser();
         })

+ 1 - 1
lib/routes/me.js

@@ -268,7 +268,7 @@ module.exports = function(crowi, app) {
         const ldapAccountId = passportService.getLdapAccountIdFromReq(req);
         const user = req.user;
 
-        ExternalAccount.create({ providerType: 'ldap', accountId: ldapAccountId, user: user._id })
+        ExternalAccount.associate('ldap', ldapAccountId, user)
           .then(() => {
             return redirectWithFlash('successMessage', 'Successfully added.');
           })