use super::{Decision, DecisionReason};
pub struct DecisionCombinator;
impl DecisionCombinator {
pub fn combine(decisions: &[Decision]) -> Decision {
if decisions.is_empty() {
return Decision::deny(DecisionReason::ExplicitDeny);
}
let mut first_deny: Option<&Decision> = None;
for d in decisions {
if d.is_forbid() {
return d.clone();
}
if !d.allowed && first_deny.is_none() {
first_deny = Some(d);
}
}
if let Some(d) = first_deny {
return d.clone();
}
Decision::allow()
}
}
#[cfg(test)]
mod tests {
use super::super::DecisionReason;
use super::*;
#[test]
fn combine_returns_forbid_when_any_forbid() {
let decisions = vec![
Decision::allow(),
Decision::forbid("r1"),
Decision::deny(DecisionReason::NoMatchingPermission),
];
let result = DecisionCombinator::combine(&decisions);
assert!(!result.allowed, "含 Forbid 时结果应为拒绝");
assert!(result.is_forbid(), "含 Forbid 时结果 reason 应为 Forbid");
assert_eq!(
result.reason,
DecisionReason::Forbid("r1".to_string()),
"应返回第一个 Forbid"
);
}
#[test]
fn combine_returns_deny_when_no_forbid_but_has_deny() {
let decisions = vec![
Decision::allow(),
Decision::deny(DecisionReason::NoMatchingPermission),
Decision::allow(),
];
let result = DecisionCombinator::combine(&decisions);
assert!(!result.allowed, "含 Deny 时结果应为拒绝");
assert!(!result.is_forbid(), "无 Forbid 时结果 reason 不应为 Forbid");
assert_eq!(
result.reason,
DecisionReason::NoMatchingPermission,
"应返回第一个 Deny 的 reason"
);
}
#[test]
fn combine_returns_allow_when_all_allow() {
let decisions = vec![Decision::allow(), Decision::allow()];
let result = DecisionCombinator::combine(&decisions);
assert!(result.allowed, "全部 Allow 时结果应为允许");
assert_eq!(
result.reason,
DecisionReason::ExplicitAllow,
"全部 Allow 时 reason 应为 ExplicitAllow"
);
}
#[test]
fn combine_returns_deny_for_empty_list() {
let decisions: Vec<Decision> = vec![];
let result = DecisionCombinator::combine(&decisions);
assert!(!result.allowed, "空列表应返回 Deny(fail-closed)");
assert_eq!(
result.reason,
DecisionReason::ExplicitDeny,
"空列表 reason 应为 ExplicitDeny"
);
}
#[test]
fn combine_returns_first_forbid() {
let decisions = vec![Decision::forbid("r1"), Decision::forbid("r2")];
let result = DecisionCombinator::combine(&decisions);
assert!(result.is_forbid(), "多个 Forbid 时结果应为 Forbid");
assert_eq!(
result.reason,
DecisionReason::Forbid("r1".to_string()),
"应返回第一个 Forbid(r1)"
);
}
#[test]
fn combine_returns_first_deny() {
let decisions = vec![
Decision::deny(DecisionReason::NoMatchingPermission),
Decision::deny(DecisionReason::ExplicitDeny),
];
let result = DecisionCombinator::combine(&decisions);
assert!(!result.allowed, "多个 Deny 时结果应为拒绝");
assert!(!result.is_forbid(), "无 Forbid 时结果不应为 Forbid");
assert_eq!(
result.reason,
DecisionReason::NoMatchingPermission,
"应返回第一个 Deny 的 reason(NoMatchingPermission)"
);
}
#[test]
fn combine_forbid_plus_deny() {
let decisions = vec![
Decision::forbid("r"),
Decision::deny(DecisionReason::NoMatchingPermission),
];
let result = DecisionCombinator::combine(&decisions);
assert!(result.is_forbid(), "Forbid + Deny 时应返回 Forbid");
assert_eq!(
result.reason,
DecisionReason::Forbid("r".to_string()),
"应返回 Forbid(\"r\")"
);
}
#[test]
fn combine_deny_plus_allow() {
let decisions = vec![
Decision::deny(DecisionReason::NoMatchingPermission),
Decision::allow(),
];
let result = DecisionCombinator::combine(&decisions);
assert!(!result.allowed, "Deny + Allow 时应返回 Deny");
assert!(!result.is_forbid(), "无 Forbid 时结果不应为 Forbid");
assert_eq!(
result.reason,
DecisionReason::NoMatchingPermission,
"应返回 Deny 的 reason(NoMatchingPermission)"
);
}
#[test]
fn combine_deny_before_forbid_still_returns_forbid() {
let decisions = vec![
Decision::deny(DecisionReason::NoMatchingPermission),
Decision::forbid("restricted-resource"),
];
let result = DecisionCombinator::combine(&decisions);
assert!(
result.is_forbid(),
"Deny 在前、Forbid 在后时仍应返回 Forbid(Forbid 最高优先级)"
);
assert_eq!(
result.reason,
DecisionReason::Forbid("restricted-resource".to_string()),
"应返回 Forbid 的 reason"
);
}
}