light-rbac 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,31 @@
1
+ Metadata-Version: 2.4
2
+ Name: light-rbac
3
+ Version: 0.1.0
4
+ Summary: A lightweight policy-based RBAC and ABAC engine.
5
+ Author: Sajan Nepali
6
+ Requires-Python: >=3.8
7
+ Description-Content-Type: text/markdown
8
+
9
+ # RBAC Engine
10
+
11
+ A lightweight policy-based Role-Based Access Control (RBAC) engine for Python.
12
+
13
+ ## Installation
14
+
15
+ ```bash
16
+ pip install rbac
17
+ from rbac import PolicyEngine
18
+
19
+ # Initialize the engine
20
+ engine = PolicyEngine()
21
+
22
+ # Add roles and permissions
23
+ engine.add_role("admin", ["*"])
24
+ engine.add_role("editor", ["read", "write"])
25
+
26
+ # Assign roles to users
27
+ engine.assign_role("user_123", "editor")
28
+
29
+ # Evaluate permissions
30
+ print(engine.evaluate("user_123", "write")) # True
31
+ print(engine.evaluate("user_123", "delete")) # False
@@ -0,0 +1,6 @@
1
+ rbac/__init__.py,sha256=xK8_klZF1BHglmGd6y2gCbrdyKqt49HO_XNNktenPl4,62
2
+ rbac/engine.py,sha256=wqHpzM_SYJHZt1iyKPdmvHKRh_Hlb_Ksuo7r9HdxJaQ,1265
3
+ light_rbac-0.1.0.dist-info/METADATA,sha256=HAAO2od0pWCIDP6XHv39NS4I8RCY1sDp83waHcX_znU,745
4
+ light_rbac-0.1.0.dist-info/WHEEL,sha256=K260EYznzXsJYBQGqmI8VTxEdiZYNvDZwW9cBh9-_MA,91
5
+ light_rbac-0.1.0.dist-info/top_level.txt,sha256=cHi0ADVWo1BPO_6kRmLuhO7mlDatvK2T6Qx1GpkM1A8,5
6
+ light_rbac-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (83.0.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1 @@
1
+ rbac
rbac/__init__.py ADDED
@@ -0,0 +1,3 @@
1
+ from .engine import PolicyEngine
2
+
3
+ __all__ = ["PolicyEngine"]
rbac/engine.py ADDED
@@ -0,0 +1,34 @@
1
+ from typing import Dict, Set, Optional
2
+
3
+ class PolicyEngine:
4
+ def __init__(self):
5
+ # Maps role_name -> set of permitted actions/permissions
6
+ self.roles: Dict[str, Set[str]] = {}
7
+
8
+ # Maps user_id -> set of assigned roles
9
+ self.user_roles: Dict[str, Set[str]] = {}
10
+
11
+ def add_role(self, role: str, permissions: list[str]) -> None:
12
+ """Define a role and its associated permissions."""
13
+ self.roles[role] = set(permissions)
14
+
15
+ def assign_role(self, user_id: str, role: str) -> None:
16
+ """Assign a role to a user."""
17
+ if role not in self.roles:
18
+ raise ValueError(f"Role '{role}' does not exist.")
19
+ if user_id not in self.user_roles:
20
+ self.user_roles[user_id] = set()
21
+ self.user_roles[user_id].add(role)
22
+
23
+ def evaluate(self, user_id: str, action: str) -> bool:
24
+ """
25
+ Evaluate whether a user has a role containing the required action/permission.
26
+ """
27
+ user_assigned_roles = self.user_roles.get(user_id, set())
28
+
29
+ for role in user_assigned_roles:
30
+ role_perms = self.roles.get(role, set())
31
+ if action in role_perms or "*" in role_perms:
32
+ return True
33
+
34
+ return False