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

152 lines (151 loc) 4.89 kB
import { RbacModuleOptions } from './rbac.module'; /** * NestJS service that provides RBAC functionality. * Wraps the main RBAC system methods for use in NestJS applications. * * @example * ```typescript * @Injectable() * export class UserService { * constructor(private rbacService: RbacService) {} * * async createUser(userData: CreateUserDto) { * // Create user in your main database * const user = await this.userRepository.create(userData); * * // Register user in RBAC system * await this.rbacService.registerUser(user.id, { * name: user.name, * email: user.email * }); * * return user; * } * * async assignRole(userId: string, roleName: string) { * await this.rbacService.assignRole(userId, roleName); * } * * async checkUserPermissions(userId: string, feature: string) { * return await this.rbacService.getFeaturePermissions(userId, feature); * } * } * ``` */ export declare class RbacService { private config; private initialized; constructor(config: RbacModuleOptions, initialized: boolean); /** * Manually register a user in the RBAC system. * Useful for programmatic user registration outside of HTTP requests. * * @param user_id - Unique identifier for the user * @param userData - User data object * @returns Promise that resolves when user is registered * @throws Error if user already exists or registration fails * * @example * ```typescript * await this.rbacService.registerUser('user123', { * name: 'John Doe', * email: 'john@example.com' * }); * ``` */ registerUser(user_id: string, userData: { name?: string; email?: string; }): Promise<void>; /** * Update user information in the RBAC system. * * @param user_id - Unique identifier for the user * @param userData - User data to update * @returns Promise that resolves when user is updated * @throws Error if user is not found * * @example * ```typescript * await this.rbacService.updateUser('user123', { * name: 'John Smith', * email: 'johnsmith@example.com' * }); * ``` */ updateUser(user_id: string, userData: { name?: string; email?: string; }): Promise<void>; /** * Assign a role to a user in the RBAC system. * * @param user_id - Unique identifier for the user * @param roleName - Name of the role to assign * @returns Promise that resolves when role is assigned * @throws Error if user or role is not found * * @example * ```typescript * await this.rbacService.assignRole('user123', 'admin'); * ``` */ assignRole(user_id: string, roleName: string): Promise<void>; /** * Get the role name assigned to a user. * * @param user_id - Unique identifier for the user * @returns Promise that resolves to the role name or null if no role assigned * * @example * ```typescript * const role = await this.rbacService.getUserRole('user123'); * console.log(role); // 'admin' or null * ``` */ getUserRole(user_id: string): Promise<string | null>; /** * Get all permissions a user has for a specific feature. * * @param user_id - Unique identifier for the user * @param featureName - Name of the feature to check permissions for * @returns Promise that resolves to an array of permission names * * @example * ```typescript * const permissions = await this.rbacService.getFeaturePermissions('user123', 'billing'); * console.log(permissions); // ['read', 'create', 'update'] * ``` */ getFeaturePermissions(user_id: string, featureName: string): Promise<string[]>; /** * Check if a user has a specific permission for a feature. * * @param user_id - Unique identifier for the user * @param feature - Feature name * @param permission - Permission name * @returns Promise that resolves to boolean indicating if user has permission * * @example * ```typescript * const canDelete = await this.rbacService.hasPermission('user123', 'billing', 'delete'); * if (canDelete) { * // User can delete billing records * } * ``` */ hasPermission(user_id: string, feature: string, permission: string): Promise<boolean>; /** * Get access to the underlying RBAC controllers for advanced operations. * * @returns Object containing controller instances * * @example * ```typescript * const { userRole, feature } = this.rbacService.getControllers(); * const allRoles = await userRole.getAllRoles(); * const allFeatures = await feature.getAllFeatures(); * ``` */ getControllers(): void; }