Principle:APIJSON APIJSON Demo Role Based Access Control
| Knowledge Sources | |
|---|---|
| Domains | Security, Authorization, API_Design |
| Last Updated | 2026-10-14 00:00 GMT |
Overview
Security model that restricts API operations on database tables based on the caller's assigned role, enforced through annotation-based declarations and a dynamic database-backed Access table.
Description
Role-Based Access Control (RBAC) in APIJSON assigns each user a role (UNKNOWN, LOGIN, CONTACT, CIRCLE, OWNER, ADMIN) and restricts which HTTP methods (GET, HEAD, GETS, HEADS, POST, PUT, DELETE) they may invoke on each table. Roles form a hierarchy: UNKNOWN < LOGIN < CONTACT < CIRCLE < OWNER < ADMIN, where higher roles inherit lower permissions.
This solves two problems:
- Unauthorized access: Without RBAC, any authenticated user could read or modify any table. RBAC ensures only permitted roles can perform each operation.
- Permission management complexity: Instead of hardcoding permission checks in every endpoint, permissions are declared declaratively via annotations or managed dynamically via a database Access table.
The framework supports two complementary approaches:
- Annotation-based: `@MethodAccess(POST={UNKNOWN, ADMIN})` on model classes statically declares allowed roles per HTTP method.
- Database-driven: An Access table in the database stores per-table, per-method role lists, loaded at startup via `APIJSONApplication.init()`. This allows runtime permission updates without redeployment.
Usage
Use this principle when:
- Building APIs where different user roles need different CRUD permissions per resource.
- Permissions may need to change at runtime without code redeployment (use the Access table approach).
- You want declarative, auditable permission definitions rather than scattered if-checks.
Theoretical Basis
The RBAC model maps a triple (subject, role, operation) to a permission decision:
Permission(subject, table, method) = ALLOW if:
role(subject) ∈ AllowedRoles(table, method)
Where:
subject = authenticated user from session
role = one of {UNKNOWN, LOGIN, CONTACT, CIRCLE, OWNER, ADMIN}
method = one of {GET, HEAD, GETS, HEADS, POST, PUT, DELETE}
AllowedRoles = from @MethodAccess annotation OR Access table
Role hierarchy (ascending):
- UNKNOWN — Unauthenticated visitor
- LOGIN — Authenticated user
- CONTACT — User in the subject's contact list
- CIRCLE — User in the subject's social circle
- OWNER — The resource owner (user ID matches record's userId)
- ADMIN — System administrator
Annotation-based declaration:
@MethodAccess(
POST = {UNKNOWN, ADMIN}, // Anyone (including unauthenticated) and admins can create
DELETE = {ADMIN} // Only admins can delete
)
public class User extends BaseModel<Long, Date> { ... }
Database-driven declaration (Access table columns):
- table — Target table name
- method — HTTP method (GET/POST/PUT/DELETE)
- GET, HEAD, GETS, HEADS, POST, PUT, DELETE — Comma-separated role lists
Related Pages
Implemented By
- Implementation:APIJSON_APIJSON_Demo_DemoVerifier
- Implementation:APIJSON_APIJSON_Demo_DemoSQLConfig
- Implementation:APIJSON_APIJSON_Demo_MethodAccess_Annotation
- Implementation:APIJSON_APIJSON_Demo_Verify_Model
- Implementation:APIJSON_APIJSON_Demo_Privacy_Model
- Implementation:APIJSON_APIJSON_Demo_DemoController_SimpleDemo