khive_capability/
validate.rs1use lion_core::{CapId, PluginId, Right};
6
7use crate::capability::CapabilityManager;
8use crate::error::KernelError;
9
10pub struct RequestValidator<'a> {
11 caps: &'a CapabilityManager,
12}
13
14impl<'a> RequestValidator<'a> {
15 pub fn new(caps: &'a CapabilityManager) -> Self {
16 Self { caps }
17 }
18
19 pub fn check(
20 &self,
21 plugin_id: PluginId,
22 cap_id: CapId,
23 namespace: &str,
24 operation: Operation,
25 ) -> Result<(), KernelError> {
26 let right = operation.required_right();
27 self.caps.validate(plugin_id, cap_id, namespace, right)
28 }
29}
30
31#[derive(Debug, Clone, Copy)]
32pub enum Operation {
33 Read,
34 Create,
35 Update,
36 Delete,
37 Search,
38 Link,
39 Traverse,
40 Send,
41 Recall,
42 Remember,
43 Assign,
44 Complete,
45 Export,
47}
48
49impl Operation {
50 pub fn required_right(self) -> Right {
51 match self {
52 Self::Read | Self::Search | Self::Traverse | Self::Recall | Self::Export => Right::Read,
53 Self::Create | Self::Remember | Self::Assign | Self::Link => Right::Create,
54 Self::Update | Self::Complete => Right::Write,
55 Self::Delete => Right::Delete,
56 Self::Send => Right::Send,
57 }
58 }
59
60 pub fn binding_right(self) -> Right {
66 match self {
67 Self::Read | Self::Search | Self::Traverse | Self::Recall | Self::Export => Right::Read,
68 Self::Create
69 | Self::Remember
70 | Self::Assign
71 | Self::Link
72 | Self::Update
73 | Self::Complete
74 | Self::Delete
75 | Self::Send => Right::Write,
76 }
77 }
78}
79
80#[cfg(test)]
81mod tests {
82 use super::*;
83
84 #[test]
89 fn binding_right_splits_read_vs_write() {
90 let reads = [
91 Operation::Read,
92 Operation::Search,
93 Operation::Traverse,
94 Operation::Recall,
95 Operation::Export,
96 ];
97 let writes = [
98 Operation::Create,
99 Operation::Remember,
100 Operation::Assign,
101 Operation::Link,
102 Operation::Update,
103 Operation::Complete,
104 Operation::Delete,
105 Operation::Send,
106 ];
107 for op in reads {
108 assert_eq!(op.binding_right(), Right::Read, "{op:?} must need Read");
109 }
110 for op in writes {
111 assert_eq!(op.binding_right(), Right::Write, "{op:?} must need Write");
112 }
113 }
114}