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

206 lines (205 loc) 6.81 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var RbacAdminModule_1; Object.defineProperty(exports, "__esModule", { value: true }); exports.RbacAdminModule = void 0; const common_1 = require("@nestjs/common"); const admin_controller_1 = require("./admin.controller"); const admin_service_1 = require("./admin.service"); const admin_auth_guard_1 = require("./guards/admin-auth.guard"); /** * NestJS Admin Dashboard Module for RBAC System * * Provides a complete web-based admin interface for managing RBAC entities. * Includes session-based authentication, user management, role management, * feature management, and permission management. * * Features: * - Beautiful web-based admin dashboard * - Session-based authentication with configurable credentials * - User management with pagination and search * - Role and permission management * - Feature management * - Real-time dashboard statistics * - Multi-database support (MongoDB/PostgreSQL) * * @example * ```typescript * // STEP 1: Setup session middleware in main.ts (REQUIRED!) * // main.ts * import { NestFactory } from '@nestjs/core'; * import { AppModule } from './app.module'; * import * as session from 'express-session'; * * async function bootstrap() { * const app = await NestFactory.create(AppModule); * * // REQUIRED: Setup session middleware for admin dashboard * app.use( * session({ * secret: 'your-session-secret-key-here', * resave: false, * saveUninitialized: false, * cookie: { * maxAge: 24 * 60 * 60 * 1000, // 24 hours * httpOnly: true, * secure: false // Set to true in production with HTTPS * } * }) * ); * * await app.listen(3000); * } * bootstrap(); * * // STEP 2: Setup modules in app.module.ts * @Module({ * imports: [ * // 1. First import the main RBAC module (REQUIRED) * RbacModule.forRoot({ * global: true, // Make it global so admin module can access providers * database: { * type: 'mongodb', * connection: mongooseConnection * }, * authAdapter: async (req) => ({ user_id: req.user.id }), * defaultRole: 'user' * }), * * // 2. Then import the admin dashboard module * RbacAdminModule.forRoot({ * adminCredentials: { * username: 'admin', * password: 'secure-password-123' * }, * sessionSecret: 'your-session-secret-key-here' // Same as main.ts * }) * ], * controllers: [AppController], * providers: [AppService] * }) * export class AppModule {} * ``` * * @example * ```typescript * // Async configuration with environment variables * @Module({ * imports: [ * // 1. Main RBAC module first (REQUIRED) * RbacModule.forRoot({ * global: true, // Make it global for admin module access * database: { * type: 'postgresql', * connection: pgPool * }, * authAdapter: async (req) => ({ user_id: req.user.id }), * defaultRole: 'user' * }), * * // 2. Admin module second * RbacAdminModule.forRootAsync({ * useFactory: async (configService: ConfigService) => ({ * adminCredentials: { * username: configService.get('ADMIN_USERNAME', 'admin'), * password: configService.get('ADMIN_PASSWORD') * }, * sessionSecret: configService.get('SESSION_SECRET'), * sessionOptions: { * name: 'rbac.admin.sid', * maxAge: 24 * 60 * 60 * 1000, // 24 hours * secure: configService.get('NODE_ENV') === 'production', * httpOnly: true * } * }), * inject: [ConfigService] * }) * ] * }) * export class AppModule {} * ``` * * @example * ```typescript * // Access admin service in your own services * @Injectable() * export class MyCustomAdminService { * constructor(private rbacAdminService: RbacAdminService) {} * * async getAdminDashboardData() { * const stats = await this.rbacAdminService.getDashboardStats(); * const users = await this.rbacAdminService.getAllUsers(10, 0, ''); * * return { * statistics: stats, * recentUsers: users.items * }; * } * * async bulkUserOperations(userIds: string[], operation: string) { * for (const userId of userIds) { * if (operation === 'delete') { * await this.rbacAdminService.deleteUser(userId); * } * // Add more bulk operations as needed * } * } * } * ``` */ let RbacAdminModule = RbacAdminModule_1 = class RbacAdminModule { /** * Configure the RBAC Admin Module with static options * * @param options - Configuration options for admin dashboard * @returns DynamicModule - Configured module */ static forRoot(options) { const providers = [ { provide: 'RBAC_ADMIN_CONFIG', useValue: options }, admin_service_1.RbacAdminService, admin_auth_guard_1.AdminAuthGuard ]; return { module: RbacAdminModule_1, controllers: [admin_controller_1.RbacAdminController], providers, exports: [admin_service_1.RbacAdminService, admin_auth_guard_1.AdminAuthGuard] }; } /** * Configure the RBAC Admin Module with async options * * @param options - Async configuration options * @returns DynamicModule - Configured module */ static forRootAsync(options) { const providers = [ { provide: 'RBAC_ADMIN_CONFIG', useFactory: options.useFactory, inject: options.inject || [] }, admin_service_1.RbacAdminService, admin_auth_guard_1.AdminAuthGuard ]; return { module: RbacAdminModule_1, controllers: [admin_controller_1.RbacAdminController], providers, exports: [admin_service_1.RbacAdminService, admin_auth_guard_1.AdminAuthGuard] }; } }; exports.RbacAdminModule = RbacAdminModule; exports.RbacAdminModule = RbacAdminModule = RbacAdminModule_1 = __decorate([ (0, common_1.Module)({}) ], RbacAdminModule);