Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Principle:APIJSON APIJSON Demo Role Based Access Control

From Leeroopedia
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):

  1. UNKNOWN — Unauthenticated visitor
  2. LOGIN — Authenticated user
  3. CONTACT — User in the subject's contact list
  4. CIRCLE — User in the subject's social circle
  5. OWNER — The resource owner (user ID matches record's userId)
  6. 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

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment