UNPKG

@gooddata/gooddata-js

Version:
211 lines (210 loc) • 8.18 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var project_1 = require("./project"); var qs_1 = __importDefault(require("qs")); var UserModule = /** @class */ (function () { function UserModule(xhr) { this.xhr = xhr; } /** * Find out whether a user is logged in * * @return {Promise} resolves with true if user logged in, false otherwise * @method isLoggedIn */ UserModule.prototype.isLoggedIn = function () { var _this = this; return new Promise(function (resolve, reject) { _this.xhr.get("/gdc/account/token").then(function (r) { if (r.response.ok) { resolve(true); } resolve(false); }, function (err) { if (err.response.status === 401) { resolve(false); } else { reject(err); } }); }); }; /** * Find out whether a specified project is available to a currently logged user * * @method isLoggedInProject * @param {String} projectId A project identifier * @return {Promise} Resolves with true if user logged in and project available, * resolves with false if user logged in and project not available, * rejects if user not logged in */ UserModule.prototype.isLoggedInProject = function (projectId) { var _this = this; return this.getCurrentProfile().then(function (profile) { return new Promise(function (resolve, reject) { var projectModule = new project_1.ProjectModule(_this.xhr); projectModule.getProjects(profile.links.self.split("/")[4]).then(function (projects) { if (projects.find(function (p) { return p.links.self === "/gdc/projects/" + projectId; })) { resolve(true); } else { resolve(false); } }, function (err) { reject(err); }); }); }); }; /** * This function provides an authentication entry point to the GD API. It is needed to authenticate * by calling this function prior any other API calls. After providing valid credentials * every subsequent API call in a current session will be authenticated. * * @method login * @param {String} username * @param {String} password */ UserModule.prototype.login = function (username, password) { return this.xhr .post("/gdc/account/login", { body: JSON.stringify({ postUserLogin: { login: username, password: password, remember: 1, captcha: "", verifyCaptcha: "", }, }), }) .then(function (r) { return r.getData(); }); }; /** * This function provides an authentication entry point to the GD API via SSO * https://help.gooddata.com/display/developer/GoodData+PGP+Single+Sign-On * * @method loginSso * @param {String} encryptedClaims PGP message * @param {String} ssoProvider * @param {String} targetUrl */ UserModule.prototype.loginSso = function (encryptedClaims, ssoProvider, targetUrl) { return this.xhr.post("/gdc/account/customerlogin", { data: { pgpLoginRequest: { targetUrl: targetUrl, ssoProvider: ssoProvider, encryptedClaims: encryptedClaims, }, }, }); }; /** * Logs out current user * @method logout */ UserModule.prototype.logout = function () { var _this = this; return this.isLoggedIn().then(function (loggedIn) { if (loggedIn) { return _this.xhr.get("/gdc/app/account/bootstrap").then(function (result) { var data = result.getData(); var userUri = data.bootstrapResource.accountSetting.links.self; var userId = userUri.match(/([^\/]+)\/?$/)[1]; return _this.xhr.del("/gdc/account/login/" + userId); }); } return Promise.resolve(); }, function (err) { return Promise.reject(err); }); }; /** * Gets current user's profile * @method getCurrentProfile * @return {Promise} Resolves with account setting object */ UserModule.prototype.getCurrentProfile = function () { return this.xhr.get("/gdc/account/profile/current").then(function (r) { return r.getData().accountSetting; }); }; /** * Updates user's profile settings * @method updateProfileSettings * @param {String} profileId - User profile identifier * @param {Object} profileSetting */ UserModule.prototype.updateProfileSettings = function (profileId, profileSetting) { // TODO return this.xhr.put("/gdc/account/profile/" + profileId + "/settings", { body: profileSetting, }); }; /** * Returns info about currently logged in user from bootstrap resource * @method getAccountInfo */ UserModule.prototype.getAccountInfo = function () { var _this = this; return this.xhr.get("/gdc/app/account/bootstrap").then(function (result) { var data = result.getData(); return _this.getAccountInfoInBootstrap(data); }); }; /** * Returns current user info from bootstrapData * @method getAccountInfoInBootstrap * @param bootstrapData - data was got from bootstrap resource */ UserModule.prototype.getAccountInfoInBootstrap = function (bootstrapData) { var _a = bootstrapData.bootstrapResource, _b = _a.accountSetting, login = _b.login, firstName = _b.firstName, lastName = _b.lastName, profileUri = _b.links.self, loginMD5 = _a.current.loginMD5, organizationName = _a.settings.organizationName; return { login: login, loginMD5: loginMD5, firstName: firstName, lastName: lastName, organizationName: organizationName, profileUri: profileUri, }; }; /** * Gets user configs including user specific feature flags * * @param {String} userId - A user identifier * @return {IUserConfigsSettingItem[]} An array of user configs setting item */ UserModule.prototype.getUserConfigs = function (userId) { return this.xhr.get("/gdc/account/profile/" + userId + "/config").then(function (apiResponse) { var userConfigs = apiResponse.getData(); var items = userConfigs.settings.items; return items || []; }); }; /** * Returns the feature flags valid for the currently logged in user. * @method getFeatureFlags */ UserModule.prototype.getFeatureFlags = function () { return this.xhr .get("/gdc/app/account/bootstrap") .then(function (r) { return r.getData(); }) .then(function (result) { return result.bootstrapResource.current.featureFlags; }); }; /** * Initiates SPI SAML SSO * @param relayState URL of the page where the user is redirected after a successful login */ UserModule.prototype.initiateSamlSso = function (relayState) { this.xhr .get("/gdc/account/samlrequest?" + qs_1.default.stringify({ relayState: relayState })) .then(function (data) { return data.getData(); }) .then(function (response) { var loginUrl = response.samlRequests.items[0].samlRequest.loginUrl; window.location.assign(loginUrl); }); }; return UserModule; }()); exports.UserModule = UserModule;