UNPKG

@gsb-core/core

Version:

GSB core services and classes for platform-independent web applications

270 lines 10.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PermissionService = void 0; const gsb_entity_service_1 = require("./entity/gsb-entity.service"); const query_params_1 = require("../types/query-params"); const query_1 = require("../types/query"); const gsb_config_1 = require("../config/gsb-config"); const gsb_utils_1 = require("../utils/gsb-utils"); /** * Service for managing Permissions (Policies) in the application */ class PermissionService { constructor() { this.ENTITY_NAME = 'GsbPermission'; this.entityService = gsb_entity_service_1.GsbEntityService.getInstance(); } /** * Get the singleton instance of PermissionService * @returns The PermissionService instance */ static getInstance() { if (!PermissionService.instance) { PermissionService.instance = new PermissionService(); } return PermissionService.instance; } /** * Check if the current user is authorized to perform an action on an entity * @param entityDef The entity definition name or ID * @param permissionType The type of permission to check * @param userId Optional user ID to check for (defaults to current user) * @returns Promise resolving to a boolean indicating if the user is authorized */ async isAuthorized(entityDef, permissionType, userId) { // For now, return true for all operations to maintain existing functionality // In a real implementation, we would: // 1. Get the current user (or use the provided userId) // 2. Get all permissions for the user (direct, via roles, via groups) // 3. Check if any permission matches the entity and permissionType try { // Get entity permissions that include this entity const entityPermissions = await this.getEntityPermissions(entityDef); // If we have no permissions for this entity, default to allowing access // This ensures backwards compatibility until permissions are properly set up if (!entityPermissions || entityPermissions.length === 0) { return true; } // Check if any permission allows this operation const hasPermission = entityPermissions.some(permission => { // Check if the permission includes the requested permission type return (permission.permissionType && (permission.permissionType & permissionType) === permissionType); }); return hasPermission; } catch (error) { console.error('Error checking authorization:', error); // In case of error, return true to maintain functionality return true; } } /** * Get permission by ID * @param id The permission ID * @returns The permission or null if not found */ async getPermissionById(id) { try { const permission = await this.entityService.getById(this.ENTITY_NAME, id, (0, gsb_config_1.getGsbToken)(), (0, gsb_config_1.getGsbTenantCode)()); return permission; } catch (error) { console.error('Error getting permission by ID:', error); return null; } } /** * Get all permissions with pagination * @param page The page number (starting from 1) * @param pageSize The number of items per page * @returns An array of permissions and the total count */ async getPermissions(page = 1, pageSize = 10) { try { const query = new query_params_1.QueryParams(this.ENTITY_NAME); // Set pagination query.startIndex = (page - 1) * pageSize; query.count = pageSize; query.calcTotalCount = true; // Sort by last update date DESC query.sortCols = (0, gsb_utils_1.getGsbDateSortCols)(); const response = await this.entityService.query(query, (0, gsb_config_1.getGsbToken)(), (0, gsb_config_1.getGsbTenantCode)()); return { permissions: (response.entities || []), totalCount: response.totalCount || 0 }; } catch (error) { console.error('Error getting permissions:', error); return { permissions: [], totalCount: 0 }; } } /** * Search for permissions * @param searchTerm The search term * @param page The page number (starting from 1) * @param pageSize The number of items per page * @returns An array of permissions and the total count */ async searchPermissions(searchTerm, page = 1, pageSize = 10) { try { const query = new query_params_1.QueryParams(this.ENTITY_NAME); // Set pagination query.startIndex = (page - 1) * pageSize; query.count = pageSize; query.calcTotalCount = true; // Sort by last update date DESC query.sortCols = (0, gsb_utils_1.getGsbDateSortCols)(); // Add search filters const nameFilter = new query_1.Filter('name'); nameFilter.isLike(`%${searchTerm}%`); query.filters = [nameFilter]; const response = await this.entityService.query(query, (0, gsb_config_1.getGsbToken)(), (0, gsb_config_1.getGsbTenantCode)()); return { permissions: (response.entities || []), totalCount: response.totalCount || 0 }; } catch (error) { console.error('Error searching permissions:', error); return { permissions: [], totalCount: 0 }; } } /** * Create a new permission * @param permission The permission to create * @returns The created permission ID or null if failed */ async createPermission(permission) { try { // Add creation fields // Prepare save request const request = { entDefName: this.ENTITY_NAME, entity: permission, entityDef: {}, entityId: '', entDefId: '', filters: [] }; const response = await this.entityService.save(request, (0, gsb_config_1.getGsbToken)(), (0, gsb_config_1.getGsbTenantCode)()); if (!response.id) { return null; } return response.id; } catch (error) { console.error('Error creating permission:', error); return null; } } /** * Update an existing permission * @param permission The permission to update * @returns True if updated successfully, false otherwise */ async updatePermission(permission) { try { if (!permission.id) { throw new Error('Permission ID is required for updates'); } // Add update fields // Prepare save request const request = { entDefName: this.ENTITY_NAME, entity: permission, entityDef: {}, entityId: permission.id, entDefId: '', filters: [] }; const response = await this.entityService.save(request, (0, gsb_config_1.getGsbToken)(), (0, gsb_config_1.getGsbTenantCode)()); return !!response.id; } catch (error) { console.error('Error updating permission:', error); return false; } } /** * Delete a permission * @param id The permission ID to delete * @returns True if deleted successfully, false otherwise */ async deletePermission(id) { try { const request = { entDefName: this.ENTITY_NAME, entityId: id, }; const response = await this.entityService.delete(request, (0, gsb_config_1.getGsbToken)(), (0, gsb_config_1.getGsbTenantCode)()); return response.deleteCount === 1; } catch (error) { console.error('Error deleting permission:', error); return false; } } /** * Get entity permissions * @param entityDefId The entity definition ID or name * @returns The entity permissions */ async getEntityPermissions(entityDefId) { try { const query = new query_params_1.QueryParams(this.ENTITY_NAME); // Add filter for entity definition const entityFilter = new query_1.Filter('entityDefs'); entityFilter.isLike(`%${entityDefId}%`); query.filters = [entityFilter]; const response = await this.entityService.query(query, (0, gsb_config_1.getGsbToken)(), (0, gsb_config_1.getGsbTenantCode)()); return (response.entities || []); } catch (error) { console.error('Error getting entity permissions:', error); return []; } } /** * Get user permissions * @param userId The user ID * @returns The user permissions */ async getUserPermissions(userId) { try { const query = new query_params_1.QueryParams(this.ENTITY_NAME); // Add filter for user const userFilter = new query_1.Filter('users'); userFilter.isLike(`%${userId}%`); query.filters = [userFilter]; const response = await this.entityService.query(query, (0, gsb_config_1.getGsbToken)(), (0, gsb_config_1.getGsbTenantCode)()); return (response.entities || []); } catch (error) { console.error('Error getting user permissions:', error); return []; } } /** * Get user's effective permissions * This combines permissions from user directly, roles and groups * @param userId The user ID * @returns All effective permissions for the user */ async getUserEffectivePermissions(userId) { try { // Get direct user permissions const userPermissions = await this.getUserPermissions(userId); // TODO: Get permissions from user's roles // TODO: Get permissions from user's groups // For now, just return direct permissions return userPermissions; } catch (error) { console.error('Error getting effective permissions:', error); return []; } } } exports.PermissionService = PermissionService; //# sourceMappingURL=permission.service.js.map