跳到主要内容

Effector

Effect 是策略规则的结果(允许或拒绝)。Effector 接口定义了 enforcer 如何将多个匹配的效果合并为单个决策。

MergeEffects()​

MergeEffects() 从匹配的策略中获取所有效果,并生成一个最终的 allow/deny 结果。

Effect, explainIndex, err = e.MergeEffects(expr, effects, matches, policyIndex, policyLength)

参数:

参数描述
expr策略效果表达式字符串(例如来自模型)。
effects效果切片:Allow、Deny 或 Indeterminate。
matches哪些策略索引匹配了。
policyIndex当前策略索引。
policyLength策略总数。

返回值:

  • Effect — 最终决策(Allow、Deny 或 Indeterminate)。
  • explainIndex — 决定结果的策略索引(如果不适用则为 -1)。
  • err — 如果 effector 不支持该表达式,则非 nil。

使用自定义 Effector:

var e Effector
Effect, explainIndex, err = e.MergeEffects(expr, effects, matches, policyIndex, policyLength)

对于标准的 allow-override 效果,如果任何匹配的策略允许且没有策略拒绝,则结果为允许。自定义 effector 实现其他语义(例如 deny-override、priority)。

备注

除非效果表达式是 priority(p_eft) || deny,否则 enforcer 可能会短路:一旦确定结果,剩余的策略将不再评估。因此,当 policyIndex < policyLength-1 时,某些效果可能会被跳过。

Comments are powered by giscus and GitHub Discussions. They load from giscus.app and GitHub only after you click the button below.