Lesson 5 of 10
RBAC & ABAC Access Control Models
Access control models define how permissions are structured and enforced. Choosing the right model determines how well your system can express and enforce the business rules governing data access.
Role-Based Access Control (RBAC)
# RBAC: Permissions assigned to roles; users assigned to roles
# Users gain permissions by being assigned roles
# Database schema
CREATE TABLE roles (id INT, name VARCHAR, description TEXT);
CREATE TABLE permissions (id INT, name VARCHAR, resource VARCHAR, action VARCHAR);
CREATE TABLE role_permissions (role_id INT, permission_id INT);
CREATE TABLE user_roles (user_id INT, role_id INT);
# Python RBAC enforcement example
class RBACMiddleware:
PERMISSIONS = {
'admin': ['users:read', 'users:write', 'users:delete',
'reports:read', 'settings:write'],
'manager': ['users:read', 'reports:read'],
'user': ['profile:read', 'profile:write'],
'auditor': ['users:read', 'reports:read', 'logs:read'],
}
def check_permission(self, user_roles: list, required_permission: str) -> bool:
for role in user_roles:
if required_permission in self.PERMISSIONS.get(role, []):
return True
return False
# Usage in Flask
from functools import wraps
def require_permission(permission):
def decorator(f):
@wraps(f)
def wrapper(*args, **kwargs):
user_roles = get_current_user_roles() # From JWT or session
rbac = RBACMiddleware()
if not rbac.check_permission(user_roles, permission):
abort(403, f"Permission denied: {permission} required")
return f(*args, **kwargs)
return wrapper
return decorator
@app.route('/admin/delete-user/<int:user_id>', methods=['DELETE'])
@require_permission('users:delete')
def delete_user(user_id):
# Only executes if current user has 'users:delete' permission
...Attribute-Based Access Control (ABAC)
# ABAC: Fine-grained access based on attributes of user, resource, and environment
# More flexible than RBAC - can express complex business rules
# Example: "Doctors can access patient records only if they are the treating physician
# OR it is an emergency AND after-hours access is permitted AND
# the patient has not opted out of emergency access"
class ABACEngine:
def check_access(self, subject: dict, resource: dict,
action: str, environment: dict) -> bool:
# Patient record access policy
if resource.get('type') == 'patient_record':
# Rule 1: Treating physician has full access
if subject.get('role') == 'doctor' and \
resource.get('treating_doctor_id') == subject.get('user_id'):
return True
# Rule 2: Admin always has read access
if subject.get('role') == 'admin' and action == 'read':
return True
# Rule 3: Emergency access (doctor + emergency flag + no opt-out)
if (subject.get('role') == 'doctor' and
environment.get('is_emergency') and
not resource.get('patient_opted_out_of_emergency')):
self.log_emergency_access(subject, resource)
return True
return False # Default deny
return False # Deny anything not explicitly handled
# ABAC vs RBAC:
# RBAC: simpler, sufficient for most applications
# ABAC: when fine-grained context-aware policies are needed
# (healthcare, finance, government compliance)
# Many real systems use RBAC as the base with ABAC extensions for edge casesCommon Mistakes
- Implementing RBAC with roles defined in client-side JWT without server verification - clients can set their own role claim if the server trusts the claim without re-verifying
- Role explosion in RBAC - creating hundreds of highly specific roles instead of using parameterized permissions; leads to unmanageable access control
- Default allow instead of default deny - access control should deny everything by default and explicitly grant permissions; not grant everything and explicitly deny
Tip
Tip
Practice RBAC ABAC Access Control Models in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
The foundational security model: Confidentiality (privacy), Integrity (trust), and Availability (uptime).
Practice Task
Note
Practice Task - (1) Write a working example of RBAC ABAC Access Control Models from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with RBAC ABAC Access Control Models is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready cybersecurity code.
Key Takeaways
- Access control models define how permissions are structured and enforced.
- Implementing RBAC with roles defined in client-side JWT without server verification - clients can set their own role claim if the server trusts the claim without re-verifying
- Role explosion in RBAC - creating hundreds of highly specific roles instead of using parameterized permissions; leads to unmanageable access control
- Default allow instead of default deny - access control should deny everything by default and explicitly grant permissions; not grant everything and explicitly deny
Finished this lesson?