|
|
@@ -79,22 +79,12 @@ class ExternalAccount {
|
|
|
}
|
|
|
|
|
|
const User = ExternalAccount.crowi.model('User');
|
|
|
- const Config = ExternalAccount.crowi.model('Config');
|
|
|
-
|
|
|
- // get option
|
|
|
- const isTreatUsernameMatchingAsIdentical = Config.isTreatUsernameMatchingAsIdentical(ExternalAccount.crowi.getConfig());
|
|
|
|
|
|
return User.findOne({username: usernameToBeRegistered})
|
|
|
.then(user => {
|
|
|
// when the User that have the same `username` exists
|
|
|
if (user != null) {
|
|
|
- if (isTreatUsernameMatchingAsIdentical) {
|
|
|
- debug(`ExternalAccount '${accountId}' will be bound to an exisiting account`);
|
|
|
- return user;
|
|
|
- }
|
|
|
- else {
|
|
|
- throw new DuplicatedUsernameException(`User '${usernameToBeRegistered}' has already been existed`);
|
|
|
- }
|
|
|
+ throw new DuplicatedUsernameException(`User '${usernameToBeRegistered}' has already been existed`, user);
|
|
|
}
|
|
|
|
|
|
// create a new User with STATUS_ACTIVE
|
|
|
@@ -102,12 +92,23 @@ class ExternalAccount {
|
|
|
return User.createUser('', usernameToBeRegistered, undefined, undefined, undefined, User.STATUS_ACTIVE);
|
|
|
})
|
|
|
.then(newUser => {
|
|
|
- return this.create({ providerType: 'ldap', accountId, user: newUser._id });
|
|
|
+ return this.associate(providerType, accountId, newUser._id);
|
|
|
});
|
|
|
|
|
|
});
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Create ExternalAccount document and associate to existing User
|
|
|
+ *
|
|
|
+ * @param {string} providerType
|
|
|
+ * @param {string} accountId
|
|
|
+ * @param {object} user
|
|
|
+ */
|
|
|
+ static associate(providerType, accountId, user) {
|
|
|
+ return this.create({ providerType, accountId, user: user._id });
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* find all entities with pagination
|
|
|
*
|
|
|
@@ -142,9 +143,10 @@ class ExternalAccount {
|
|
|
* @class DuplicatedUsernameException
|
|
|
*/
|
|
|
class DuplicatedUsernameException {
|
|
|
- constructor(message) {
|
|
|
+ constructor(message, user) {
|
|
|
this.name = this.constructor.name;
|
|
|
this.message = message;
|
|
|
+ this.user = user;
|
|
|
}
|
|
|
}
|
|
|
|