Explorar el Código

WIP: impl findOrRegisterUserByLdapInfo

Yuki Takei hace 8 años
padre
commit
6d2fdb715a
Se han modificado 1 ficheros con 23 adiciones y 8 borrados
  1. 23 8
      lib/service/passport.js

+ 23 - 8
lib/service/passport.js

@@ -185,18 +185,33 @@ class PassportService {
 
   findOrRegisterUserByLdapInfo(ldapUserInfo) {
     const User = this.crowi.model('User');
-    const username = ldapUserInfo['uid'];
+    const ExternalAccount = this.crowi.model('ExternalAccount');
 
-    return User.findUserByUsername(username)
-      .then((user) => {
-        if (user != null) {
-          debug(`LdapStrategy: username ${username} is found `, user);
-          return user;
+    const accountId = ldapUserInfo['uid'];
+
+    return ExternalAccount.findOne({ providerType: 'ldap', accountId: accountId })
+      .then((account) => {
+        if (account != null) {
+          debug(`LdapStrategy: accountId '${accountId}' is found `, account);
+          return account;
         }
         else {
-          debug(`LdapStrategy: username ${username} is not found, it is going to be registered.`);
-          return User.createUser('', username, undefined, undefined, undefined);
+          debug(`LdapStrategy: accountId '${accountId}' is not found, it is going to be registered.`);
+
+          // TODO ensure to be able to select the way to determine username
+          const username = ldapUserInfo['uid'];
+
+          return User.createUser('', username, undefined, undefined, undefined)
+            .then((user) => {
+              return ExternalAccount.create({ providerType: 'ldap', accountId, user: user._id });
+            });
         }
+      })
+      .then((account) => {
+        return account.populate('user').execPopulate();
+      })
+      .then((account) => {
+        return account.user;
       });
   }