apache / apache/casbin

[feature] Programmatic model builder

Open
#1,622 1 comment 1 reaction 2 assignees Claimed by @hsluoyz View on GitHub
enhancement
Dominant language
Go
Stars
20.4k
Forks
1.8k
Avg merge
5d 11h
Merged PRs (30d)
2

Description

## 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):

```go
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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.