UNPKG

@mamoorali295/rbac

Version:

Complete RBAC (Role-Based Access Control) system for Node.js with Express middleware, NestJS integration, GraphQL support, MongoDB & PostgreSQL support, modern admin dashboard, TypeScript support, and dynamic permission management

274 lines (273 loc) 8.78 kB
import { DatabaseAdapter } from '../adapters/DatabaseAdapter'; import { RbacModuleOptions } from './rbac.module'; /** * Admin service interface for defining user data structure */ export interface UserData { user_id: string; name?: string; email?: string; role_id?: string; } /** * Admin service interface for defining role data structure */ export interface RoleData { name: string; description?: string; } /** * Admin service interface for defining feature data structure */ export interface FeatureData { name: string; description?: string; } /** * Admin service interface for defining permission data structure */ export interface PermissionData { name: string; description?: string; } /** * Admin service interface for dashboard statistics */ export interface DashboardStats { users: number; roles: number; features: number; permissions: number; } /** * NestJS Admin Service for RBAC Dashboard * Provides business logic for the admin dashboard operations. * Handles all database operations through the DatabaseAdapter. * * Features: * - User management (CRUD operations) * - Role management with feature assignments * - Feature and permission management * - Dashboard statistics * - Admin authentication validation * * @example * ```typescript * @Injectable() * export class MyAdminService { * constructor(private adminService: RbacAdminService) {} * * async getAdminStats() { * return await this.adminService.getDashboardStats(); * } * * async manageUsers() { * const users = await this.adminService.getAllUsers(10, 0, ''); * return users; * } * } * ``` */ export declare class RbacAdminService { private config; private dbAdapter; private adminCredentials; constructor(config: RbacModuleOptions, dbAdapter: DatabaseAdapter, adminConfig: { adminCredentials: { username: string; password: string; }; sessionSecret: string; }); /** * Validate admin credentials for authentication * @param username - Admin username * @param password - Admin password * @returns Promise<boolean> - True if credentials are valid */ validateAdmin(username: string, password: string): Promise<boolean>; /** * Get dashboard statistics including counts of users, roles, features, and permissions * @returns Promise<DashboardStats> - Dashboard statistics object */ getDashboardStats(): Promise<DashboardStats>; /** * Get all users with pagination and search functionality * @param limit - Number of users per page * @param skip - Number of users to skip (for pagination) * @param search - Search query for filtering users * @returns Promise with users array and total count */ getAllUsers(limit: number, skip: number, search?: string): Promise<{ items: any[]; total: number; }>; /** * Find user by user ID * @param userId - Unique user identifier * @returns Promise<any | null> - User object or null if not found */ findUserByUserId(userId: string): Promise<any | null>; /** * Find user by user ID with role information * @param userId - Unique user identifier * @returns Promise<any | null> - User object with role or null if not found */ findUserByUserIdWithRole(userId: string): Promise<any | null>; /** * Create a new user in the RBAC system * @param userData - User data object * @returns Promise<any> - Created user object */ createUser(userData: UserData): Promise<any>; /** * Update user information * @param userId - Unique user identifier * @param updateData - Data to update * @returns Promise<any> - Updated user object */ updateUser(userId: string, updateData: Partial<UserData>): Promise<any>; /** * Delete user from the RBAC system * @param userId - Unique user identifier * @returns Promise<void> */ deleteUser(userId: string): Promise<void>; /** * Get all roles in the system * @returns Promise with roles array and total count */ getAllRoles(): Promise<{ items: any[]; total: number; }>; /** * Find role by name * @param roleName - Role name * @returns Promise<any | null> - Role object or null if not found */ findRoleByName(roleName: string): Promise<any | null>; /** * Find role by ID with associated features * @param roleId - Role identifier * @returns Promise<any | null> - Role object with features or null if not found */ findRoleByIdWithFeatures(roleId: string): Promise<any | null>; /** * Create a new role * @param roleData - Role data object * @returns Promise<any> - Created role object */ createRole(roleData: RoleData): Promise<any>; /** * Update role information * @param roleId - Role identifier * @param updateData - Data to update * @returns Promise<any> - Updated role object */ updateRole(roleId: string, updateData: Partial<RoleData>): Promise<any>; /** * Delete role from the system * @param roleId - Role identifier * @returns Promise<void> */ deleteRole(roleId: string): Promise<void>; /** * Assign features and permissions to a role * @param roleId - Role identifier * @param featurePermissions - Array of feature-permission mappings * @returns Promise<void> */ assignRoleFeaturePermissions(roleId: string, featurePermissions: Array<{ feature_id: string; permission_ids: string[]; }>): Promise<void>; /** * Get all features in the system * @returns Promise with features array and total count */ getAllFeatures(): Promise<{ items: any[]; total: number; }>; /** * Find feature by name * @param featureName - Feature name * @returns Promise<any | null> - Feature object or null if not found */ findFeatureByName(featureName: string): Promise<any | null>; /** * Find feature by ID * @param featureId - Feature identifier * @returns Promise<any | null> - Feature object or null if not found */ findFeatureById(featureId: string): Promise<any | null>; /** * Create a new feature * @param featureData - Feature data object * @returns Promise<any> - Created feature object */ createFeature(featureData: FeatureData): Promise<any>; /** * Update feature information * @param featureId - Feature identifier * @param updateData - Data to update * @returns Promise<any> - Updated feature object */ updateFeature(featureId: string, updateData: Partial<FeatureData>): Promise<any>; /** * Delete feature from the system * @param featureId - Feature identifier * @returns Promise<void> */ deleteFeature(featureId: string): Promise<void>; /** * Get all permissions in the system * @returns Promise with permissions array and total count */ getAllPermissions(): Promise<{ items: any[]; total: number; }>; /** * Find permission by name * @param permissionName - Permission name * @returns Promise<any | null> - Permission object or null if not found */ findPermissionByName(permissionName: string): Promise<any | null>; /** * Find permission by ID * @param permissionId - Permission identifier * @returns Promise<any | null> - Permission object or null if not found */ findPermissionById(permissionId: string): Promise<any | null>; /** * Create a new permission * @param permissionData - Permission data object * @returns Promise<any> - Created permission object */ createPermission(permissionData: PermissionData): Promise<any>; /** * Update permission information * @param permissionId - Permission identifier * @param updateData - Data to update * @returns Promise<any> - Updated permission object */ updatePermission(permissionId: string, updateData: Partial<PermissionData>): Promise<any>; /** * Delete permission from the system * @param permissionId - Permission identifier * @returns Promise<void> */ deletePermission(permissionId: string): Promise<void>; /** * Get the underlying database adapter for advanced operations * @returns DatabaseAdapter - The database adapter instance */ getDbAdapter(): DatabaseAdapter; /** * Check if the admin service is properly configured and initialized * @returns boolean - True if service is ready */ isReady(): boolean; }