datascope

package
v0.2.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: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNoRole indicates that the user has no roles in the acting domain and should not be granted data access.
	ErrNoRole = errors.New("no roles in acting domain")
)
View Source
var ScopePriority = map[ScopeType]int{
	ScopeAll:  2,
	ScopeSelf: 1,
}

ScopePriority 范围优先级(用于合并时取最大)

Functions

func WithFilterCondition

func WithFilterCondition(ctx context.Context, fc *FilterCondition) context.Context

WithFilterCondition 注入过滤条件到 context

Types

type FilterCondition

type FilterCondition struct {
	Type   ScopeType `json:"type"`
	UserID uuid.UUID `json:"userId,omitempty"`
}

FilterCondition 数据过滤条件

func GetFilterCondition

func GetFilterCondition(ctx context.Context) *FilterCondition

GetFilterCondition 从 context 获取过滤条件

func MustGetFilterCondition

func MustGetFilterCondition(ctx context.Context) *FilterCondition

MustGetFilterCondition 必须获取过滤条件

func (*FilterCondition) IsUnrestricted

func (fc *FilterCondition) IsUnrestricted() bool

IsUnrestricted 是否无限制

type QueryBuilder

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

QueryBuilder 数据范围 SQL 过滤构建器。

func NewQueryBuilder

func NewQueryBuilder(fc *FilterCondition) *QueryBuilder

NewQueryBuilder creates a new QueryBuilder.

func (*QueryBuilder) BuildPredicate

func (qb *QueryBuilder) BuildPredicate(userIDColumn string) func(*sql.Selector)

BuildPredicate 构建通用 SQL 谓词。

type ScopeResolver

type ScopeResolver interface {
	ScopeTypes() []ScopeType
	Resolve(
		ctx context.Context,
		userID uuid.UUID,
		domainID uuid.UUID,
		scopeType ScopeType,
		scopeValue string,
	) (*FilterCondition, error)
}

ScopeResolver resolves plugin-defined scope types to filter conditions.

type ScopeType

type ScopeType string

ScopeType 数据范围类型

const (
	ScopeAll       ScopeType = "ALL"        // 当前域内全部数据
	ScopeSelf      ScopeType = "SELF"       // 仅自己创建的数据
	ScopeOUSelf    ScopeType = "OU_SELF"    // 当前用户主组织数据
	ScopeOUSubtree ScopeType = "OU_SUBTREE" // 当前用户主组织及其子树数据
)

func (ScopeType) IsKnown

func (s ScopeType) IsKnown() bool

IsKnown reports whether the scope type is supported by current runtime.

type Service

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

Service 数据范围服务

func NewService

func NewService(enforcer *casbin.Enforcer, db *ent.Client) *Service

NewService 创建数据范围服务

func (*Service) GetUserDataScope

func (s *Service) GetUserDataScope(
	ctx context.Context,
	userID uuid.UUID,
	domain string,
	resourceKey string,
) (*FilterCondition, error)

GetUserDataScope resolves the user's data scope for a given resource in the specified domain.

func (*Service) RegisterScopeResolver

func (s *Service) RegisterScopeResolver(r ScopeResolver)

RegisterScopeResolver registers a custom scope resolver by scope type.

Jump to

Keyboard shortcuts

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