@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
180 lines (179 loc) • 6.69 kB
JavaScript
"use strict";
/**
* @fileoverview Core RBAC functionality without Express dependencies
*
* This module provides the core RBAC functionality that works with any framework,
* including the database operations and user management without Express middleware.
*/
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.CoreRBAC = void 0;
const MongoAdapter_1 = require("./adapters/MongoAdapter");
const PostgresAdapter_1 = require("./adapters/PostgresAdapter");
/**
* Core RBAC system without Express dependencies.
* Provides database operations and user management for any framework.
*/
class CoreRBACSystem {
constructor() {
this._config = null;
this._initialized = false;
this._dbAdapter = null;
}
/**
* Initialize the RBAC system with the provided configuration.
*/
init(config) {
return __awaiter(this, void 0, void 0, function* () {
this._config = config;
// Handle legacy configuration format
if (config.db && !config.database) {
config.database = {
type: 'mongodb',
connection: config.db
};
}
// Initialize database adapter based on configuration
if (config.database) {
switch (config.database.type) {
case 'mongodb':
this._dbAdapter = new MongoAdapter_1.MongoAdapter(config.database.connection);
break;
case 'postgresql':
this._dbAdapter = new PostgresAdapter_1.PostgresAdapter(config.database.connection);
break;
default:
throw new Error(`Unsupported database type: ${config.database.type}`);
}
yield this._dbAdapter.init();
}
else {
throw new Error("Database configuration is required. Please provide either 'database' or 'db' in config.");
}
this._initialized = true;
});
}
ensureInitialized() {
if (!this._initialized || !this._config || !this._dbAdapter) {
throw new Error("RBAC system not initialized. Call CoreRBAC.init(config) first.");
}
}
/**
* Manually register a user in the RBAC system.
*/
registerUserManual(user_id, userData) {
return __awaiter(this, void 0, void 0, function* () {
this.ensureInitialized();
const existingUser = yield this._dbAdapter.findUserByUserId(user_id);
if (existingUser) {
throw new Error("User already exists");
}
let defaultRoleId = undefined;
if (this._config.defaultRole) {
const role = yield this._dbAdapter.findRoleByName(this._config.defaultRole);
if (role) {
defaultRoleId = role.id;
}
}
yield this._dbAdapter.createUser({
user_id,
name: userData.name || "",
email: userData.email || "",
role_id: defaultRoleId,
});
if (this._config.onUserRegister) {
yield this._config.onUserRegister(Object.assign({ user_id }, userData));
}
});
}
/**
* Update user information in the RBAC system.
*/
updateUser(user_id, userData) {
return __awaiter(this, void 0, void 0, function* () {
this.ensureInitialized();
const user = yield this._dbAdapter.findUserByUserId(user_id);
if (!user) {
throw new Error("User not found");
}
const updates = {};
if (userData.name !== undefined)
updates.name = userData.name;
if (userData.email !== undefined)
updates.email = userData.email;
yield this._dbAdapter.updateUser(user_id, updates);
});
}
/**
* Assign a role to a user in the RBAC system.
*/
assignRole(user_id, roleName) {
return __awaiter(this, void 0, void 0, function* () {
this.ensureInitialized();
const user = yield this._dbAdapter.findUserByUserId(user_id);
if (!user) {
throw new Error("User not found");
}
const role = yield this._dbAdapter.findRoleByName(roleName);
if (!role) {
throw new Error("Role not found");
}
yield this._dbAdapter.updateUser(user_id, { role_id: role.id });
if (this._config.onRoleUpdate) {
yield this._config.onRoleUpdate({ user_id, role: roleName });
}
});
}
/**
* Get the role name assigned to a user.
*/
getUserRole(user_id) {
return __awaiter(this, void 0, void 0, function* () {
this.ensureInitialized();
const user = yield this._dbAdapter.findUserByUserIdWithRole(user_id);
if (!user || !user.role) {
return null;
}
return user.role.name;
});
}
/**
* Get all permissions a user has for a specific feature.
*/
getFeaturePermissions(user_id, featureName) {
return __awaiter(this, void 0, void 0, function* () {
this.ensureInitialized();
return yield this._dbAdapter.getUserFeaturePermissions(user_id, featureName);
});
}
/**
* Get access to the database adapter for advanced operations.
*/
get dbAdapter() {
return this._dbAdapter;
}
/**
* Get configuration
*/
get config() {
return this._config;
}
/**
* Check if system is initialized
*/
get initialized() {
return this._initialized;
}
}
/**
* Core RBAC instance for framework-agnostic usage
*/
exports.CoreRBAC = new CoreRBACSystem();