|
|
@@ -79,19 +79,24 @@ class ExternalAccount {
|
|
|
}
|
|
|
// not found
|
|
|
else {
|
|
|
- debug(`ExternalAccount '${accountId}' is not found, it is going to be registered.`);
|
|
|
-
|
|
|
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
|
|
|
- if (count > 0) {
|
|
|
+ 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];
|
|
|
+ }
|
|
|
|
|
|
- // create user with STATUS_ACTIVE
|
|
|
- return User.createUser('', usernameToBeRegistered, undefined, undefined, undefined, User.STATUS_ACTIVE);
|
|
|
})
|
|
|
.then((user) => {
|
|
|
return this.create({ providerType: 'ldap', accountId, user: user._id });
|