Documentation
¶
Overview ¶
Package arangoadapter provides a Casbin adapter for ArangoDB. It allows you to persist authorization policies in ArangoDB instead of local files.
Index ¶
- type Adapter
- func (a *Adapter) AddPolicies(sec string, ptype string, rules [][]string) error
- func (a *Adapter) AddPoliciesCtx(ctx context.Context, sec string, ptype string, rules [][]string) error
- func (a *Adapter) AddPolicy(sec string, ptype string, rule []string) error
- func (a *Adapter) AddPolicyCtx(ctx context.Context, sec string, ptype string, rule []string) error
- func (a *Adapter) BeginTransaction(ctx context.Context) (persist.TransactionContext, error)
- func (a *Adapter) Close() error
- func (a *Adapter) Copy() *Adapter
- func (a *Adapter) IsFiltered() bool
- func (a *Adapter) LoadFilteredPolicy(model model.Model, filter interface{}) error
- func (a *Adapter) LoadFilteredPolicyCtx(ctx context.Context, model model.Model, filter interface{}) error
- func (a *Adapter) LoadPolicy(model model.Model) error
- func (a *Adapter) LoadPolicyCtx(ctx context.Context, model model.Model) error
- func (a *Adapter) Preview(rules *[]CasbinRule, model model.Model) error
- func (a *Adapter) RemoveFilteredPolicy(sec string, ptype string, fieldIndex int, fieldValues ...string) error
- func (a *Adapter) RemoveFilteredPolicyCtx(ctx context.Context, sec string, ptype string, fieldIndex int, ...) error
- func (a *Adapter) RemovePolicies(sec string, ptype string, rules [][]string) error
- func (a *Adapter) RemovePoliciesCtx(ctx context.Context, sec string, ptype string, rules [][]string) error
- func (a *Adapter) RemovePolicy(sec string, ptype string, rule []string) error
- func (a *Adapter) RemovePolicyCtx(ctx context.Context, sec string, ptype string, rule []string) error
- func (a *Adapter) SavePolicy(model model.Model) error
- func (a *Adapter) SavePolicyCtx(ctx context.Context, model model.Model) error
- func (a *Adapter) Transaction(e casbin.IEnforcer, fc func(casbin.IEnforcer) error) error
- func (a *Adapter) UpdateFilteredPolicies(sec string, ptype string, newPolicies [][]string, fieldIndex int, ...) ([][]string, error)
- func (a *Adapter) UpdatePolicies(sec string, ptype string, oldRules, newRules [][]string) error
- func (a *Adapter) UpdatePolicy(sec string, ptype string, oldRule, newPolicy []string) error
- type ArangoTransactionContext
- type BatchFilter
- type CasbinRule
- type Config
- type Filter
- type Option
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Adapter ¶
type Adapter struct {
// contains filtered or unexported fields
}
Adapter is the main struct that connects Casbin to ArangoDB. It handles all the CRUD operations for policy rules.
func NewAdapter ¶
NewAdapter creates a new ArangoDB adapter using functional options. It automatically creates the database and collection if they don't exist.
Example:
adapter, err := NewAdapter(
WithEndpoints("http://localhost:8529"),
WithAuthentication("root", "password"),
WithDatabase("casbin"),
WithCollection("casbin_rule"),
)
func NewAdapterFromClient ¶
func NewAdapterFromClient(client arangodb.Client, databaseName string, collectionName string) (*Adapter, error)
NewAdapterFromClient creates a new ArangoDB adapter from an existing client. This is useful when you already have an ArangoDB client configured. It'll automatically create the database and collection if they don't exist.
func NewFilteredAdapter ¶
NewFilteredAdapter creates a filtered adapter that won't auto-load all policies. Casbin won't automatically call LoadPolicy() for filtered adapters. You'll need to manually call LoadFilteredPolicy() with your filter criteria.
func (*Adapter) AddPolicies ¶
AddPolicies adds multiple policy rules at once.
func (*Adapter) AddPoliciesCtx ¶
func (a *Adapter) AddPoliciesCtx(ctx context.Context, sec string, ptype string, rules [][]string) error
AddPoliciesCtx adds multiple policy rules with context support.
func (*Adapter) AddPolicyCtx ¶
AddPolicyCtx is like AddPolicy but with context support.
func (*Adapter) BeginTransaction ¶
BeginTransaction starts a new database transaction. Returns a context you can use to commit or rollback.
func (*Adapter) Close ¶
Close shuts down the adapter. ArangoDB handles connections internally, so this is mostly a no-op. We have it to satisfy the adapter interface.
func (*Adapter) Copy ¶
Copy creates a shallow copy of the adapter. Useful for transaction handling where we need separate adapter instances.
func (*Adapter) IsFiltered ¶
IsFiltered returns true if the loaded policy has been filtered.
func (*Adapter) LoadFilteredPolicy ¶
LoadFilteredPolicy loads only policies that match the filter. Useful when you have millions of policies and don't want to load them all.
func (*Adapter) LoadFilteredPolicyCtx ¶
func (a *Adapter) LoadFilteredPolicyCtx(ctx context.Context, model model.Model, filter interface{}) error
LoadFilteredPolicyCtx loads filtered policies with context support.
func (*Adapter) LoadPolicy ¶
LoadPolicy loads all policies from the database into the Casbin model. This is called when Casbin initializes.
func (*Adapter) LoadPolicyCtx ¶
LoadPolicyCtx is like LoadPolicy but with context support for cancellation and timeouts.
func (*Adapter) Preview ¶
func (a *Adapter) Preview(rules *[]CasbinRule, model model.Model) error
Preview checks which rules are valid for the model. Filters out rules that don't match, so you don't get partial load failures.
func (*Adapter) RemoveFilteredPolicy ¶
func (a *Adapter) RemoveFilteredPolicy(sec string, ptype string, fieldIndex int, fieldValues ...string) error
RemoveFilteredPolicy removes policies that match a partial filter.
func (*Adapter) RemoveFilteredPolicyCtx ¶
func (a *Adapter) RemoveFilteredPolicyCtx(ctx context.Context, sec string, ptype string, fieldIndex int, fieldValues ...string) error
RemoveFilteredPolicyCtx is like RemoveFilteredPolicy but with context support.
func (*Adapter) RemovePolicies ¶
RemovePolicies removes multiple policy rules at once.
func (*Adapter) RemovePoliciesCtx ¶
func (a *Adapter) RemovePoliciesCtx(ctx context.Context, sec string, ptype string, rules [][]string) error
RemovePoliciesCtx removes multiple policy rules with context support.
func (*Adapter) RemovePolicy ¶
RemovePolicy removes a single policy rule from the database.
func (*Adapter) RemovePolicyCtx ¶
func (a *Adapter) RemovePolicyCtx(ctx context.Context, sec string, ptype string, rule []string) error
RemovePolicyCtx is like RemovePolicy but with context support. It builds a query to match the exact rule and removes it.
func (*Adapter) SavePolicy ¶
SavePolicy saves all policies from the Casbin model back to the database. Warning: This wipes out the entire collection and replaces it with the current policy set.
func (*Adapter) SavePolicyCtx ¶
SavePolicyCtx is like SavePolicy but with context support. Uses batching to handle large policy sets efficiently.
func (*Adapter) Transaction ¶
Transaction executes a function within a database transaction. This is the old-style transaction interface for backward compatibility.
func (*Adapter) UpdateFilteredPolicies ¶
func (a *Adapter) UpdateFilteredPolicies(sec string, ptype string, newPolicies [][]string, fieldIndex int, fieldValues ...string) ([][]string, error)
UpdateFilteredPolicies updates policies that match a filter. Right now it just adds the new policies - doesn't remove old ones.
func (*Adapter) UpdatePolicies ¶
UpdatePolicies updates multiple policy rules at once.
type ArangoTransactionContext ¶
type ArangoTransactionContext struct {
// contains filtered or unexported fields
}
ArangoTransactionContext wraps an ArangoDB transaction for Casbin.
func (*ArangoTransactionContext) Commit ¶
func (atx *ArangoTransactionContext) Commit() error
Commit commits the database transaction.
func (*ArangoTransactionContext) GetAdapter ¶
func (atx *ArangoTransactionContext) GetAdapter() persist.Adapter
GetAdapter returns an adapter that uses this transaction. Any policies you add/remove through it will be part of the transaction.
func (*ArangoTransactionContext) Rollback ¶
func (atx *ArangoTransactionContext) Rollback() error
Rollback rolls back the database transaction.
type BatchFilter ¶
type BatchFilter struct {
// contains filtered or unexported fields
}
BatchFilter wraps multiple filters for batch operations.
type CasbinRule ¶
type CasbinRule struct {
Key string `json:"_key,omitempty"` // ArangoDB document key
Ptype string `json:"ptype"` // Policy type (p, g, p2, g2, etc.)
V0 string `json:"v0"`
V1 string `json:"v1"`
V2 string `json:"v2"`
V3 string `json:"v3"`
V4 string `json:"v4"`
V5 string `json:"v5"`
}
CasbinRule represents a single policy rule in ArangoDB. Casbin supports up to 6 values per rule, so we've got V0 through V5.
type Config ¶
type Config struct {
Endpoints []string // ArangoDB endpoints (e.g., ["http://localhost:8529"])
Username string // Database username
Password string // Database password
DatabaseName string // Name of the database to use
CollectionName string // Name of the collection for Casbin rules
TLSEnabled bool // Whether to use TLS
CACertPath string // Path to CA certificate file (for TLS)
TLSConfig *tls.Config // Custom TLS configuration (optional)
}
Config holds the configuration for connecting to ArangoDB.
type Filter ¶
type Filter struct {
Ptype []string
V0 []string
V1 []string
V2 []string
V3 []string
V4 []string
V5 []string
}
Filter lets you query policies based on specific field values. Each field is a slice so you can match against multiple values.
type Option ¶
type Option func(*Config)
Option is a functional option for configuring the adapter.
func WithAuthentication ¶
WithAuthentication sets the username and password.
func WithCollection ¶
WithCollection sets the collection name for Casbin rules.
func WithEndpoints ¶
WithEndpoints sets the ArangoDB endpoints.
func WithTLSConfig ¶
WithTLSConfig sets a custom TLS configuration.