MCPcopy Index your code
hub / github.com/apache/casbin-ent-adapter

github.com/apache/casbin-ent-adapter @v1.4.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release v1.4.0 ↗ · + Follow
516 symbols 1,205 edges 22 files 464 documented · 90%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Ent Adapter

Go Report Card Go Coverage Status Godoc Release Discord Sourcegraph

Ent Adapter is the Ent adapter for Casbin. With this library, Casbin can load policy from Ent-supported databases or save policy to them.

Based on Ent Supported Drivers, the current supported databases are:

  • MySQL
  • PostgreSQL
  • SQLite
  • Gremlin

Installation

go get github.com/casbin/ent-adapter

Simple MySQL Example

package main

import (
    "github.com/casbin/casbin/v3"
    entadapter "github.com/casbin/ent-adapter"
    _ "github.com/go-sql-driver/mysql"
)

func main() {
    // Initialize an Ent adapter and use it in a Casbin enforcer:
    // The adapter will use the MySQL database named "casbin".
    // The database should be created manually before using the adapter.
    a, _ := entadapter.NewAdapter("mysql", "root:@tcp(127.0.0.1:3306)/casbin") // Your driver and data source.
    e, _ := casbin.NewEnforcer("examples/rbac_model.conf", a)

    // Load the policy from DB.
    e.LoadPolicy()

    // Check the permission.
    e.Enforce("alice", "data1", "read")

    // Modify the policy.
    // e.AddPolicy(...)
    // e.RemovePolicy(...)

    // Save the policy back to DB.
    e.SavePolicy()
}

Simple PostgreSQL Example

package main

import (
    "github.com/casbin/casbin/v3"
    entadapter "github.com/casbin/ent-adapter"
    _ "github.com/lib/pq"
)

func main() {
    // Initialize an Ent adapter and use it in a Casbin enforcer:
    // The adapter will use the PostgreSQL database named "casbin".
    // The database should be created manually before using the adapter.
    a, _ := entadapter.NewAdapter("postgres", "user=postgres password=postgres host=127.0.0.1 port=5432 sslmode=disable dbname=casbin") // Your driver and data source.
    e, _ := casbin.NewEnforcer("examples/rbac_model.conf", a)

    // Load the policy from DB.
    e.LoadPolicy()

    // Check the permission.
    e.Enforce("alice", "data1", "read")

    // Modify the policy.
    // e.AddPolicy(...)
    // e.RemovePolicy(...)

    // Save the policy back to DB.
    e.SavePolicy()
}

Use NewAdapterWithClient

You can also create an adapter with an existing Ent client instance:

package main

import (
    "github.com/casbin/casbin/v3"
    entadapter "github.com/casbin/ent-adapter"
    "github.com/casbin/ent-adapter/ent"
)

func main() {
    // Create an Ent client
    client, _ := ent.Open("mysql", "root:@tcp(127.0.0.1:3306)/casbin")

    // Initialize an Ent adapter with the client
    a, _ := entadapter.NewAdapterWithClient(client)
    e, _ := casbin.NewEnforcer("examples/rbac_model.conf", a)

    // Load the policy from DB.
    e.LoadPolicy()

    // Check the permission.
    e.Enforce("alice", "data1", "read")

    // Save the policy back to DB.
    e.SavePolicy()
}

Database Configuration

The database used in the adapter should be created manually before calling NewAdapter. The adapter will automatically create the casbin_rule table if it doesn't exist.

Getting Help

License

This project is under Apache 2.0 License. See the LICENSE file for the full license text.

Extension points exported contracts — how you extend this code

Committer (Interface)
Committer is the interface that wraps the Commit method. [2 implementers]
ent/tx.go
OrderFunc (FuncType)
OrderFunc applies an ordering on the sql selector. Deprecated: Use Asc/Desc functions or the package builders instead.
ent/ent.go
Option (FuncType)
Option function to configure the client.
ent/client.go
OrderOption (FuncType)
OrderOption defines the ordering options for the CasbinRule queries.
ent/casbinrule/casbinrule.go
CasbinRuleFunc (FuncType)
The CasbinRuleFunc type is an adapter to allow the use of ordinary function as CasbinRule mutator.
ent/hook/hook.go
TestingT (Interface)
TestingT is the interface that is shared between testing.T and testing.B and used by enttest.
ent/enttest/enttest.go
CasbinRule (FuncType)
CasbinRule is the predicate function for casbinrule builders.
ent/predicate/predicate.go
Option (FuncType)
(no doc)
adapter.go

Core symbols most depended-on inside this repo

SetField
called by 21
ent/mutation.go
Where
called by 18
ent/mutation.go
Error
called by 13
ent/enttest/enttest.go
setContextOp
called by 10
ent/ent.go
Query
called by 10
ent/tx.go
Save
called by 10
ent/casbinrule_update.go
LoadPolicy
called by 9
adapter.go
WithTx
called by 9
adapter.go

Shape

Method 269
Function 196
Struct 32
FuncType 15
Interface 3
TypeAlias 1

Languages

Go100%

Modules by API surface

ent/casbinrule/where.go110 symbols
ent/mutation.go61 symbols
ent/ent.go60 symbols
ent/casbinrule_update.go45 symbols
ent/client.go39 symbols
ent/casbinrule_query.go39 symbols
ent/casbinrule_create.go29 symbols
ent/tx.go26 symbols
adapter.go24 symbols
ent/hook/hook.go20 symbols
adapter_test.go13 symbols
ent/enttest/enttest.go11 symbols

For agents

$ claude mcp add casbin-ent-adapter \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact