|
@@ -24,6 +24,14 @@ schema.plugin(uniqueValidator);
|
|
|
*/
|
|
*/
|
|
|
class ExternalAccount {
|
|
class ExternalAccount {
|
|
|
|
|
|
|
|
|
|
+ static set crowi(crowi) {
|
|
|
|
|
+ this._crowi = crowi;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ static get crowi() {
|
|
|
|
|
+ return this._crowi;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* get the populated user entity
|
|
* get the populated user entity
|
|
|
*
|
|
*
|
|
@@ -43,11 +51,11 @@ class ExternalAccount {
|
|
|
* @static
|
|
* @static
|
|
|
* @param {string} providerType
|
|
* @param {string} providerType
|
|
|
* @param {string} accountId
|
|
* @param {string} accountId
|
|
|
- * @param {object} createUserFunction the function that create a user document and return Promise<User>
|
|
|
|
|
|
|
+ * @param {object} usernameToBeRegistered the username of User entity that will be created when accountId is not found
|
|
|
* @returns {Promise<ExternalAccount>}
|
|
* @returns {Promise<ExternalAccount>}
|
|
|
* @memberof ExternalAccount
|
|
* @memberof ExternalAccount
|
|
|
*/
|
|
*/
|
|
|
- static findOrRegister(providerType, accountId, createUserFunction) {
|
|
|
|
|
|
|
+ static findOrRegister(providerType, accountId, usernameToBeRegistered) {
|
|
|
|
|
|
|
|
return this.findOne({ providerType, accountId })
|
|
return this.findOne({ providerType, accountId })
|
|
|
.then((account) => {
|
|
.then((account) => {
|
|
@@ -60,9 +68,18 @@ class ExternalAccount {
|
|
|
else {
|
|
else {
|
|
|
debug(`ExternalAccount '${accountId}' is not found, it is going to be registered.`);
|
|
debug(`ExternalAccount '${accountId}' is not found, it is going to be registered.`);
|
|
|
|
|
|
|
|
|
|
+ const User = ExternalAccount.crowi.model('User');
|
|
|
|
|
+
|
|
|
// TODO count and throw error if username is dupricated
|
|
// TODO count and throw error if username is dupricated
|
|
|
|
|
+ return User.count({username: usernameToBeRegistered})
|
|
|
|
|
+ .then((count) => {
|
|
|
|
|
+ if (count > 0) {
|
|
|
|
|
+ throw new DuplicatedUsernameException(`username '${usernameToBeRegistered}' has already been existed`);
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- return createUserFunction().then((user) => {
|
|
|
|
|
|
|
+ return User.createUser('', usernameToBeRegistered, undefined, undefined, undefined);
|
|
|
|
|
+ })
|
|
|
|
|
+ .then((user) => {
|
|
|
return this.create({ providerType: 'ldap', accountId, user: user._id });
|
|
return this.create({ providerType: 'ldap', accountId, user: user._id });
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
@@ -72,7 +89,20 @@ class ExternalAccount {
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+/**
|
|
|
|
|
+ * The Exception class thrown when User.username is duplicated when creating user
|
|
|
|
|
+ *
|
|
|
|
|
+ * @class DuplicatedUsernameException
|
|
|
|
|
+ */
|
|
|
|
|
+class DuplicatedUsernameException {
|
|
|
|
|
+ constructor(message) {
|
|
|
|
|
+ this.name = this.constructor.name;
|
|
|
|
|
+ this.message = message;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
module.exports = function(crowi) {
|
|
module.exports = function(crowi) {
|
|
|
|
|
+ ExternalAccount.crowi = crowi;
|
|
|
schema.loadClass(ExternalAccount);
|
|
schema.loadClass(ExternalAccount);
|
|
|
return mongoose.model('ExternalAccount', schema);
|
|
return mongoose.model('ExternalAccount', schema);
|
|
|
}
|
|
}
|