|
@@ -70,35 +70,39 @@ class ExternalAccount {
|
|
|
*/
|
|
*/
|
|
|
static findOrRegister(providerType, accountId, usernameToBeRegistered) {
|
|
static findOrRegister(providerType, accountId, usernameToBeRegistered) {
|
|
|
|
|
|
|
|
- return this.findOne({ providerType, accountId })
|
|
|
|
|
- .then((account) => {
|
|
|
|
|
- // found
|
|
|
|
|
- if (account != null) {
|
|
|
|
|
- debug(`ExternalAccount '${accountId}' is found `, account);
|
|
|
|
|
- return account;
|
|
|
|
|
|
|
+ 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`);
|
|
|
}
|
|
}
|
|
|
- // 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) => {
|
|
|
|
|
- // throw Exception when count is not zero
|
|
|
|
|
- if (count > 0) {
|
|
|
|
|
- throw new DuplicatedUsernameException(`username '${usernameToBeRegistered}' has already been existed`);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // 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 });
|
|
|
|
|
- });
|
|
|
|
|
|
|
+ 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 });
|
|
|
|
|
+ })
|
|
|
|
|
+
|
|
|
|
|
+ });
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|