A lightweight, extensible, and efficient Role-Based Access Control (RBAC) system implemented in Node.js, designed to manage user roles and permissions effectively.
- Define and manage roles and their associated permissions.
- Easily assign roles to users.
- Perform authorization checks to control access to resources.
- Support for hierarchical roles.
- Lightweight and simple to integrate into any Node.js project.
Before using this RBAC system, ensure you have:
- Node.js (v14 or later)
- npm or yarn
- Clone this repository:
git clone https://github.com/EslamYasser-Dev/RBAC.git
- Navigate to the project directory:
cd RBAC
- Install dependencies:
npm install
Define roles and their permissions in the roles.json
file or any database you prefer.
Example roles.json
:
{
"admin": ["create_user", "delete_user", "update_user"],
"editor": ["update_content", "delete_content"],
"viewer": ["read_content"]
}
-
Import and initialize the RBAC system:
const RBAC = require('./rbac'); const roles = require('./roles.json'); // Load roles from JSON or database const rbac = new RBAC(roles);
-
Assign roles to users:
const userRoles = { user1: ['admin'], user2: ['editor'], };
-
Perform access checks:
const hasAccess = rbac.can('user1', 'delete_user'); console.log(`Access granted: ${hasAccess}`);
rbac.can('user2', 'delete_user')
.then((access) => {
if (access) {
console.log('Access granted!');
} else {
console.log('Access denied.');
}
})
.catch(err => console.error(err));
You can dynamically add new roles and permissions using:
rbac.addRole('new_role', ['new_permission']);
Contributions are welcome! To contribute:
- Fork the repository.
- Create a new branch for your feature or bug fix.
- Submit a pull request.
This project is licensed under the MIT License.
If you discover any security vulnerabilities, please contact EslamYasser-Dev directly.
Special thanks to the open-source community for their support and contributions.