|
|
@@ -68,42 +68,38 @@ class ExternalAccount {
|
|
|
* @returns {Promise<ExternalAccount>}
|
|
|
* @memberof ExternalAccount
|
|
|
*/
|
|
|
- static findOrRegister(providerType, accountId, usernameToBeRegistered) {
|
|
|
+ static async findOrRegister(providerType, accountId, usernameToBeRegistered) {
|
|
|
|
|
|
- return this.findOne({ providerType, accountId })
|
|
|
- .then((account) => {
|
|
|
- // found
|
|
|
- if (account != null) {
|
|
|
- debug(`ExternalAccount '${accountId}' is found `, account);
|
|
|
- return account;
|
|
|
- }
|
|
|
- // not found
|
|
|
- else {
|
|
|
- const User = ExternalAccount.crowi.model('User');
|
|
|
-
|
|
|
- return User.find({username: usernameToBeRegistered})
|
|
|
- .then(users => {
|
|
|
- // throw Exception when count is not zero
|
|
|
- if (users.length > 1) {
|
|
|
- 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];
|
|
|
- }
|
|
|
-
|
|
|
- })
|
|
|
- .then((user) => {
|
|
|
- return this.create({ providerType: 'ldap', accountId, user: user._id });
|
|
|
- });
|
|
|
- }
|
|
|
- });
|
|
|
+ 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 treatExternalUserAsLocalUser = false; // TODO: fetch from config model
|
|
|
+
|
|
|
+ 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];
|
|
|
+ }
|
|
|
|
|
|
+ return this.create({ providerType: 'ldap', accountId, user: newUser._id });
|
|
|
}
|
|
|
|
|
|
/**
|