caskin

package module
v0.2.5 Latest Latest
Warning

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

Go to latest
Published: May 31, 2023 License: MIT Imports: 21 Imported by: 0

README

Caskin

Go

Caskin is a multi-domain rbac library for Golang projects. It develops base on caskin

Introduction

Example

Documentation

  1. Configuration to configure caskin instance and dictionary.
  2. Design for the details of design.
  3. API for the details of caskin service method.

Getting Started

Define the dictionary configuration file

Create a new file for example named caskin.toml to define feature, backend, frontend, package, creator_object, creator_role, creator_policy.

feature = [
    {name = "feature"},
]

backend = [
    {path = "api/feature", method = "GET"},
    {path = "api/feature", method = "POST"},
]

frontend = [
    {name = "feature", type = "menu"},
]

package = [
    {key = "feature", backend = [["api/feature", "GET"], ["api/feature", "POST"]], frontend = [["feature", "menu"]]},
]

creator_object = [
    {name = "role_root", type = "role"},
]

creator_role = [
    {name = "admin"},
    {name = "member"},
]

creator_policy = [
    {role = "admin", object = "role_root", action = ["read", "write", "manage"]},
    {role = "admin", object = "github.com/awatercolorpen/caskin::feature", action = ["read"]},
    {role = "member", object = "role_root", action = ["read"]},
]
To make use of caskin in golang

Register user-role-object-domain instance.

It should implement the interface of caskin.User, caskin.Role, caskin.Object, caskin.Domain generally. Or use the example implementation in github.com/awatercolorpen/caskin/example for the prototype.

import "github.com/awatercolorpen/caskin"
import "github.com/awatercolorpen/caskin/example"

// register instance type
caskin.Register[*example.User, *example.Role, *example.Object, *example.Domain]()

Create a new caskin service instance.

import "github.com/awatercolorpen/caskin"

// set db option
dbOption := &caskin.DBOption{
	DSN:  "./sqlite.db", 
	Type: "sqlite",
}

// set dictionary option
dictionaryOption := &caskin.DictionaryOption{
	Dsn: "caskin.toml",
}

// build service option
option := &caskin.Options{
	Dictionary: dictionaryOption, 
	DB:         dbOption,
}

// create a new service instance
service, err := caskin.New(option)

Initialize first domain, and add first superadmin.

domain := &example.Domain{Name: "school-1"}
superadmin := &example.User{Email: "superadmin@qq.com"}

// create domain
err := service.CreateDomain(domain)

// reset domain by the creator setting from caskin.toml
err := service.ResetDomain(domain)

// reset domain by the feature setting from caskin.toml
err := service.ResetFeature(domain)

// add a user to caskin
err := service.CreateUser(superadmin)

// set a user as superadmin
err := service.AddSuperadmin(p.Superadmin)
To manage the authorization business

Use the caskin.Service's API to control on authorization management.

// authorization business: delete one role
err := service.DeleteRole(operatorUser, workingOnDomain, toDeleteRole))

Use the caskin.CurrentService interface.

currentService := service.SetCurrent(operatorUser, workingOnDomain)

License

See the License File.

Documentation

Index

Constants

View Source
const (
	ObjectPType = "g2"

	SuperadminRole   = "superadmin"
	SuperadminDomain = "superdomain"
)

Variables

View Source
var (
	ErrNil                   = fmt.Errorf("nil data")
	ErrEmptyID               = fmt.Errorf("empty id")
	ErrAlreadyExists         = fmt.Errorf("already exists")
	ErrNotExists             = fmt.Errorf("not exists")
	ErrInValidObject         = fmt.Errorf("invalid object")
	ErrInValidObjectType     = fmt.Errorf("invalid object type")
	ErrCantChangeObjectType  = fmt.Errorf("can't change object type")
	ErrCantOperateRootObject = fmt.Errorf("can't operate root object")
	ErrParentCanNotBeItself  = fmt.Errorf("parent id can't be it self id")
	ErrParentToDescendant    = fmt.Errorf("can't change parent to descendant")
	ErrInValidRequest        = fmt.Errorf("invalid request")

	ErrNoReadPermission    = fmt.Errorf("no read permission")
	ErrNoWritePermission   = fmt.Errorf("no write permission")
	ErrNoManagePermission  = fmt.Errorf("no manage permission")
	ErrNoBackendPermission = fmt.Errorf("no backend api permission")

	ErrIsNotSuperadmin = fmt.Errorf("is not superadmin")
	ErrInValidCurrent  = fmt.Errorf("invalid current api")
)
View Source
var (
	DefaultSuperadminRoleName   = "superadmin_role"
	DefaultSuperadminDomainName = "superadmin_domain"
)
View Source
var CasbinModelText string
View Source
var (
	DefaultFeatureRootName = "github.com/awatercolorpen/caskin::feature"
)

Functions

func CasbinModel

func CasbinModel() (model.Model, error)

func Check added in v0.1.0

func Check[T any](e IEnforcer, u User, d Domain, one T, action Action) bool

Check object/object_data permission by u, d, action

func Diff

func Diff[T constraints.Ordered](source, target []T) (add, remove []T)

Diff do diff source, target list to get add, remove list

func Filter

func Filter[T any](e IEnforcer, u User, d Domain, action Action, source []T) []T

Filter do filter source permission by u, d, action

func GetByID added in v0.1.0

func GetByID[T any](db MetaDB, id []uint64) ([]T, error)

func ID added in v0.1.0

func ID[E idInterface](in []E) []uint64

func IDMap added in v0.1.0

func IDMap[E idInterface](in []E) map[uint64]E

func NewObjectDeleter added in v0.1.0

func NewObjectDeleter(children ObjectChildrenGetFunc, delete ObjectDeleteFunc) *objectDeleter

func NewObjectDirectory added in v0.1.0

func NewObjectDirectory(in []*Directory) *objectDirectory

func NewObjectUpdater added in v0.1.0

func NewObjectUpdater(
	parentGet ObjectParentGetFunc,
	parentAdd ObjectParentAddFunc,
	parentDel ObjectParentDelFunc) *objectUpdater

func Register added in v0.1.0

func Register[U User, R Role, O Object, D Domain]()

func SetWatcher added in v0.2.0

func SetWatcher(e casbin.IEnforcer, option *WatcherOption) error

Types

type Action

type Action = string
const (
	Read   Action = "read"
	Write  Action = "write"
	Manage Action = "manage"
)

type Backend added in v0.1.0

type Backend struct {
	Path        string `json:"path"        toml:"path"`
	Method      string `json:"method"      toml:"method"`
	Description string `json:"description" toml:"description"`
	Group       string `json:"group"       toml:"group"`
}

Backend it is for backend API

func (*Backend) Key added in v0.1.0

func (b *Backend) Key() string

func (*Backend) ToObject added in v0.1.0

func (b *Backend) ToObject() Object

type CountDirectoryItem added in v0.1.0

type CountDirectoryItem = func([]uint64) (map[uint64]uint64, error)

type CreatorObject added in v0.1.0

type CreatorObject struct {
	Name        string `json:"name"        toml:"name"`
	Type        string `json:"type"        toml:"type"`
	Description string `json:"description" toml:"description"`
}

func (*CreatorObject) ToObject added in v0.1.0

func (c *CreatorObject) ToObject() Object

type CreatorPolicy added in v0.1.0

type CreatorPolicy struct {
	Object string   `json:"object" toml:"object"`
	Role   string   `json:"role"   toml:"role"`
	Action []string `json:"action" toml:"action"`
}

type CreatorRole added in v0.1.0

type CreatorRole struct {
	Name        string `json:"name"        toml:"name"`
	Description string `json:"description" toml:"description"`
}

func (*CreatorRole) ToRole added in v0.1.0

func (c *CreatorRole) ToRole() Role

type DBOption added in v0.1.0

type DBOption struct {
	DSN  string `json:"dsn"`
	Type string `json:"type"`
}

func (*DBOption) NewDB added in v0.1.0

func (o *DBOption) NewDB() (*gorm.DB, error)

type DictionaryOption added in v0.1.0

type DictionaryOption struct {
	Dsn  string `json:"dsn"`
	Type string `json:"type"`
}

type Directory added in v0.1.0

type Directory struct {
	Object
	AllDirectoryCount uint64 `json:"all_directory_count"`
	AllItemCount      uint64 `json:"all_item_count"`
	TopDirectoryCount uint64 `json:"top_directory_count"`
	TopItemCount      uint64 `json:"top_item_count"`
}

type DirectoryRequest added in v0.1.0

type DirectoryRequest struct {
	To              uint64   `json:"to,omitempty"`
	ID              []uint64 `json:"id,omitempty"`
	Type            string   `json:"type,omitempty"`
	Policy          string   `json:"policy,omitempty"`
	SearchType      string   `json:"search_type,omitempty"`
	CountDirectory  func([]uint64) (map[uint64]uint64, error)
	ActionDirectory func([]uint64) error
}

type DirectoryResponse added in v0.1.0

type DirectoryResponse struct {
	DoneDirectoryCount uint64 `json:"done_directory_count,omitempty"`
	DoneItemCount      uint64 `json:"done_item_count,omitempty"`
	ToDoDirectoryCount uint64 `json:"to_do_directory_count,omitempty"`
	ToDoItemCount      uint64 `json:"to_do_item_count,omitempty"`
}

type DirectorySearchType added in v0.1.0

type DirectorySearchType = string
const (
	DirectorySearchAll DirectorySearchType = "all"
	DirectorySearchTop DirectorySearchType = "top"
)

type Domain

type Domain interface {
	// contains filtered or unexported methods
}

func GetSuperadminDomain added in v0.1.0

func GetSuperadminDomain() Domain

type EdgeSorter added in v0.1.0

type EdgeSorter[T constraints.Ordered] map[T]int

func NewEdgeSorter added in v0.1.0

func NewEdgeSorter[T constraints.Ordered](order []T) EdgeSorter[T]

func (EdgeSorter[T]) LeafFirstSort added in v0.1.0

func (e EdgeSorter[T]) LeafFirstSort(edges []*InheritanceEdge[T])

func (EdgeSorter[T]) RootFirstSort added in v0.1.0

func (e EdgeSorter[T]) RootFirstSort(edges []*InheritanceEdge[T])

type Factory added in v0.1.0

type Factory interface {
	User(string) (User, error)
	Role(string) (Role, error)
	Object(string) (Object, error)
	Domain(string) (Domain, error)
	NewUser() User
	NewRole() Role
	NewObject() Object
	NewDomain() Domain
	MetadataDB(db *gorm.DB) MetaDB
}

func DefaultFactory added in v0.1.0

func DefaultFactory() Factory

type Feature added in v0.1.0

type Feature struct {
	Name        string `json:"name"        toml:"name"`
	Description string `json:"description" toml:"description"`
	Group       string `json:"group"       toml:"group"`
}

Feature it is a package of Backend and Frontend

func (*Feature) Key added in v0.1.0

func (f *Feature) Key() string

func (*Feature) ToObject added in v0.1.0

func (f *Feature) ToObject() Object

type Frontend added in v0.1.0

type Frontend struct {
	Name        string `json:"name"        toml:"name"`
	Type        string `json:"type"        toml:"type"`
	Description string `json:"description" toml:"description"`
	Group       string `json:"group"       toml:"group"`
}

Frontend it is for frontend web component

func (*Frontend) Key added in v0.1.0

func (f *Frontend) Key() string

func (*Frontend) ToObject added in v0.1.0

func (f *Frontend) ToObject() Object

type IBaseService added in v0.1.0

type IBaseService interface {
	// AddSuperadmin adds a superadmin user
	AddSuperadmin(User) error
	// DeleteSuperadmin deletes a superadmin user
	DeleteSuperadmin(User) error
	// GetSuperadmin gets all superadmin users
	GetSuperadmin() ([]User, error)

	// CreateUser creates a new user
	CreateUser(User) error
	// RecoverUser recovers a deleted user
	RecoverUser(User) error
	// DeleteUser deletes a user
	DeleteUser(User) error
	// UpdateUser updates a user
	UpdateUser(User) error

	// CreateDomain creates a new domain
	CreateDomain(Domain) error
	// RecoverDomain recovers a deleted domain
	RecoverDomain(Domain) error
	// DeleteDomain deletes a domain
	DeleteDomain(Domain) error
	// UpdateDomain updates a domain
	UpdateDomain(Domain) error
	// GetDomain gets all domains
	GetDomain() ([]Domain, error)
	// ResetDomain resets a domain to its initial state
	ResetDomain(Domain) error

	// CreateObject creates a new object in a domain
	CreateObject(User, Domain, Object) error
	// RecoverObject recovers a deleted object in a domain
	RecoverObject(User, Domain, Object) error
	// DeleteObject deletes an object in a domain
	DeleteObject(User, Domain, Object) error
	// UpdateObject updates an object in a domain
	UpdateObject(User, Domain, Object) error
	// GetObject gets all objects in a domain that the user can perform an action on
	GetObject(User, Domain, Action, ...ObjectType) ([]Object, error)
	// GetObjectHierarchyLevel gets the hierarchy level of an object in a domain
	GetObjectHierarchyLevel(user User, domain Domain, object Object) (int, error)

	// CreateRole creates a new role in a domain
	CreateRole(User, Domain, Role) error
	// RecoverRole recovers a deleted role in a domain
	RecoverRole(User, Domain, Role) error
	// DeleteRole deletes a role in a domain
	DeleteRole(User, Domain, Role) error
	// UpdateRole updates a role in a domain
	UpdateRole(User, Domain, Role) error
	// GetRole gets all roles in a domain
	GetRole(User, Domain) ([]Role, error)

	// AddUserRole adds user-role pairs in a domain
	AddUserRole(User, Domain, []*UserRolePair) error
	// RemoveUserRole removes user-role pairs in a domain
	RemoveUserRole(User, Domain, []*UserRolePair) error
	// AddRoleG adds a role inheritance relation in a domain
	AddRoleG(User, Domain, Role, Role) error
	// RemoveRoleG removes a role inheritance relation in a domain
	RemoveRoleG(User, Domain, Role, Role) error

	// GetUserByDomain gets all users in a domain
	GetUserByDomain(Domain) ([]User, error)
	// GetDomainByUser gets all domains that a user belongs to
	GetDomainByUser(User) ([]Domain, error)

	// GetUserRole gets all user-role pairs in a domain
	GetUserRole(User, Domain) ([]*UserRolePair, error)
	// GetUserRoleByUser gets all user-role pairs in a domain for a specific user
	GetUserRoleByUser(User, Domain, User) ([]*UserRolePair, error)
	// GetUserRoleByRole gets all user-role pairs in a domain for a specific role
	GetUserRoleByRole(User, Domain, Role) ([]*UserRolePair, error)
	// ModifyUserRolePerUser modifies the user-role pairs in a domain for a specific user
	ModifyUserRolePerUser(User, Domain, User, []*UserRolePair) error
	// ModifyUserRolePerRole modifies the user-role pairs in a domain for a specific role
	ModifyUserRolePerRole(User, Domain, Role, []*UserRolePair) error

	// GetPolicy gets all policies in a domain
	GetPolicy(User, Domain) ([]*Policy, error)
	// GetPolicyByRole gets all policies in a domain for a specific role
	GetPolicyByRole(User, Domain, Role) ([]*Policy, error)
	// ModifyPolicyPerRole modifies the policies in a domain for a specific role
	ModifyPolicyPerRole(User, Domain, Role, []*Policy) error

	// CreateObjectData creates a new object data in a domain with an object type
	CreateObjectData(User, Domain, ObjectData, ObjectType) error
	// RecoverObjectData recovers a deleted object data in a domain
	RecoverObjectData(User, Domain, ObjectData) error
	// DeleteObjectData deletes an object data in a domain
	DeleteObjectData(User, Domain, ObjectData) error
	// UpdateObjectData updates an object data in a domain with an object type
	UpdateObjectData(User, Domain, ObjectData, ObjectType) error

	// CheckCreateObjectData checks if the user can create an object data in a domain with an object type
	CheckCreateObjectData(User, Domain, ObjectData, ObjectType) error
	// CheckRecoverObjectData checks if the user can recover an object data in a domain
	CheckRecoverObjectData(User, Domain, ObjectData) error
	// CheckDeleteObjectData checks if the user can delete an object data in a domain
	CheckDeleteObjectData(User, Domain, ObjectData) error
	// CheckWriteObjectData checks if the user can write an object data in a domain with an object type
	CheckWriteObjectData(User, Domain, ObjectData, ObjectType) error
	// CheckUpdateObjectData checks if the user can update an object data in a domain with an object type
	CheckUpdateObjectData(User, Domain, ObjectData, ObjectType) error
	// CheckModifyObjectData checks if the user can modify an object data in a domain
	CheckModifyObjectData(User, Domain, ObjectData) error
	// CheckGetObjectData checks if the user can get an object data in a domain
	CheckGetObjectData(User, Domain, ObjectData) error
}

IBaseService is the interface that defines the basic CRUD operations for users, domains, objects and roles

type ICreatorDictionary added in v0.1.0

type ICreatorDictionary interface {
	GetCreatorObject() ([]*CreatorObject, error)
	GetCreatorRole() ([]*CreatorRole, error)
	GetCreatorPolicy() ([]*CreatorPolicy, error)
}

type ICurrentService added in v0.1.0

type ICurrentService interface {
	// SetCurrent sets the current user and domain for the service and returns a new service instance
	SetCurrent(User, Domain) IService

	// CreateObjectDataWithCurrent creates a new object data in the current domain with an object type
	CreateObjectDataWithCurrent(ObjectData, ObjectType) error
	// RecoverObjectDataWithCurrent recovers a deleted object data in the current domain
	RecoverObjectDataWithCurrent(ObjectData) error
	// DeleteObjectDataWithCurrent deletes an object data in the current domain
	DeleteObjectDataWithCurrent(ObjectData) error
	// UpdateObjectDataWithCurrent updates an object data in the current domain with an object type
	UpdateObjectDataWithCurrent(ObjectData, ObjectType) error

	// CheckCreateObjectDataWithCurrent checks if the current user can create an object data in the current domain with an object type
	CheckCreateObjectDataWithCurrent(ObjectData, ObjectType) error
	// CheckRecoverObjectDataWithCurrent checks if the current user can recover an object data in the current domain
	CheckRecoverObjectDataWithCurrent(ObjectData) error
	// CheckDeleteObjectDataWithCurrent checks if the current user can delete an object data in the current domain
	CheckDeleteObjectDataWithCurrent(ObjectData) error
	// CheckWriteObjectDataWithCurrent checks if the current user can write an object data in the current domain with an object type
	CheckWriteObjectDataWithCurrent(ObjectData, ObjectType) error
	// CheckUpdateObjectDataWithCurrent checks if the current user can update an object data in the current domain with an object type
	CheckUpdateObjectDataWithCurrent(ObjectData, ObjectType) error
	// CheckModifyObjectDataWithCurrent checks if the current user can modify an object data in the current domain
	CheckModifyObjectDataWithCurrent(ObjectData) error
	// CheckGetObjectDataWithCurrent checks if the current user can get an object data in the current domain
	CheckGetObjectDataWithCurrent(ObjectData) error
}

ICurrentService is the interface that defines the current user-related operations

type IDictionary added in v0.1.0

type IDictionary interface {
	IFeatureDictionary
	ICreatorDictionary
}

func NewDictionary added in v0.1.0

func NewDictionary(option *DictionaryOption) (IDictionary, error)

type IDirectory added in v0.1.0

type IDirectory interface {
	Search(uint64, DirectorySearchType) []*Directory
}

type IDirectoryService added in v0.1.0

type IDirectoryService interface {
	// CreateDirectory creates a new directory for an object in a domain
	CreateDirectory(User, Domain, Object) error
	// UpdateDirectory updates an existing directory for an object in a domain
	UpdateDirectory(User, Domain, Object) error
	// DeleteDirectory deletes a directory and its subdirectories in a domain based on a request
	DeleteDirectory(User, Domain, *DirectoryRequest) error
	// GetDirectory gets all directories and their subdirectories in a domain based on a request
	GetDirectory(User, Domain, *DirectoryRequest) ([]*Directory, error)
	// MoveDirectory moves a directory and its subdirectories to another directory in a domain based on a request and returns the updated directory structure
	MoveDirectory(User, Domain, *DirectoryRequest) (*DirectoryResponse, error)
	// MoveItem moves an object data to another directory in a domain based on a request and returns the updated directory structure
	MoveItem(User, Domain, ObjectData, *DirectoryRequest) (*DirectoryResponse, error)
	// CopyItem copies an object data to another directory in a domain based on a request and returns the updated directory structure
	CopyItem(User, Domain, ObjectData, *DirectoryRequest) (*DirectoryResponse, error)
}

IDirectoryService is the interface that defines the directory-related operations for objects and object data

type IEnforcer

type IEnforcer interface {
	Enforce(User, Object, Domain, Action) (bool, error)
	EnforceRole(son Role, parent Role, domain Domain) (bool, error)
	EnforceObject(son Object, parent Object, domain Domain) (bool, error)
	IsSuperadmin(User) (bool, error)

	GetDomainsIncludeUser(User) []Domain

	GetRolesForUserInDomain(User, Domain) []Role
	GetUsersForRoleInDomain(Role, Domain) []User
	GetParentsForRoleInDomain(Role, Domain) []Role
	GetChildrenForRoleInDomain(Role, Domain) []Role
	GetParentsForObjectInDomain(Object, Domain) []Object
	GetChildrenForObjectInDomain(Object, Domain) []Object
	GetPoliciesForRoleInDomain(Role, Domain) []*Policy
	GetPoliciesForObjectInDomain(Object, Domain) []*Policy

	RemoveUserInDomain(User, Domain) error
	RemoveRoleInDomain(Role, Domain) error
	RemoveObjectInDomain(Object, Domain) error

	AddPolicyInDomain(Role, Object, Domain, Action) error
	RemovePolicyInDomain(Role, Object, Domain, Action) error

	AddRoleForUserInDomain(User, Role, Domain) error
	RemoveRoleForUserInDomain(User, Role, Domain) error

	AddParentForRoleInDomain(Role, Role, Domain) error
	RemoveParentForRoleInDomain(Role, Role, Domain) error

	AddParentForObjectInDomain(Object, Object, Domain) error
	RemoveParentForObjectInDomain(Object, Object, Domain) error

	GetUsersInDomain(Domain) []User
	GetRolesInDomain(Domain) []Role
	GetObjectsInDomain(Domain) []Object
	GetPoliciesInDomain(Domain) []*Policy

	RemoveUsersInDomain(Domain) error
}

func NewEnforcer

func NewEnforcer(e casbin.IEnforcer, factory Factory) IEnforcer

type IFeatureDictionary added in v0.1.0

type IFeatureDictionary interface {
	GetFeature() ([]*Feature, error)
	GetBackend() ([]*Backend, error)
	GetFrontend() ([]*Frontend, error)
	GetFeatureByKey(key string) (*Feature, error)
	GetBackendByKey(key string) (*Backend, error)
	GetFrontendByKey(key string) (*Frontend, error)
	GetPackage() ([]*Package, error)
}

type IFeatureService added in v0.1.0

type IFeatureService interface {
	// AuthBackend authenticates a user for a backend in a domain
	AuthBackend(User, Domain, *Backend) error
	// AuthFrontend authenticates a user for frontends in a domain
	AuthFrontend(User, Domain) []*Frontend
	// GetFeature gets all features in a domain
	GetFeature(User, Domain) ([]*Feature, error)
	// GetFeaturePolicy gets all feature policies in a domain
	GetFeaturePolicy(User, Domain) ([]*Policy, error)
	// GetFeaturePolicyByRole gets all feature policies in a domain for a specific role
	GetFeaturePolicyByRole(User, Domain, Role) ([]*Policy, error)
	// ModifyFeaturePolicyPerRole modifies the feature policies in a domain for a specific role
	ModifyFeaturePolicyPerRole(User, Domain, Role, []*Policy) error
	// ResetFeature resets the features in a domain to their initial state
	ResetFeature(Domain) error
}

IFeatureService is the interface that defines the feature-related operations for backends, frontends and policies

type IService added in v0.1.0

type IService interface {
	IBaseService      // basic CRUD operations for users, domains, objects and roles
	IDirectoryService // directory-related operations for objects and object data
	IFeatureService   // feature-related operations for backends, frontends and policies
	ICurrentService   // current user-related operations
}

IService is the interface that defines all the methods for caskin service

func New

func New(options *Options, opts ...Option) (IService, error)

type InheritanceEdge added in v0.1.0

type InheritanceEdge[T constraints.Ordered] struct {
	U T `json:"u"`
	V T `json:"v"`
}

InheritanceEdge x is node, y is adjacency

func (*InheritanceEdge[T]) Decode added in v0.1.0

func (i *InheritanceEdge[T]) Decode(in string) error

func (*InheritanceEdge[T]) Encode added in v0.1.0

func (i *InheritanceEdge[T]) Encode(u, v T) string

type InheritanceGraph added in v0.1.0

type InheritanceGraph[T constraints.Ordered] map[T][]T

func MergeInheritanceGraph added in v0.1.0

func MergeInheritanceGraph[T constraints.Ordered](graphs ...InheritanceGraph[T]) InheritanceGraph[T]

func (InheritanceGraph[T]) Sort added in v0.1.0

func (g InheritanceGraph[T]) Sort() InheritanceGraph[T]

func (InheritanceGraph[T]) TopSort added in v0.1.0

func (g InheritanceGraph[T]) TopSort() []T

type MetaDB

type MetaDB interface {
	Create(any) error
	Recover(any) error
	Update(any) error
	UpsertType(any) UpsertType
	Take(any) error
	TakeUnscoped(any) error
	Find(any, ...any) error
	DeleteByID(any, uint64) error

	GetUserByID([]uint64) ([]User, error)
	GetRoleInDomain(Domain) ([]Role, error)
	GetRoleByID([]uint64) ([]Role, error)
	GetObjectInDomain(Domain, ...ObjectType) ([]Object, error)
	GetObjectByID([]uint64) ([]Object, error)
	GetDomainByID([]uint64) ([]Domain, error)
	GetAllDomain() ([]Domain, error)
}

type NamedObject added in v0.1.0

type NamedObject struct {
	Name string `json:"name"`
}

NamedObject build in Object for name encode/decode

func (*NamedObject) Decode added in v0.1.0

func (o *NamedObject) Decode(code string) error

func (*NamedObject) Encode added in v0.1.0

func (o *NamedObject) Encode() string

func (*NamedObject) GetDomainID added in v0.1.0

func (o *NamedObject) GetDomainID() uint64

func (*NamedObject) GetID added in v0.1.0

func (o *NamedObject) GetID() uint64

func (*NamedObject) GetObjectType added in v0.1.0

func (o *NamedObject) GetObjectType() string

func (*NamedObject) GetParentID added in v0.1.0

func (o *NamedObject) GetParentID() uint64

func (*NamedObject) SetDomainID added in v0.1.0

func (o *NamedObject) SetDomainID(uint64)

func (*NamedObject) SetID added in v0.1.0

func (o *NamedObject) SetID(uint64)

func (*NamedObject) SetParentID added in v0.1.0

func (o *NamedObject) SetParentID(uint64)

type Object

type Object interface {
	GetObjectType() string
	// contains filtered or unexported methods
}

type ObjectChildrenGetFunc added in v0.1.0

type ObjectChildrenGetFunc = func(Object, Domain) []Object

type ObjectData

type ObjectData interface {

	// GetObjectID get object
	GetObjectID() uint64
	// SetObjectID set object
	SetObjectID(uint64)
	// contains filtered or unexported methods
}

type ObjectDeleteFunc added in v0.1.0

type ObjectDeleteFunc = func(Object, Domain) error

type ObjectParentAddFunc added in v0.1.0

type ObjectParentAddFunc = func(Object, Object, Domain) error

type ObjectParentDelFunc added in v0.1.0

type ObjectParentDelFunc = func(Object, Object, Domain) error

type ObjectParentGetFunc added in v0.1.0

type ObjectParentGetFunc = func(Object, Domain) []Object

type ObjectType

type ObjectType = string
const (
	ObjectTypeRole ObjectType = "role"
)

type Option

type Option func(*Options)

type Options

type Options struct {
	// default caskin option
	DefaultSuperadminDomainName string            `json:"default_superadmin_domain_name"`
	DefaultSuperadminRoleName   string            `json:"default_superadmin_role_name"`
	Dictionary                  *DictionaryOption `json:"dictionary"`
	DB                          *DBOption         `json:"db"`
	Watcher                     *WatcherOption    `json:"watcher"`
}

Options configuration for caskin

type Package added in v0.1.0

type Package struct {
	Key      string     `toml:"key"`
	Feature  [][]string `toml:"feature"`
	Backend  [][]string `toml:"backend"`
	Frontend [][]string `toml:"frontend"`
}

func (*Package) GetName added in v0.1.0

func (p *Package) GetName() string

type Policy

type Policy struct {
	Role   Role   `json:"role"`
	Object Object `json:"object"`
	Domain Domain `json:"domain"`
	Action Action `json:"action"`
}

Policy tuple of role-object-domain-action

func DiffPolicy

func DiffPolicy(source, target []*Policy) (add, remove []*Policy)

DiffPolicy diff policy source, target list to get add, remove list

func (*Policy) Key

func (p *Policy) Key() string

Key get the unique identify of the policy

type Role

type Role interface {
	ObjectData
	// contains filtered or unexported methods
}

func GetSuperadminRole added in v0.1.0

func GetSuperadminRole() Role

type SampleSuperadminDomain added in v0.1.0

type SampleSuperadminDomain struct {
	ID   uint64 `json:"id"`
	Name string `json:"name"`
}

func (*SampleSuperadminDomain) Decode added in v0.1.0

func (s *SampleSuperadminDomain) Decode(code string) error

func (*SampleSuperadminDomain) Encode added in v0.1.0

func (s *SampleSuperadminDomain) Encode() string

func (*SampleSuperadminDomain) GetID added in v0.1.0

func (s *SampleSuperadminDomain) GetID() uint64

func (*SampleSuperadminDomain) SetID added in v0.1.0

func (s *SampleSuperadminDomain) SetID(uint64)

type SampleSuperadminRole

type SampleSuperadminRole struct {
	ID   uint64 `json:"id"`
	Name string `json:"name"`
}

func (*SampleSuperadminRole) Decode

func (s *SampleSuperadminRole) Decode(code string) error

func (*SampleSuperadminRole) Encode

func (s *SampleSuperadminRole) Encode() string

func (*SampleSuperadminRole) GetDomainID

func (s *SampleSuperadminRole) GetDomainID() uint64

func (*SampleSuperadminRole) GetID

func (s *SampleSuperadminRole) GetID() uint64

func (*SampleSuperadminRole) GetObjectID added in v0.1.0

func (s *SampleSuperadminRole) GetObjectID() uint64

func (*SampleSuperadminRole) SetDomainID

func (s *SampleSuperadminRole) SetDomainID(uint64)

func (*SampleSuperadminRole) SetID

func (s *SampleSuperadminRole) SetID(uint64)

func (*SampleSuperadminRole) SetObjectID

func (s *SampleSuperadminRole) SetObjectID(uint64)

type UpsertType

type UpsertType string
const (
	UpsertTypeCreate  UpsertType = "create"
	UpsertTypeRecover UpsertType = "recover"
	UpsertTypeUpdate  UpsertType = "update"
)

type User

type User interface {
	// contains filtered or unexported methods
}

type UserRolePair

type UserRolePair struct {
	User User `json:"user"`
	Role Role `json:"role"`
}

UserRolePair pair of user and role

type WatcherOption added in v0.2.0

type WatcherOption struct {
	Type     string `json:"type"`
	Address  string `json:"address"`
	Password string `json:"password"`
	Channel  string `json:"channel"`
	AutoLoad int64  `json:"auto_load"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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