acl

package
v2.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2026 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package acl 是项目知识库的授权判定(#114,设计稿 06 §7):一个不碰数据库的纯函数。

调用方(MCP 分发层、REST)负责把「谁在调用、碰的是什么、库里有哪些有效授权」整理成 Request, 这里只回答「够不够」以及「依据哪条规则」。这样规则表可以用表驱动测试穷举,不依赖任何存储。

明确不是安全边界:身份是调用方自报的,这套规则防的是误操作、越权写和「拿着别人的 ID 就改」; 真正的边界仍然是 Token。

Index

Constants

View Source
const (
	ModeOff     = "off"
	ModeAudit   = "audit"
	ModeEnforce = "enforce"
)

档位:off 不判定;audit 只记录「若启用会被拒绝」的调用;enforce 真正拦截。

View Source
const (
	// KindDoc 是项目知识库里的文档(含 sessions/<工作流>/ 下的)。
	KindDoc = "doc"
	// KindModule 是模块本身(元数据、负责人、授权)。
	KindModule = "module"
	// KindProject 是项目级(目录、模块树)。
	KindProject = "project"
	// KindSession 是某个工作流的会话数据:任务、历史、检查点等(不含收件箱)。
	KindSession = "session"
	// KindInbox 是某个工作流的收件箱:读取会把消息标成已送达,等于写,所以只有它的所有者能碰。
	KindInbox = "inbox"
	// KindTasks 是项目任务板的管理权(#175 B2c):派任务、改所有人的任务。它只认显式授权——
	// 没有「同项目只读」「负责人」「知道编号」这些隐含规则,没被授权的 Agent 一律不是管理 Agent。
	KindTasks = "tasks"
)

资源种类。

View Source
const (
	ScopeProject  = "project"
	ScopeModule   = "module"
	ScopeDoc      = "doc"
	ScopeWorkflow = "workflow"
	// ScopeTasks 只覆盖 KindTasks:给了任务管理权,不等于能编辑文档。
	ScopeTasks = "tasks"
)

授权范围种类(与 model.Scope* 同值)。

View Source
const (
	RuleOff      = "off"
	RuleHuman    = "R1"
	RuleProject  = "R2"
	RuleOwner    = "R3"
	RuleKnownID  = "R4"
	RuleGrant    = "grant"
	RuleInbox    = "inbox-owner"
	RuleNoAccess = "R7"
)

规则编号,对应设计稿 06 §7.3。

View Source
const MaxGrantRole = RoleEdit

MaxGrantRole 是授权能给出的最高角色:manage 不能转授。

Variables

This section is empty.

Functions

This section is empty.

Types

type Caller

type Caller struct {
	Human bool
	// IdentityID 是 Agent 身份编号;0 表示认不出。
	IdentityID uint
	// ProjectID 是调用方身份所在目录归属的项目;0 表示不属于任何项目。
	ProjectID uint
}

Caller 是调用方。

type Decision

type Decision struct {
	Allow bool
	// Rule 是起决定作用的规则:放行时是给出最高权限的那条,拒绝时是 R7。
	Rule string
	// Have 是调用方在这个资源上得到的最高角色。
	Have Role
	// Reason 是给人和 Agent 看的一句话(拒绝时说明缺什么、怎么补)。
	Reason string
}

Decision 是判定结果。

func Authorize

func Authorize(r Request) Decision

Authorize 按设计稿 R1–R7 判定一次访问。

func CanManageTasks

func CanManageTasks(identityID, projectID uint, grants []Grant) Decision

CanManageTasks 回答「这个身份是不是这个项目的任务管理 Agent」(#175 B2c): 持有这个项目的 tasks 范围授权,或项目范围、角色在 edit 及以上的授权(项目级编辑本来就包含改所有任务)。 一律按 enforce 判定(项目在 off / audit 档时它只用来决定「若启用强制会怎样」,不能因为档位放宽管理权); 没有可识别的身份、没有授权都返回不允许。grants 是调用方已经过滤好的有效授权。

type Grant

type Grant struct {
	ProjectID uint
	ScopeKind string
	ScopeRef  string
	Role      Role
}

Grant 是一条已经过滤过的有效授权(没撤销、没过期、主体就是调用方或调用方所在的群)。

type Request

type Request struct {
	Mode   string
	Caller Caller
	Res    Resource
	// Need 是这次操作需要的最低角色:读 = read,写 = edit,授权 / 改负责人 = manage。
	Need   Role
	Grants []Grant
}

Request 是一次判定的全部输入。

type Resource

type Resource struct {
	Kind       string
	ProjectID  uint // 资源所在项目;0 = 不属于任何项目
	ModulePath string
	DocPath    string
	DocID      uint
	WorkflowID string
	// OwnerIdentityID 是文档自己的负责人。
	OwnerIdentityID uint
	// ModuleOwnerID 是文档所在模块(或模块本身)的负责人。
	ModuleOwnerID uint
	// WorkflowIdentityID 是资源所属工作流绑定的身份(自己的 sessions/<工作流>/、任务、收件箱都算它的)。
	WorkflowIdentityID uint
}

Resource 是被访问的东西,字段由调用方从库里查出来。

type Role

type Role int

Role 是权限等级,逐级包含:read ⊂ comment ⊂ edit ⊂ manage。

const (
	RoleNone Role = iota
	RoleRead
	RoleComment
	RoleEdit
	RoleManage
)

func ParseRole

func ParseRole(s string) Role

ParseRole 把字符串角色转成 Role;不认识的返回 RoleNone。

func (Role) String

func (r Role) String() string

Jump to

Keyboard shortcuts

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