| Crates.io | rbac-rs |
| lib.rs | rbac-rs |
| version | 0.1.2 |
| created_at | 2025-01-30 12:44:28.23276+00 |
| updated_at | 2025-01-30 14:15:24.516129+00 |
| description | A pluggable RBAC system for Rust using SQLx. |
| homepage | |
| repository | https://github.com/JafrulTripto/permissions-rs.git |
| max_upload_size | |
| id | 1536327 |
| size | 59,653 |
A pluggable RBAC (Role-Based Access Control) system built in Rust using SQLx.
This crate provides a simple, flexible RBAC system that integrates with a PostgreSQL database. It is designed to be easily integrated into existing applications.
Run the following SQL commands to set up the required tables:
CREATE TABLE roles (
id UUID PRIMARY KEY,
name TEXT NOT NULL UNIQUE
);
CREATE TABLE permissions (
id UUID PRIMARY KEY,
name TEXT NOT NULL UNIQUE
);
CREATE TABLE user_roles (
user_id UUID NOT NULL,
role_id UUID NOT NULL,
PRIMARY KEY (user_id, role_id),
FOREIGN KEY (role_id) REFERENCES roles(id) ON DELETE CASCADE
);
CREATE TABLE role_permissions (
role_id UUID NOT NULL,
permission_id UUID NOT NULL,
PRIMARY KEY (role_id, permission_id),
FOREIGN KEY (role_id) REFERENCES roles(id) ON DELETE CASCADE,
FOREIGN KEY (permission_id) REFERENCES permissions(id) ON DELETE CASCADE
);
Add the following to your Cargo.toml file:
[dependencies]
permissions-rs = { git = "https://github.com/JafrulTripto/permissions-rs.git" }
use permissions_rs::repository::RbacRepository;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let repo = RbacRepository::new("postgres://username:password@localhost/db_name").await?;
Ok(())
}
use permissions_rs::service::RbacService;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let service = RbacService::new("postgres://username:password@localhost/db_name").await?;
// Assign a role to a user
service.assign_role(user_id, role_id).await?;
// Check if a user has a specific permission
let has_permission = service.check_permission(user_id, "read_access").await?;
println!("User has permission: {}", has_permission);
Ok(())
}
This library supports both UUID and incremental IDs for entity identifiers. The identifier type can be switched by enabling the appropriate feature in your Cargo.toml file:
[features]
default = ["uuid-id"]
incremental-id = []
uuid-id = []
Set the database connection URL and enable the desired features in your Cargo.toml file. For example:
[dependencies]
permissions-rs = { git = "https://github.com/JafrulTripto/permissions-rs.git" }
[features]
default = ["uuid-id"]
uuid-id = []
incremental-id = []
This project is licensed under the MIT License - see the LICENSE file for details.