Heuristic:APIJSON APIJSON Role Hierarchy Validation
| Knowledge Sources | |
|---|---|
| Domains | Access_Control, Security |
| Last Updated | 2026-10-11 00:00 GMT |
Overview
Role trust hierarchy validation where APIJSON enforces that the client-claimed role (UNKNOWN, LOGIN, CONTACT, CIRCLE, OWNER, ADMIN) matches the authenticated user's actual permissions before allowing access to any table.
Description
APIJSON uses a six-level role hierarchy for access control. The client specifies the desired role via the @role key in the request. The server validates this role against the user's actual identity and permissions before executing the query.
The role hierarchy, from least to most privileged:
| Role | Description | Validation Entry |
|---|---|---|
| UNKNOWN | No login required; no ROLE_MAP entry needed | |
| LOGIN | userId > 0 (user has a valid ID) | |
| CONTACT | userId{} → contactIdList (user ID is in the owner's contact list) | |
| CIRCLE | userId-() → verifyCircle() (custom verification function) | |
| OWNER | userId → userId (user ID matches the record's userId field) | |
| ADMIN | userId-() → verifyAdmin() (custom admin verification function) |
The ROLE_MAP is a LinkedHashMap that stores the validation condition for each role. When verifyAccess() is called, it:
- Checks that the claimed role exists in ROLE_MAP
- If the role is not UNKNOWN, calls verifyLogin() to ensure the user is authenticated
- Calls verifyAllowRole() to check the ACCESS_MAP (table-method-role permission matrix)
- Calls verifyUseRole() to validate the user actually has the claimed role's relationship to the data
Usage
Use this heuristic when:
- Configuring table-level access permissions via @MethodAccess annotations
- Implementing custom role verification (e.g., verifyAdmin(), verifyCircle())
- Debugging access denied errors
- Designing the permission model for a new APIJSON deployment
The Insight (Rule of Thumb)
- Action: Set the @role key in the request to the minimum role needed for the operation.
- Value: One of: UNKNOWN, LOGIN, CONTACT, CIRCLE, OWNER, ADMIN.
- Trade-off: Claiming a higher role than needed increases the chance of access denial; claiming UNKNOWN when authentication is needed will fail.
- Compatibility: Custom roles cannot be added without modifying ROLE_MAP; the six roles are fixed in the core library.
Reasoning
The role hierarchy is designed around the principle of progressive trust: each role level requires stronger evidence of the user's relationship to the data. UNKNOWN requires nothing, LOGIN requires a valid session, CONTACT requires membership in the owner's contact list, OWNER requires the user ID to match the record's owner, and ADMIN requires a custom verification function.
The ACCESS_MAP (populated from @MethodAccess annotations) defines which roles are allowed to perform which HTTP methods (GET, POST, PUT, DELETE) on each table. The ROLE_MAP defines how to verify that the user actually has the claimed role. Together, they form a two-layer access control: first checking that the role is permitted for the table-method combination, then verifying the user's identity matches the role's criteria.
The SYSTEM_ACCESS_MAP contains built-in permissions for system tables (Access, Function, Script, Request). These are only modifiable by ADMIN role, and system metadata tables (Table, Column, etc.) are only accessible when Log.DEBUG = true.
Code Evidence
Role definitions and validation entries in `AbstractVerifier.java:76-137`:
public static final String UNKNOWN = "UNKNOWN";
public static final String LOGIN = "LOGIN";
public static final String CONTACT = "CONTACT";
public static final String CIRCLE = "CIRCLE";
public static final String OWNER = "OWNER";
public static final String ADMIN = "ADMIN";
static {
ROLE_MAP = new LinkedHashMap<>();
ROLE_MAP.put(UNKNOWN, new Entry<String, Object>());
ROLE_MAP.put(LOGIN, new Entry<String, Object>("userId>", 0));
ROLE_MAP.put(CONTACT, new Entry<String, Object>("userId{}", "contactIdList"));
ROLE_MAP.put(CIRCLE, new Entry<String, Object>("userId-()", "verifyCircle()"));
ROLE_MAP.put(OWNER, new Entry<String, Object>("userId", "userId"));
ROLE_MAP.put(ADMIN, new Entry<String, Object>("userId-()", "verifyAdmin()"));
}
Role existence check in `AbstractVerifier.java:277-285`:
if (ROLE_MAP.containsKey(role) == false) {
Set<String> NAMES = ROLE_MAP.keySet();
throw new IllegalArgumentException("角色 " + role + " 不存在!只能是[" + StringUtil.get(NAMES.toArray()) + "]中的一种!");
}
if (role.equals(UNKNOWN) == false) {
verifyLogin();
}
Access map permission check in `AbstractVerifier.java:327-331`:
Map<RequestMethod, String[]> map = ACCESS_MAP.get(table);
if (map == null || Arrays.asList(map.get(method)).contains(role) == false) {
throw new IllegalAccessException(table + " 不允许 " + role + " 用户的 " + method.name() + " 请求!");
}