UNPKG

baasic-sdk-javascript

Version:

JavaScript SDK provides core functionality for building web and mobile applications on [Baasic](http://www.baasic.com/).

358 lines (357 loc) 15.6 kB
"use strict"; /* globals module */ /** * @module permissionClient * @description Role Client provides an easy way to consume Role REST API end-points. In order to obtain needed routes `permissionClient` uses `permissionRoute`. */ Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var inversify_1 = require("inversify"); var common_1 = require("../../common"); var httpApi_1 = require("../../httpApi"); var _1 = require("./"); var contracts_1 = require("../../core/contracts"); var PermissionClient = /** @class */ (function () { function PermissionClient(permissionRoute, apiClient, application) { this.permissionRoute = permissionRoute; this.apiClient = apiClient; this.application = application; this.utility = new common_1.Utility(); this.permissionHash = {}; } Object.defineProperty(PermissionClient.prototype, "routeDefinition", { /** * Provides direct access to `permissionRoute`. * @method * @example permissionClient.routeDefinition.get().expand(expandObject); **/ get: function () { return this.permissionRoute; }, enumerable: true, configurable: true }); /** * Returns a promise that is resolved once the findAll action has been performed. Success response returns a list of access sections matching the given criteria. * @method * @param options Query resource options object. * @returns A promise that is resolved once the find action has been performed. * @example permissionClient.findAll({ searchQuery : '<search-phrase>' }) .then(function (collection) { // perform success action here }, function (response, status, headers, config) { // perform error handling here }); **/ PermissionClient.prototype.findAll = function (options) { return this.apiClient.get(this.permissionRoute.findAll(options)); }; /** * Returns a promise that is resolved once the find action has been performed. Success response returns a list of role resources matching the given criteria. * @method * @param options Query resource options object. * @returns A promise that is resolved once the find action has been performed. * @example permissionClient.find({ section : '<access-section>', search : '<search-phrase>' }) .then(function (collection) { // perform success action here }, function (response, status, headers, config) { // perform error handling here }); **/ PermissionClient.prototype.find = function (section, options) { return this.apiClient.get(this.permissionRoute.find(section, options)); }; /** * Returns a promise that is resolved once the getActions action has been performed. Success response returns a list of access policies that match the specified search parameters. * @method * @example permissionClient.getActions({ search : '<search-phrase>' }) .success(function (collection) { // perform success action here }) .error(function (response, status, headers, config) { // perform error handling here }); **/ PermissionClient.prototype.getActions = function (options) { return this.apiClient.get(this.routeDefinition.getActions(options)); }; /** * Returns a promise that is resolved once the getSectionPolicies action has beed preformed. Success response returns a list of requested access policies that match the specified search parameters */ PermissionClient.prototype.getSectionsPolicies = function (sectionAbrvs, options) { return this.apiClient.get(this.permissionRoute.findSectionsPolicies(sectionAbrvs, options)); }; /** * Returns a promise that is resolved once the getPermissionSubjects action has been performed. Success response returns a list of matching user and role resources. * @method * @example permissionClient.getPermissionSubjects({ orderBy : '<field>', orderDirection : '<asc|desc>', search : '<search-phrase>' }) .success(function (collection) { // perform success action here }) .error(function (response, status, headers, config) { // perform error handling here }); **/ PermissionClient.prototype.getPermissionSubjects = function (options) { var queue = []; var resolvedTasks = 0; var self = this; queue.push(this.getUsers(options) .then(function (collection) { var membCollection = []; collection.data.item.forEach(function (element) { var membershipItem = { name: element.userName, role: '' }; membCollection.push(self.utility.extend(membershipItem, element)); }); return membCollection; }, function (data) { if (data.status !== undefined && data.status !== 403) { return data; } })); queue.push(self.getRoles(options) .then(function (collection) { var membCollection = []; collection.data.item.forEach(function (element) { var membershipItem = { name: element.name, roleName: element.name, userName: '' }; membCollection.push(self.utility.extend(membershipItem, element)); }); return membCollection; }, function (data) { if (data.status !== undefined && data.status !== 403) { return data; } })); return Promise.all(queue).then(function (membershipCollection) { return self.utility.OrderByArray([].concat.apply([], membershipCollection), 'name'); }); }; /** * Returns a promise that is resolved once the create action has been performed; this action creates a new permission resource. * @method * @example // readAction and updateActions are resources previously fetched using getActions. permissionClient.create({ actions : [readAction, updateAction], section : '<section-name>', userName : '<userName>' }) .success(function (data) { // perform success action here }) .error(function (response, status, headers, config) { // perform error handling here }); **/ PermissionClient.prototype.create = function (data) { return this.apiClient.post(this.permissionRoute.create(), data); }; /** * Returns a promise that is resolved once the remove action has been performed. If the action is successfully complete, an access policy assigned to the specified role and section will be removed. * @method * @example // permission is a resource previously fetched using get action. permissionClient.remove(permission) .success(function (data) { // perform success action here }) .error(function (response, status, headers, config) { // perform error handling here }); **/ PermissionClient.prototype.remove = function (data) { return this.apiClient.delete(this.routeDefinition.remove(data)); }; /** * Creates a new in-memory permission object. * @method * @example // action collection are lookup items fetched using lookupClient.get action. var actionCollection; return lookupClient.get() .success(function (data) { actionCollection = data; }) .error(function (data, status, headers, config) {}); // subjectItem is an item fetched using permissionClient.getPermissionSubjects action. permissionClient.createPermission('<section-Name>', actionCollection, subjectItem); **/ PermissionClient.prototype.createPermission = function (section, actions, membershipItem) { var _this = this; var permission = { dirty: true, role: membershipItem.roleName, userName: membershipItem.userName, section: section, actions: [] }; actions.forEach(function (element) { var newAction = { checked: false }; _this.utility.extend(newAction, element); permission.actions.push(newAction); }); return permission; }; /** * Finds a permission in a given permission collection. * @method * @example permissionClient.findPermission(permissionObj, permissionCollection); **/ PermissionClient.prototype.findPermission = function (permission, permissionCollection) { for (var i = 0; i < permissionCollection.length; i++) { var item = permissionCollection[i]; if (item.section === permission.section && ((!this.isEmpty(item.role) && !this.isEmpty(permission.role) && item.role === permission.role) || (!this.isEmpty(item.userName) && !this.isEmpty(permission.userName) && item.userName === permission.userName))) { return item; } } return undefined; }; /** * Checks if a permission object exists in a given permission collection. * @method * @example permissionClient.exists(permissionObj, permissionCollection); **/ PermissionClient.prototype.exists = function (permission, permissionCollection) { return this.findPermission(permission, permissionCollection) !== undefined; }; /** * Returns a promise that is resolved once the togglePermission action has been completed. The action will internally either call a `remove` or `create` action based on given criteria. * @method * @example permissionClient.togglePermission(permissionObj, action); **/ PermissionClient.prototype.togglePermission = function (permission, action) { var requestPermission = { actions: [] }; this.utility.extend(requestPermission, permission); requestPermission.actions = [action]; var operation; if (!action.checked) { operation = this.remove; } else { operation = this.create; } return operation.call(this, requestPermission); }; /** * Fetches and returns and object containing all existing module permissions. * @method * @example permissionClient.getModulePermissions('<section-name>'); **/ PermissionClient.prototype.getModulePermissions = function (section) { var permission = { update: this.hasPermission(this.firstCharToLowerCase(section) + '.update'), create: this.hasPermission(this.firstCharToLowerCase(section) + '.create'), remove: this.hasPermission(this.firstCharToLowerCase(section) + '.delete'), read: this.hasPermission(this.firstCharToLowerCase(section) + '.read'), full: this.hasPermission(this.firstCharToLowerCase(section) + '.full') }; return permission; }; PermissionClient.prototype.resetPermissions = function () { this.permissionHash[this.application.getApiKey()] = {}; }; /** * Checks if current user has permissions to perform a certain action. To optimize performance this information is cached and can be reset using the resetPermissions action. Permissions cache should be reset when updated user information is set. * @method * @example baasicAuthorizationService.hasPermission("<baasic-Section>.<action>"); **/ PermissionClient.prototype.hasPermission = function (authorization) { var apiKey = this.application.getApiKey(); //Initialize application permissions if (!this.permissionHash.hasOwnProperty(apiKey)) { this.resetPermissions(); } if (this.permissionHash[apiKey].hasOwnProperty(authorization)) { return this.permissionHash[apiKey][authorization]; } var userContainer = this.application.getUser(); if (userContainer === undefined) { return; } var user = userContainer.user; var hasPermission = false; if (user) { if (user.permissions) { var tokens = authorization.split('.'); if (tokens.length > 0) { var section = tokens[0]; var sectionPermissions = user.permissions[section]; if (sectionPermissions) { if (tokens.length > 1) { var action = tokens[1].toLowerCase(); for (var i = 0; i < sectionPermissions.length; i++) { if (sectionPermissions[i].toLowerCase() === action) { hasPermission = true; break; } } } else { hasPermission = true; } } } } } this.permissionHash[apiKey][authorization] = hasPermission; return hasPermission; }; PermissionClient.prototype.isEmpty = function (data) { return data === undefined || data === null || data === ''; }; PermissionClient.prototype.getRoles = function (options) { return this.apiClient.get(this.routeDefinition.getRoles(options)); }; PermissionClient.prototype.getUsers = function (options) { return this.apiClient.get(this.routeDefinition.getUsers(options)); }; PermissionClient.prototype.firstCharToLowerCase = function (text) { return text.replace(/^./, function (char) { return char.toLowerCase(); }); }; PermissionClient = tslib_1.__decorate([ inversify_1.injectable(), tslib_1.__param(0, inversify_1.inject(_1.TYPES.PermissionRoute)), tslib_1.__param(1, inversify_1.inject(httpApi_1.httpTYPES.ApiClient)), tslib_1.__param(2, inversify_1.inject(contracts_1.TYPES.IBaasicApp)), tslib_1.__metadata("design:paramtypes", [_1.PermissionRoute, httpApi_1.ApiClient, Object]) ], PermissionClient); return PermissionClient; }()); exports.PermissionClient = PermissionClient; /** * @overview ***Notes:** - Refer to the [Baasic REST API](http://dev.baasic.com/api/reference/home) for detailed information about available Baasic REST API end-points. - All end-point objects are transformed by the associated route definition. */