rbac

package module
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2023 License: MIT Imports: 8 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

usage

package main

import (
	"context"
	"log"
	"os"

	"github.com/vine-io/rbac"
	"github.com/vine-io/rbac/adapter"
	api "github.com/vine-io/rbac/api"
	vapi "github.com/vine-io/vine/lib/api"
	"gorm.io/driver/sqlite"
	"gorm.io/gorm"
)

const dsn = "rbac.sqlite.db"

func main() {
	db, err := gorm.Open(sqlite.Open(dsn))
	if err != nil {
		log.Fatal(err)
	}

	defer os.Remove(dsn)

	apt, err := adapter.NewGormAdapter(db)
	if err != nil {
		log.Fatal(err)
	}

	cfg, err := rbac.NewConfig(apt)
	if err != nil {
		log.Fatal(err)
	}

	r, err := rbac.NewRBAC(cfg)
	if err != nil {
		log.Fatal(err)
	}

	r.Enforce(context.TODO(), &api.Policy{
		Sub: "lack",
		Endpoint: &vapi.Endpoint{
			Entity: "user",
			Method: []string{"read"},
		},
	})
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultAdminName = "admin"

	DefaultModel = fmt.Sprintf(`### rbac model
[request_definition]
r = sub, obj, act

[policy_definition]
p = sub, obj, act

[role_definition]
g = _, _
g2 = _, _

[policy_effect]
e = some(where (p.eft == allow))

[matchers]
m = g(r.sub, p.sub) && g2(r.sub, p.sub) && r.obj == p.obj && r.act == p.act || r.sub == "administrator" || r.sub == "root" || r.sub == "%s"`, DefaultAdminName)
)
View Source
var (
	ErrAlreadyExists = fmt.Errorf("policy already exists")
	ErrNotFound      = fmt.Errorf("policy not found")
	ErrCasbin        = fmt.Errorf("casbin error")
)

Functions

This section is empty.

Types

type Config

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

func NewConfig

func NewConfig(adapter persist.Adapter) (Config, error)

type RBAC

type RBAC interface {
	GetAllPolicies(ctx context.Context) ([]*api.Policy, []*api.Subject)
	GetPolicies(ctx context.Context, sub string) []*api.Policy
	AddPolicy(ctx context.Context, p *api.Policy) error
	DelPolicy(ctx context.Context, p *api.Policy) error
	GetGroupPolicies(ctx context.Context, p api.PType, sub string) []*api.Subject
	AddGroupPolicy(ctx context.Context, subject *api.Subject) error
	DelGroupPolicy(ctx context.Context, subject *api.Subject) error
	Enforce(ctx context.Context, p *api.Policy) (bool, error)
}

func NewRBAC

func NewRBAC(cfg Config) (RBAC, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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