@mindmakr/gs-websdk
Version:
Web SDK for Guru SaaS System - Complete JavaScript/TypeScript SDK for building applications with dynamic schema management
307 lines • 10.4 kB
JavaScript
;
/**
* Authentication & Authorization Service Module
* Provides comprehensive RBAC (Role-Based Access Control) functionality
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.AuthService = void 0;
class AuthService {
constructor(client) {
this.client = client;
}
// ============================================================================
// USER MANAGEMENT
// ============================================================================
/**
* Get users with pagination and filtering
*/
async getUsers(pagination, filters, config) {
const params = new URLSearchParams();
if (pagination?.page)
params.append('page', pagination.page.toString());
if (pagination?.limit)
params.append('limit', pagination.limit.toString());
if (filters?.email)
params.append('email', filters.email);
if (filters?.role)
params.append('role', filters.role);
if (filters?.tenant_id)
params.append('tenant_id', filters.tenant_id);
if (filters?.search)
params.append('search', filters.search);
const queryString = params.toString() ? `?${params.toString()}` : '';
return this.client.get(`/api/user${queryString}`, config);
}
/**
* Get user by ID
*/
async getUserById(id, config) {
return this.client.get(`/api/user/${id}`, config);
}
/**
* Create new user
*/
async createUser(userData, config) {
return this.client.post('/api/user', userData, config);
}
/**
* Update user
*/
async updateUser(id, userData, config) {
return this.client.patch(`/api/user/${id}`, userData, config);
}
// Note: User deletion is not supported by the backend for data integrity.
// Users can be disabled by removing their roles instead.
// ============================================================================
// ROLE MANAGEMENT
// ============================================================================
/**
* Get all roles
*/
async getRoles(tenantId, config) {
const params = new URLSearchParams();
if (tenantId)
params.append('tenant_id', tenantId);
params.append('limit', '1000'); // Get all roles
const queryString = params.toString() ? `?${params.toString()}` : '';
const response = await this.client.get(`/api/role${queryString}`, config);
return Array.isArray(response) ? response : response.data || [];
}
/**
* Get role by ID with permissions
*/
async getRoleById(roleId, config) {
return this.client.get(`/api/role/${roleId}`, config);
}
/**
* Create new role
*/
async createRole(roleData, config) {
return this.client.post('/api/role', roleData, config);
}
/**
* Update role
*/
async updateRole(roleId, roleData, config) {
return this.client.patch(`/api/role/${roleId}`, roleData, config);
}
/**
* Delete role
*/
async deleteRole(roleId, config) {
return this.client.delete(`/api/role/${roleId}`, config);
}
/**
* Clone role
*/
async cloneRole(roleId, data, config) {
return this.client.post(`/api/role/${roleId}/clone`, data, config);
}
// ============================================================================
// PERMISSION MANAGEMENT
// ============================================================================
/**
* Get all permissions
*/
async getPermissions(config) {
const response = await this.client.get('/api/permission?limit=1000', config);
return Array.isArray(response) ? response : response.data || [];
}
/**
* Assign permission to role
*/
async assignPermissionToRole(roleId, permissionId, config) {
return this.client.post(`/api/role/${roleId}/permissions`, {
permissions: [parseInt(permissionId)]
}, config);
}
/**
* Revoke permission from role
*/
async revokePermissionFromRole(roleId, permissionId, config) {
return this.client.delete(`/api/role/${roleId}/permissions`, {
...config,
headers: {
...config?.headers,
'Content-Type': 'application/json'
}
});
}
// ============================================================================
// USER-ROLE MANAGEMENT
// ============================================================================
/**
* Get roles assigned to a user
*/
async getUserRoles(userId, config) {
const response = await this.client.get(`/api/user-role/${userId}/roles`, config);
return Array.isArray(response) ? response : response.data || [];
}
/**
* Set roles for a user (replaces all existing roles)
*/
async setUserRoles(userId, roleIds, config) {
return this.client.put(`/api/user-role/${userId}/roles`, { roles: roleIds }, config);
}
/**
* Add role to user
*/
async addRoleToUser(userId, roleId, config) {
return this.client.post(`/api/user-role/${userId}/roles`, { roleId: parseInt(roleId) }, config);
}
/**
* Remove role from user
*/
async removeRoleFromUser(userId, roleId, config) {
return this.client.delete(`/api/user-role/${userId}/roles/${roleId}`, config);
}
// ============================================================================
// TENANT MANAGEMENT
// ============================================================================
/**
* Get all tenants (super admin only)
*/
async getAllTenants(config) {
return this.client.get('/api/admin/tenants', config);
}
/**
* Create tenant (super admin only)
*/
async createTenant(tenantData, config) {
return this.client.post('/api/admin/tenants', tenantData, config);
}
/**
* Update tenant (super admin only)
*/
async updateTenant(tenantId, tenantData, config) {
return this.client.patch(`/api/admin/tenants/${tenantId}`, tenantData, config);
}
// Note: Tenant deletion is not supported by the backend for data integrity.
// Tenants can be disabled through other means if needed.
// ============================================================================
// PERMISSION CHECKING
// ============================================================================
/**
* Check if current user can access admin endpoints
*/
async canAccessAdmin(config) {
try {
const response = await this.client.get('/api/auth/check-admin-access', config);
return response.canAccessAdmin || false;
}
catch (error) {
if (error.statusCode === 403) {
return false;
}
throw error;
}
}
/**
* Check if current user can create tenants
*/
async canCreateTenant(config) {
try {
const response = await this.client.get('/api/auth/check-tenant-creation', config);
return response.canCreateTenant || false;
}
catch (error) {
if (error.statusCode === 403) {
return false;
}
throw error;
}
}
/**
* Check if current user has a specific permission
*/
async checkPermission(resource, action, config) {
try {
const response = await this.client.post('/api/auth/check-permission', { resource, action }, config);
return response.hasPermission || false;
}
catch (error) {
if (error.statusCode === 403) {
return false;
}
throw error;
}
}
/**
* Check if current user has a template-specific permission
*/
async checkTemplatePermission(templateCode, action, config) {
try {
const response = await this.client.post('/api/auth/check-template-permission', { templateCode, action }, config);
return response.hasPermission || false;
}
catch (error) {
if (error.statusCode === 403) {
return false;
}
throw error;
}
}
// ============================================================================
// USER SETTINGS
// ============================================================================
/**
* Get all user settings
*/
async getUserSettings(config) {
return this.client.get('/api/settings', config);
}
/**
* Get specific user setting
*/
async getUserSetting(key, config) {
try {
return await this.client.get(`/api/settings/${key}`, config);
}
catch (error) {
if (error.statusCode === 404) {
return null;
}
throw error;
}
}
/**
* Set user setting
*/
async setUserSetting(key, value, config) {
return this.client.put(`/api/settings/${key}`, { value }, config);
}
/**
* Delete user setting
*/
async deleteUserSetting(key, config) {
try {
await this.client.delete(`/api/settings/${key}`, config);
return true;
}
catch (error) {
if (error.statusCode === 404) {
return false;
}
throw error;
}
}
// ============================================================================
// ADMIN FUNCTIONS (Super Admin Only)
// ============================================================================
/**
* Get all roles across all tenants (super admin only)
*/
async getAllRoles(config) {
const response = await this.client.get('/api/admin/roles', config);
return Array.isArray(response) ? response : response.data || [];
}
/**
* Get all permissions (super admin access)
*/
async getAllPermissions(config) {
const response = await this.client.get('/api/admin/permissions', config);
return Array.isArray(response) ? response : response.data || [];
}
}
exports.AuthService = AuthService;
//# sourceMappingURL=auth.js.map