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

176 lines (175 loc) 6.05 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.featureController = void 0; const Feature_1 = require("../models/Feature"); /** * Retrieves all features from the database. * Features represent application modules or functionalities that can be assigned to roles. * * @returns {Promise<{message: string, features: any[]} | {error: string}>} * Success response with features array or error response * * @example * ```typescript * const { featureController } = RBAC.controllers; * const result = await featureController.getAllFeatures(); * * if (result.error) { * console.error('Failed to fetch features:', result.error); * } else { * console.log('Features:', result.features); * // result.features = [{ _id: '...', name: 'billing', description: '...' }, ...] * } * ``` */ const getAllFeatures = () => __awaiter(void 0, void 0, void 0, function* () { try { const features = yield Feature_1.Feature.find().exec(); return { message: "Features fetched successfully", features }; } catch (error) { return { error: "Internal server error" }; } }); /** * Creates a new feature in the RBAC system. * Features represent application modules that can be assigned to roles with specific permissions. * * @param {string} name - Unique name for the feature (e.g., 'billing', 'user-management') * @param {string} description - Human-readable description of the feature * @returns {Promise<{message: string, feature: any} | {error: string}>} * Success response with created feature or error response * * @example * ```typescript * const { featureController } = RBAC.controllers; * * const result = await featureController.createFeature( * 'billing', * 'Billing and payment management system' * ); * * if (result.error) { * console.error('Failed to create feature:', result.error); * } else { * console.log('Feature created:', result.feature); * // result.feature = { _id: '...', name: 'billing', description: '...', createdAt: '...' } * } * ``` */ const createFeature = (name, description) => __awaiter(void 0, void 0, void 0, function* () { try { const feature = new Feature_1.Feature({ name, description, }); yield feature.save(); return { message: "Feature created successfully", feature }; } catch (error) { return { error: "Internal server error" }; } }); /** * Updates an existing feature's information. * Currently returns a placeholder response - implementation needed. * * @param {string} featureId - MongoDB ObjectId of the feature to update * @param {string} name - New name for the feature * @param {string} description - New description for the feature * @returns {Promise<{message: string} | {error: string}>} * Success message or error response * * @example * ```typescript * const { featureController } = RBAC.controllers; * * const result = await featureController.updateFeature( * '507f1f77bcf86cd799439011', * 'billing-advanced', * 'Advanced billing and payment management' * ); * * if (result.error) { * console.error('Failed to update feature:', result.error); * } else { * console.log(result.message); // 'Feature updated successfully' * } * ``` * * @todo Implement actual feature update logic */ const updateFeature = (featureId, name, description) => __awaiter(void 0, void 0, void 0, function* () { try { return { message: "Feature updated successfully" }; } catch (error) { return { error: "Internal server error" }; } }); /** * Deletes a feature from the RBAC system. * Currently returns a placeholder response - implementation needed. * * @param {string} featureId - MongoDB ObjectId of the feature to delete * @returns {Promise<{message: string} | {error: string}>} * Success message or error response * * @example * ```typescript * const { featureController } = RBAC.controllers; * * const result = await featureController.deleteFeature('507f1f77bcf86cd799439011'); * * if (result.error) { * console.error('Failed to delete feature:', result.error); * } else { * console.log(result.message); // 'Feature deleted successfully' * } * ``` * * @warning Deleting a feature may affect existing roles that reference it. * Consider checking for dependencies before deletion. * @todo Implement actual feature deletion logic with dependency checks */ const deleteFeature = (featureId) => __awaiter(void 0, void 0, void 0, function* () { try { return { message: "Feature deleted successfully" }; } catch (error) { return { error: "Internal server error" }; } }); /** * Feature controller providing CRUD operations for RBAC features. * Features represent application modules or functionalities that can be assigned to roles. * * @namespace featureController * * @example * ```typescript * import { RBAC } from '@sheikh295/rbac'; * const { featureController } = RBAC.controllers; * * // Get all features * const { features } = await featureController.getAllFeatures(); * * // Create a new feature * await featureController.createFeature('reports', 'Reporting system'); * ``` */ exports.featureController = { getAllFeatures, createFeature, updateFeature, deleteFeature, };