|
|
@@ -68,40 +68,41 @@ class ExternalAccount {
|
|
|
* @returns {Promise<ExternalAccount>}
|
|
|
* @memberof ExternalAccount
|
|
|
*/
|
|
|
- static async findOrRegister(providerType, accountId, usernameToBeRegistered) {
|
|
|
-
|
|
|
- const account = await this.findOne({ providerType, accountId })
|
|
|
- // 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());
|
|
|
-
|
|
|
- const users = await User.find({username: usernameToBeRegistered})
|
|
|
-
|
|
|
- // 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`);
|
|
|
- }
|
|
|
-
|
|
|
- let newUser;
|
|
|
- if (users.length === 0) {
|
|
|
- debug(`ExternalAccount '${accountId}' is not found, it is going to be registered.`);
|
|
|
- // create user with STATUS_ACTIVE
|
|
|
- newUser = await User.createUser('', usernameToBeRegistered, undefined, undefined, undefined, User.STATUS_ACTIVE);
|
|
|
- }
|
|
|
- else {
|
|
|
- debug(`ExternalAccount '${accountId}' will be linked to an exisiting account`);
|
|
|
- newUser = users[0];
|
|
|
- }
|
|
|
+ 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`);
|
|
|
+ }
|
|
|
+
|
|
|
+ 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];
|
|
|
+
|
|
|
+ }).then( newUser => {
|
|
|
+ return this.create({ providerType: 'ldap', accountId, user: newUser._id });
|
|
|
+ })
|
|
|
|
|
|
- return this.create({ providerType: 'ldap', accountId, user: newUser._id });
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
/**
|