跳转至主要内容

Effector

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

MergeEffects()​

MergeEffects() 接收所有匹配策略的效果,得出最终的允许 / 拒绝结果。

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)

对于标准的允许优先效果,只要有匹配的策略允许且没有拒绝,结果就是允许。自定义 effector 可以实现其他语义(例如拒绝优先、优先级)。

备注

除非效果表达式是 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.