163 lines
6.4 KiB
Python
163 lines
6.4 KiB
Python
"""Role-Based Access Control (RBAC) & Segregation of Duties (SoD) Engine.
|
|
|
|
Fulfills FR-AUTH-002 (Role-Based Authorization), BRD-FR-040 & LLD-001.
|
|
Enforces permissions across all 11 PRD roles, 7 discrete actions,
|
|
and strictly enforces mandatory segregation of duties before business logic execution.
|
|
"""
|
|
from enum import Enum
|
|
from typing import Any, Dict, List, Optional, Set
|
|
|
|
|
|
class Action(str, Enum):
|
|
READ = "Read"
|
|
WRITE = "Write"
|
|
CREATE = "Create"
|
|
DELETE = "Delete"
|
|
SUBMIT = "Submit"
|
|
CANCEL = "Cancel"
|
|
AMEND = "Amend"
|
|
|
|
|
|
# All 11 PRD Roles from BRD-FR-040
|
|
PRD_ROLES = {
|
|
"Tenant Administrator",
|
|
"Mandi Buyer",
|
|
"Purchase Manager",
|
|
"Yard Supervisor",
|
|
"Quality Officer",
|
|
"Sales Executive",
|
|
"Sales Manager",
|
|
"Logistics Coordinator",
|
|
"Accounts Executive",
|
|
"Partner / MD",
|
|
"System Administrator",
|
|
}
|
|
|
|
# Role permission matrix mapping roles to allowed (resource, action) pairs
|
|
# Resources are domain aggregates: 'market', 'pricing', 'procurement', 'receiving',
|
|
# 'quality', 'inventory', 'sales', 'sales_confirm', 'credit_override', 'logistics',
|
|
# 'finance', 'tenant_mgmt', 'platform'
|
|
ROLE_PERMISSIONS: Dict[str, Set[str]] = {
|
|
"Tenant Administrator": {
|
|
"tenant_mgmt:*", "market:Read", "procurement:Read", "sales:Read",
|
|
"finance:Read", "quality:Read", "inventory:Read"
|
|
},
|
|
"Mandi Buyer": {
|
|
"market:Read", "market:Create", "market:Write",
|
|
"procurement:Read", "procurement:Create", "procurement:Submit"
|
|
},
|
|
"Purchase Manager": {
|
|
"market:Read", "pricing:Read", "pricing:Write",
|
|
"procurement:Read", "procurement:Create", "procurement:Write",
|
|
"procurement:Submit", "procurement:Amend", "procurement:Cancel"
|
|
},
|
|
"Yard Supervisor": {
|
|
"receiving:Read", "receiving:Create", "receiving:Write", "receiving:Submit",
|
|
"inventory:Read", "inventory:Write"
|
|
},
|
|
"Quality Officer": {
|
|
"receiving:Read", "quality:Read", "quality:Create", "quality:Write",
|
|
"quality:Submit", "inventory:Read"
|
|
},
|
|
"Sales Executive": {
|
|
"market:Read", "inventory:Read", "sales:Read", "sales:Create", "sales:Write",
|
|
"sales:Submit"
|
|
},
|
|
"Sales Manager": {
|
|
"market:Read", "inventory:Read", "pricing:Read", "pricing:Write",
|
|
"sales:Read", "sales:Create", "sales:Write", "sales:Submit",
|
|
"sales:Amend", "sales:Cancel", "sales_confirm:Create"
|
|
},
|
|
"Logistics Coordinator": {
|
|
"inventory:Read", "logistics:Read", "logistics:Create", "logistics:Write",
|
|
"logistics:Submit"
|
|
},
|
|
"Accounts Executive": {
|
|
"market:Read", "procurement:Read", "sales:Read", "finance:Read",
|
|
"finance:Create", "finance:Write", "finance:Submit"
|
|
},
|
|
"Partner / MD": {
|
|
"market:*", "pricing:*", "procurement:*", "sales:*", "sales_confirm:*",
|
|
"credit_override:*", "finance:*", "inventory:*", "quality:*", "logistics:*",
|
|
"tenant_mgmt:Read"
|
|
},
|
|
"System Administrator": {
|
|
"platform:*", "tenant_mgmt:Read", "tenant_mgmt:Create"
|
|
}
|
|
}
|
|
|
|
|
|
class PermissionDeniedError(Exception):
|
|
"""Raised when an operation is prohibited by role policy or segregation of duties."""
|
|
def __init__(self, message: str, code: str = "PERMISSION_DENIED"):
|
|
super().__init__(message)
|
|
self.message = message
|
|
self.code = code
|
|
|
|
|
|
class RBACPolicyEngine:
|
|
"""Evaluates role permissions and segregation of duties."""
|
|
|
|
def is_permitted(self, roles: List[str], resource: str, action: str,
|
|
context: Optional[Dict[str, Any]] = None) -> bool:
|
|
"""Determines if the given roles allow the requested action on the resource."""
|
|
if not roles:
|
|
return False
|
|
|
|
# Enforce Segregation of Duties (SoD) Invariants (BRD-FR-040)
|
|
self.verify_segregation_of_duties(roles, resource, action)
|
|
|
|
# Check permission matrix
|
|
for role in roles:
|
|
allowed_rules = ROLE_PERMISSIONS.get(role, set())
|
|
# Wildcard match (e.g. 'sales:*' or 'platform:*')
|
|
if f"{resource}:*" in allowed_rules or "*:*" in allowed_rules:
|
|
return True
|
|
# Specific match (e.g. 'sales:Create')
|
|
if f"{resource}:{action}" in allowed_rules:
|
|
return True
|
|
|
|
return False
|
|
|
|
def verify_segregation_of_duties(self, roles: List[str], resource: str, action: str) -> None:
|
|
"""Hard enforcement of PRD Segregation of Duties rules.
|
|
|
|
Raises PermissionDeniedError immediately if a segregation rule is violated.
|
|
"""
|
|
role_set = set(roles)
|
|
|
|
# SoD Rule 1: Quality Officers cannot edit prices (pricing:Write, pricing:Create, etc.)
|
|
if "Quality Officer" in role_set and resource in ("pricing", "market_rates") and action in ("Write", "Create", "Amend"):
|
|
raise PermissionDeniedError(
|
|
"Segregation of Duties Violation: Quality Officers are prohibited from modifying prices.",
|
|
code="SOD_PRICE_EDIT_FORBIDDEN"
|
|
)
|
|
|
|
# SoD Rule 2: Yard Supervisors cannot confirm sales contracts (sales_confirm)
|
|
if "Yard Supervisor" in role_set and resource in ("sales_confirm", "sales") and action in ("Confirm", "Submit", "Create"):
|
|
if resource == "sales_confirm" or (resource == "sales" and action == "Confirm"):
|
|
raise PermissionDeniedError(
|
|
"Segregation of Duties Violation: Yard Supervisors are prohibited from confirming sales contracts.",
|
|
code="SOD_SALES_CONFIRM_FORBIDDEN"
|
|
)
|
|
|
|
# SoD Rule 3: Sales Executives cannot override customer credit locks (credit_override)
|
|
if "Sales Executive" in role_set and resource in ("credit_override", "credit_lock"):
|
|
raise PermissionDeniedError(
|
|
"Segregation of Duties Violation: Sales Executives are prohibited from overriding credit locks.",
|
|
code="SOD_CREDIT_OVERRIDE_FORBIDDEN"
|
|
)
|
|
|
|
def enforce_permission(self, roles: List[str], resource: str, action: str,
|
|
context: Optional[Dict[str, Any]] = None) -> None:
|
|
"""Enforces permission, raising PermissionDeniedError on denial."""
|
|
if not self.is_permitted(roles, resource, action, context):
|
|
raise PermissionDeniedError(
|
|
f"Access denied: Roles {roles} do not have permission '{action}' on resource '{resource}'.",
|
|
code="INSUFFICIENT_PERMISSIONS"
|
|
)
|
|
|
|
|
|
rbac_engine = RBACPolicyEngine()
|
|
|