casbin

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 28, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package casbin provides a Casbin-based authorization provider.

This provider uses Casbin for flexible RBAC/ABAC policy evaluation. It supports both code-defined policies and database-stored policies.

Example usage:

provider, err := casbin.New(
    casbin.WithModel(casbin.RBACModel),
    casbin.WithRoleGetter(getRoleFromDB),
)

Index

Constants

View Source
const (
	// RBACModel is a basic RBAC model with role hierarchy.
	RBACModel = `` /* 225-byte string literal not displayed */

	// RBACWithResourceRolesModel supports resource-specific roles.
	RBACWithResourceRolesModel = `` /* 237-byte string literal not displayed */

	// ABACModel supports attribute-based access control.
	ABACModel = `` /* 233-byte string literal not displayed */

)

Pre-defined Casbin models for common use cases.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option func(*Provider) error

Option configures an Provider.

func WithEnforcer

func WithEnforcer(e *casbin.Enforcer) Option

WithEnforcer sets a pre-configured Casbin enforcer.

func WithModel

func WithModel(modelText string) Option

WithModel sets the Casbin model from a string.

func WithOwnerFullAccess

func WithOwnerFullAccess(enabled bool) Option

WithOwnerFullAccess enables full access for resource owners.

func WithPlatformAdminBypass

func WithPlatformAdminBypass(enabled bool) Option

WithPlatformAdminBypass enables platform admin bypass for all checks.

func WithPlatformAdminChecker

func WithPlatformAdminChecker(fn func(ctx context.Context, principalID uuid.UUID) (bool, error)) Option

WithPlatformAdminChecker sets the function to check platform admin status.

func WithRoleGetter

func WithRoleGetter(fn func(ctx context.Context, principalID, orgID uuid.UUID) (string, error)) Option

WithRoleGetter sets the function to retrieve a principal's role in an organization.

type Provider

type Provider struct {
	// contains filtered or unexported fields
}

Provider implements the Authorizer interface using Casbin.

func New

func New(opts ...Option) (*Provider, error)

New creates a new Casbin Provider with the given options.

func (*Provider) AddPolicies

func (a *Provider) AddPolicies(policies [][]string) error

AddPolicies adds multiple policy rules at once.

func (*Provider) AddPolicy

func (a *Provider) AddPolicy(role, resource, action string) error

AddPolicy adds a policy rule.

func (*Provider) AddRoleForUser

func (a *Provider) AddRoleForUser(user, role string) error

AddRoleForUser assigns a role to a user.

func (*Provider) Can

func (a *Provider) Can(ctx context.Context, principal authz.Principal, action authz.Action, resource authz.Resource) (bool, error)

Can checks if a principal can perform an action on a resource.

func (*Provider) CanAll

func (a *Provider) CanAll(ctx context.Context, principal authz.Principal, actions []authz.Action, resource authz.Resource) (bool, error)

CanAll checks if a principal can perform all specified actions on a resource.

func (*Provider) CanAny

func (a *Provider) CanAny(ctx context.Context, principal authz.Principal, actions []authz.Action, resource authz.Resource) (bool, error)

CanAny checks if a principal can perform any of the specified actions on a resource.

func (*Provider) CanForOrg

func (a *Provider) CanForOrg(ctx context.Context, principal authz.Principal, orgID uuid.UUID, action authz.Action, resource authz.Resource) (bool, error)

CanForOrg checks permission scoped to a specific organization.

func (*Provider) Enforcer

func (a *Provider) Enforcer() *casbin.Enforcer

Enforcer returns the underlying Casbin enforcer for advanced configuration.

func (*Provider) Filter

func (a *Provider) Filter(ctx context.Context, principal authz.Principal, action authz.Action, resources []authz.Resource) ([]authz.Resource, error)

Filter returns only the resources the principal can access with the given action.

func (*Provider) GetRole

func (a *Provider) GetRole(ctx context.Context, principal authz.Principal, orgID uuid.UUID) (string, error)

GetRole returns the principal's role in an organization.

func (*Provider) IsMember

func (a *Provider) IsMember(ctx context.Context, principal authz.Principal, orgID uuid.UUID) (bool, error)

IsMember checks if a principal is a member of an organization.

func (*Provider) IsPlatformAdmin

func (a *Provider) IsPlatformAdmin(ctx context.Context, principal authz.Principal) (bool, error)

IsPlatformAdmin checks if a principal has platform-wide admin access.

func (*Provider) LoadPoliciesFromMap

func (a *Provider) LoadPoliciesFromMap(permissions authz.RolePermissions) error

LoadPoliciesFromMap loads policies from a role-permission map.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL