scim_server/resource/
context.rs1use crate::resource::tenant::{IsolationLevel, TenantContext};
7use uuid::Uuid;
8
9#[derive(Debug, Clone)]
14pub struct RequestContext {
15 pub request_id: String,
17 pub tenant_context: Option<TenantContext>,
19}
20
21impl RequestContext {
22 pub fn new(request_id: String) -> Self {
24 Self {
25 request_id,
26 tenant_context: None,
27 }
28 }
29
30 pub fn with_generated_id() -> Self {
32 Self {
33 request_id: Uuid::new_v4().to_string(),
34 tenant_context: None,
35 }
36 }
37
38 pub fn with_tenant(request_id: String, tenant_context: TenantContext) -> Self {
40 Self {
41 request_id,
42 tenant_context: Some(tenant_context),
43 }
44 }
45
46 pub fn with_tenant_generated_id(tenant_context: TenantContext) -> Self {
48 Self {
49 request_id: Uuid::new_v4().to_string(),
50 tenant_context: Some(tenant_context),
51 }
52 }
53
54 pub fn tenant_id(&self) -> Option<&str> {
56 self.tenant_context.as_ref().map(|t| t.tenant_id.as_str())
57 }
58
59 pub fn client_id(&self) -> Option<&str> {
61 self.tenant_context.as_ref().map(|t| t.client_id.as_str())
62 }
63
64 pub fn is_multi_tenant(&self) -> bool {
66 self.tenant_context.is_some()
67 }
68
69 pub fn isolation_level(&self) -> Option<&IsolationLevel> {
71 self.tenant_context.as_ref().map(|t| &t.isolation_level)
72 }
73
74 pub fn can_perform_operation(&self, operation: &str) -> bool {
76 match &self.tenant_context {
77 Some(tenant) => tenant.can_perform_operation(operation),
78 None => true, }
80 }
81
82 pub fn validate_operation(&self, operation: &str) -> Result<(), String> {
84 if self.can_perform_operation(operation) {
85 Ok(())
86 } else {
87 Err(format!(
88 "Operation '{}' not permitted for tenant",
89 operation
90 ))
91 }
92 }
93}
94
95impl Default for RequestContext {
96 fn default() -> Self {
97 Self::with_generated_id()
98 }
99}
100
101#[derive(Debug, Clone, Default)]
106pub struct ListQuery {
107 pub count: Option<usize>,
109 pub start_index: Option<usize>,
111 pub filter: Option<String>,
113 pub attributes: Vec<String>,
115 pub excluded_attributes: Vec<String>,
117}
118
119impl ListQuery {
120 pub fn new() -> Self {
122 Self::default()
123 }
124
125 pub fn with_count(mut self, count: usize) -> Self {
127 self.count = Some(count);
128 self
129 }
130
131 pub fn with_start_index(mut self, start_index: usize) -> Self {
133 self.start_index = Some(start_index);
134 self
135 }
136
137 pub fn with_filter(mut self, filter: String) -> Self {
139 self.filter = Some(filter);
140 self
141 }
142
143 pub fn with_attribute(mut self, attribute: String) -> Self {
145 self.attributes.push(attribute);
146 self
147 }
148
149 pub fn with_attributes(mut self, attributes: Vec<String>) -> Self {
151 self.attributes.extend(attributes);
152 self
153 }
154
155 pub fn with_excluded_attribute(mut self, attribute: String) -> Self {
157 self.excluded_attributes.push(attribute);
158 self
159 }
160
161 pub fn with_excluded_attributes(mut self, attributes: Vec<String>) -> Self {
163 self.excluded_attributes.extend(attributes);
164 self
165 }
166}