Skip to content

Commit

Permalink
Merge pull request #9 from cermati/fix/error-provision-existing-pendi…
Browse files Browse the repository at this point in the history
…ng-user

fix(provision): Fix Error When Provisioning Existing Pending User
  • Loading branch information
sdsdkkk authored Jun 9, 2021
2 parents ea33e0f + a4cdd3a commit 37391ea
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 44 deletions.
32 changes: 29 additions & 3 deletions lib/core/AccountManager.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
});
});
}
Expand Down
6 changes: 6 additions & 0 deletions lib/core/Errors.js
Original file line number Diff line number Diff line change
@@ -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";
2 changes: 1 addition & 1 deletion lib/core/Metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' ]
};
106 changes: 67 additions & 39 deletions lib/core/RedashClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down

0 comments on commit 37391ea

Please sign in to comment.