permission

package
v0.1.9 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2026 License: Apache-2.0 Imports: 4 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.

Documentation

Overview

Package permission 统一评估OFD文档权限声明。它不提供加密或操作系统级防复制能力。

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrDenied 表示声明拒绝操作或文档不在有效期内。
	ErrDenied = errors.New("文档权限不允许此操作")
	// ErrAction 表示未知操作;即使显式忽略声明也不接受未知操作。
	ErrAction = errors.New("未知权限操作")
	// ErrMode 表示无效执行策略。
	ErrMode = errors.New("无效权限执行策略")
)

Functions

func ValidAction

func ValidAction(action Action) bool

ValidAction 报告是否为支持的操作。

Types

type Action

type Action string

Action 是待执行操作;Copy按Export声明检查,不引入非标准Copy字段。

const (
	Access      Action = "access"
	Copy        Action = "copy"
	Export      Action = "export"
	Print       Action = "print"
	Edit        Action = "edit"
	Annot       Action = "annot"
	Signature   Action = "signature"
	Watermark   Action = "watermark"
	PrintScreen Action = "print_screen"
)

type DeniedError

type DeniedError struct {
	Action Action
	Scope  int
	Reason Reason
}

DeniedError 保留操作、文档体索引及拒绝原因,可用errors.As读取。

func (*DeniedError) Error

func (e *DeniedError) Error() string

func (*DeniedError) Unwrap

func (e *DeniedError) Unwrap() error

type Mode

type Mode uint8

Mode 指定是否执行声明。忽略声明仅供明确授权的诊断/修复,不应从不可信输入自动启用。

const (
	Enforce Mode = iota
	IgnoreDeclarations
)

type Policy

type Policy struct {
	Mode Mode
	Now  func() time.Time
}

Policy 是可复用的执行策略,零值默认执行声明并使用time.Now。 Now每次检查时调用,必须支持并发且不得重入持有此策略的组件。

func (Policy) Check

func (p Policy) Check(d *spec.Permissions, action Action, scope int) error

Check 按§7.5表8检查有效期与操作声明。起止时刻均包含端点;空/倒置有效期拒绝。 此方法不缓存判断;有效期约束所有操作,普通Access不受Export/Print限制。 nil声明及省略的布尔字段默认允许;检查不修改原始声明,也不缓存授权结果。 调用期间不得并发修改d;长任务需要固定原始声明时可先使用d.Clone()。

Example
package main

import (
	"encoding/xml"
	"errors"
	"fmt"

	"github.com/zc310/ofd/pkg/permission"
	"github.com/zc310/ofd/pkg/spec"
)

func main() {
	var declaration spec.Permissions
	if err := xml.Unmarshal([]byte(`<Permissions><Export>false</Export></Permissions>`), &declaration); err != nil {
		panic(err)
	}
	policy := permission.Policy{}
	fmt.Println(policy.Check(nil, permission.Export, 0))
	fmt.Println(errors.Is(policy.Check(&declaration, permission.Export, 0), permission.ErrDenied))
	fmt.Println(policy.Check(&declaration, permission.Access, 0))
}
Output:
<nil>
true
<nil>

func (Policy) Validate

func (p Policy) Validate() error

Validate 检查策略配置,不调用时钟。

type Reason

type Reason string

Reason 是拒绝的稳定机器可读原因。

const (
	Forbidden     Reason = "forbidden"
	NotYetValid   Reason = "not_yet_valid"
	Expired       Reason = "expired"
	InvalidPeriod Reason = "invalid_period"
)

Jump to

Keyboard shortcuts

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