1use serde::{Deserialize, Serialize};
2
3use crate::condition::Condition;
4use crate::resource::{Anchor, GLOBAL, Resource};
5use crate::subject::Subject;
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
8#[serde(rename_all = "snake_case")]
9pub enum Effect {
10 Allow,
11 Deny,
12}
13
14#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
16#[serde(tag = "kind", rename_all = "snake_case")]
17pub enum SubjectMatcher {
18 User { id: String },
20 Role { id: String },
22 Principal { name: String },
24 Relation { relation: String },
26 Any,
28}
29
30impl SubjectMatcher {
31 pub fn user(id: impl Into<String>) -> Self {
32 Self::User { id: id.into() }
33 }
34
35 pub fn role(id: impl Into<String>) -> Self {
36 Self::Role { id: id.into() }
37 }
38
39 pub fn principal(name: impl Into<String>) -> Self {
40 Self::Principal { name: name.into() }
41 }
42
43 pub fn relation(relation: impl Into<String>) -> Self {
44 Self::Relation {
45 relation: relation.into(),
46 }
47 }
48}
49
50#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
52pub struct TargetSelector {
53 pub kind: String,
54 #[serde(default, skip_serializing_if = "Option::is_none")]
55 pub id: Option<String>,
56}
57
58impl TargetSelector {
59 pub fn global() -> Self {
60 Self::kind(GLOBAL)
61 }
62
63 pub fn kind(kind: impl Into<String>) -> Self {
64 Self {
65 kind: kind.into(),
66 id: None,
67 }
68 }
69
70 pub fn exact(kind: impl Into<String>, id: impl Into<String>) -> Self {
71 Self {
72 kind: kind.into(),
73 id: Some(id.into()),
74 }
75 }
76
77 pub fn matches(&self, anchor: &Anchor) -> bool {
78 self.kind == anchor.kind && (self.id.is_none() || self.id == anchor.id)
79 }
80}
81
82impl From<&Anchor> for TargetSelector {
83 fn from(anchor: &Anchor) -> Self {
84 Self {
85 kind: anchor.kind.clone(),
86 id: anchor.id.clone(),
87 }
88 }
89}
90
91#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
93#[serde(rename_all = "camelCase")]
94pub struct Grant {
95 pub id: String,
96 pub subject: SubjectMatcher,
97 pub permission: String,
98 pub target: TargetSelector,
99 pub effect: Effect,
100 #[serde(default, skip_serializing_if = "Option::is_none")]
103 pub scope: Option<Anchor>,
104 #[serde(default, skip_serializing_if = "Option::is_none")]
106 pub condition: Option<Condition>,
107}
108
109impl Grant {
110 pub fn new(
111 id: impl Into<String>,
112 effect: Effect,
113 subject: SubjectMatcher,
114 permission: impl Into<String>,
115 target: TargetSelector,
116 ) -> Self {
117 Self {
118 id: id.into(),
119 subject,
120 permission: permission.into(),
121 target,
122 effect,
123 scope: None,
124 condition: None,
125 }
126 }
127
128 pub fn allow(
129 id: impl Into<String>,
130 subject: SubjectMatcher,
131 permission: impl Into<String>,
132 target: TargetSelector,
133 ) -> Self {
134 Self::new(id, Effect::Allow, subject, permission, target)
135 }
136
137 pub fn deny(
138 id: impl Into<String>,
139 subject: SubjectMatcher,
140 permission: impl Into<String>,
141 target: TargetSelector,
142 ) -> Self {
143 Self::new(id, Effect::Deny, subject, permission, target)
144 }
145
146 pub fn scoped(mut self, scope: Anchor) -> Self {
147 self.scope = Some(scope);
148 self
149 }
150
151 pub fn when(mut self, condition: Condition) -> Self {
152 self.condition = Some(condition);
153 self
154 }
155}
156
157pub trait GrantSource {
163 fn visit(&self, subject: &Subject, resource: &Resource, visitor: &mut dyn FnMut(&Grant));
164}
165
166impl GrantSource for [Grant] {
167 fn visit(&self, _: &Subject, _: &Resource, visitor: &mut dyn FnMut(&Grant)) {
168 self.iter().for_each(visitor);
169 }
170}
171
172impl GrantSource for Vec<Grant> {
173 fn visit(&self, subject: &Subject, resource: &Resource, visitor: &mut dyn FnMut(&Grant)) {
174 self.as_slice().visit(subject, resource, visitor);
175 }
176}
177
178impl<T: GrantSource + ?Sized> GrantSource for &T {
179 fn visit(&self, subject: &Subject, resource: &Resource, visitor: &mut dyn FnMut(&Grant)) {
180 (**self).visit(subject, resource, visitor);
181 }
182}
183
184impl<A: GrantSource, B: GrantSource> GrantSource for (A, B) {
186 fn visit(&self, subject: &Subject, resource: &Resource, visitor: &mut dyn FnMut(&Grant)) {
187 self.0.visit(subject, resource, visitor);
188 self.1.visit(subject, resource, visitor);
189 }
190}