Skip to content

[feature] Programmatic model builder #1622

@nodece

Description

@nodece

Background

Casbin models are currently defined as plain text (model.conf).
While flexible, this makes it inconvenient to analyze, refactor, or compose models programmatically, especially in larger or long-lived projects.

This proposal explores adding a programmatic way to construct models, without changing existing semantics or model behavior.

Proposal

Introduce an optional model builder that allows Casbin models to be constructed in code and then converted into a standard Casbin model.

Example (conceptual):

model := builder.New().
    Request("sub", "obj", "act").
    Policy("sub", "obj", "act").
    Effect(builder.AllowOverride).
    Matcher(
        builder.And(
            builder.Eq("sub"),
            builder.Eq("obj"),
            builder.Eq("act"),
        ),
    ).
    Build()

The resulting model is equivalent to one loaded from model.conf.

Design Notes

  • The builder does not replace model.conf
  • Core enforcement logic remains unchanged
  • Effect and matcher expressions are generated using existing Casbin semantics
  • The builder provides a structured way to construct commonly used model patterns

Effect and matcher definitions may be derived from previously defined sections, such as request and policy fields, but are ultimately emitted as standard Casbin expressions.

Scope

In scope

  • Programmatic construction of model sections
  • Generating standard Casbin models

Out of scope

  • Introducing a new DSL or matcher language
  • Enforce-time validation or semantic changes

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions