diff --git a/lib/core/AccountManager.js b/lib/core/AccountManager.js index b21682e..40fa851 100644 --- a/lib/core/AccountManager.js +++ b/lib/core/AccountManager.js @@ -1,8 +1,9 @@ 'use strict'; -const Promise = require('bluebird'); const RedashClient = require('./RedashClient').RedashClient; +const errors = require('./Errors'); + exports.AccountManager = class AccountManager { constructor (config) { this.config = config; @@ -17,8 +18,33 @@ exports.AccountManager = class AccountManager { return this.redashClient().login() .then(() => { return this.redashClient().createUser(context.user.email, context.user.name) - .catch(() => { - return this.redashClient().enableUser(context.user.email); + .catch((err) => { + if (err.response.body.message === errors.EMAIL_TAKEN_ERROR) { + return this.redashClient().getUsers(context.user.email, 'disabled') + .then(res => { + // If user is disabled, then we need to enable it first and then + // resend the invitation if the user invitation status is pending + if (res.results.length > 0) { + return this.redashClient().enableUser(context.user.email) + .then(() => { + if (res.results[0].is_invitation_pending) { + return this.redashClient().resendUserInvitation(context.user.email); + } + }); + } + + // Otherwise, check if the invitation status is pending, if yes then + // resend the invitation + return this.redashClient().getUsers(context.user.email, 'pending') + .then(res => { + if (res.results.length > 0) { + return this.redashClient().resendUserInvitation(context.user.email); + } + }); + }); + } + + throw err; }); }); } diff --git a/lib/core/Errors.js b/lib/core/Errors.js new file mode 100644 index 0000000..2dfc9e5 --- /dev/null +++ b/lib/core/Errors.js @@ -0,0 +1,6 @@ +// Error message returned from Redash when email is already taken +exports.EMAIL_TAKEN_ERROR = "Email already taken."; + +exports.USER_PENDING_NOT_FOUND_ERROR = "User pending not found"; +exports.USER_ACTIVE_NOT_FOUND_ERROR = "User active not found"; +exports.USER_DISABLED_NOT_FOUND_ERROR = "User disabled not found"; diff --git a/lib/core/Metadata.js b/lib/core/Metadata.js index 3363125..a69d04b 100644 --- a/lib/core/Metadata.js +++ b/lib/core/Metadata.js @@ -3,6 +3,6 @@ exports.ModuleMetadata = { Engine: 'iamx-redash', Name: 'IAMX Redash Connector', - Version: '0.0.3', + Version: '0.0.4', SupportedExecution: [ 'provision', 'revoke', 'show' ] }; diff --git a/lib/core/RedashClient.js b/lib/core/RedashClient.js index 8200bcd..d03d08d 100644 --- a/lib/core/RedashClient.js +++ b/lib/core/RedashClient.js @@ -8,6 +8,8 @@ const fs = require('fs'); const FAILED_LOGIN_TITLE_TEXT = 'Login to Redash'; const SUCCESS_LOGIN_TITLE_TEXT = 'Redash'; +const errors = require('./Errors'); + exports.RedashClient = class RedashClient { constructor (config) { this.config = config; @@ -98,55 +100,81 @@ exports.RedashClient = class RedashClient { enableUser (email) { this._mustLogin(); - return Promise.resolve( - this.getUsers(email, 'disabled').then(res => { - let userId = res.results[0].id; - let uri = `${this.config.baseUri}/api/users/${userId}/disable`; - - return this.agent.delete(uri) - .set('Cookie', this.cookies) - .then(response => { - let result = JSON.parse(response.text); - return result; - }); - }) - ); + return this.getUsers(email, 'disabled').then(res => { + if (res.results.length <= 0) { + throw new Error(errors.USER_DISABLED_NOT_FOUND_ERROR); + } + + let userId = res.results[0].id; + let uri = `${this.config.baseUri}/api/users/${userId}/disable`; + + return this.agent.delete(uri) + .set('Cookie', this.cookies) + .then(response => { + let result = JSON.parse(response.text); + return result; + }); + }); }; disableUser (email) { this._mustLogin(); - return Promise.resolve( - this.getUsers(email, 'active').then(res => { - let userId = res.results[0].id; - let uri = `${this.config.baseUri}/api/users/${userId}/disable`; - - return this.agent.post(uri) - .set('Cookie', this.cookies) - .then(response => { - let result = JSON.parse(response.text); - return result; - }); - }) - ); + return this.getUsers(email, 'active').then(res => { + if (res.results.length <= 0) { + throw new Error(errors.USER_ACTIVE_NOT_FOUND_ERROR); + } + + let userId = res.results[0].id; + let uri = `${this.config.baseUri}/api/users/${userId}/disable`; + + return this.agent.post(uri) + .set('Cookie', this.cookies) + .then(response => { + let result = JSON.parse(response.text); + return result; + }); + }); + }; + + resendUserInvitation(email) { + this._mustLogin(); + + return this.getUsers(email, 'pending').then(res => { + if (res.results.length <= 0) { + throw new Error(errors.USER_PENDING_NOT_FOUND_ERROR); + } + + let userId = res.results[0].id; + let uri = `${this.config.baseUri}/api/users/${userId}/invite`; + + return this.agent.post(uri) + .set('Cookie', this.cookies) + .then(response => { + let result = JSON.parse(response.text); + return result; + }); + }); }; deletePendingUser (email) { this._mustLogin(); - return Promise.resolve( - this.getUsers(email, 'pending').then(res => { - let userId = res.results[0].id; - let uri = `${this.config.baseUri}/api/users/${userId}`; - - return this.agent.delete(uri) - .set('Cookie', this.cookies) - .then(response => { - let result = JSON.parse(response.text); - return result; - }); - }) - ); + return this.getUsers(email, 'pending').then(res => { + if (res.results.length <= 0) { + return reject(errors.USER_PENDING_NOT_FOUND_ERROR); + } + + let userId = res.results[0].id; + let uri = `${this.config.baseUri}/api/users/${userId}`; + + return this.agent.delete(uri) + .set('Cookie', this.cookies) + .then(response => { + let result = JSON.parse(response.text); + return result; + }); + }); }; _loginSuccess(response) { diff --git a/package.json b/package.json index 781418c..69283ad 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@cermati/iamx-redash-connector", - "version": "0.0.3", + "version": "0.0.4", "description": "Redash Connector for IAMX", "main": "lib/index.js", "dependencies": {