1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
//! # Secure ORM Layer Documentation
//!
//! The secure ORM layer provides type-safe, scoped access to database entities using `SeaORM`.
//! It enforces an implicit security policy that prevents unscoped queries from executing.
//!
//! ## Core Concepts
//!
//! ### 1. `AccessScope`
//!
//! The [`AccessScope`](crate::secure::AccessScope) struct defines the security boundary:
//!
//! ```rust
//! use modkit_db::secure::{AccessScope, ScopeConstraint, ScopeFilter, pep_properties};
//! use uuid::Uuid;
//!
//! let tenant_id = Uuid::new_v4();
//! let resource_id = Uuid::new_v4();
//!
//! // Scope to specific tenants
//! let scope = AccessScope::for_tenants(vec![tenant_id]);
//!
//! // Scope to specific resources
//! let scope = AccessScope::for_resources(vec![resource_id]);
//!
//! // Scope to both (AND relationship – single constraint with two filters)
//! let scope = AccessScope::single(ScopeConstraint::new(vec![
//! ScopeFilter::in_uuids(pep_properties::OWNER_TENANT_ID, vec![tenant_id]),
//! ScopeFilter::in_uuids(pep_properties::RESOURCE_ID, vec![resource_id]),
//! ]));
//!
//! // Empty scope (will deny all)
//! let scope = AccessScope::default();
//! ```
//!
//! ### 2. `ScopableEntity`
//!
//! Entities must implement [`ScopableEntity`](crate::secure::ScopableEntity) to declare
//! which columns are used for scoping:
//!
//! ```rust,ignore
//! use modkit_db::secure::ScopableEntity;
//!
//! impl ScopableEntity for user::Entity {
//! fn tenant_col() -> Option<Self::Column> {
//! Some(user::Column::TenantId) // Multi-tenant entity
//! }
//! fn resource_col() -> Option<Self::Column> {
//! Some(user::Column::Id)
//! }
//! fn owner_col() -> Option<Self::Column> {
//! None
//! }
//! fn type_col() -> Option<Self::Column> {
//! None
//! }
//! }
//!
//! // Global entity (no tenant scoping)
//! impl ScopableEntity for system_config::Entity {
//! fn tenant_col() -> Option<Self::Column> {
//! None // Global entity
//! }
//! fn resource_col() -> Option<Self::Column> {
//! Some(system_config::Column::Id)
//! }
//! fn owner_col() -> Option<Self::Column> {
//! None
//! }
//! fn type_col() -> Option<Self::Column> {
//! None
//! }
//! }
//! ```
//!
//! ### 3. Typestate-Based Queries
//!
//! The [`SecureSelect`](crate::secure::SecureSelect) wrapper uses typestates to prevent
//! executing unscoped queries at compile time:
//!
//! ```rust,ignore
//! use modkit_db::secure::{AccessScope, SecureEntityExt};
//!
//! // This works ✓
//! let users = user::Entity::find()
//! .secure() // Returns SecureSelect<E, Unscoped>
//! .scope_with(&scope)? // Returns SecureSelect<E, Scoped>
//! .all(conn) // Now can execute
//! .await?;
//!
//! // This won't compile ✗
//! let users = user::Entity::find()
//! .secure()
//! .all(conn); // ERROR: method not found in `SecureSelect<E, Unscoped>`
//! ```
//!
//! ## Implicit Security Policy
//!
//! The layer enforces these rules automatically:
//!
//! | Scope Condition | SQL Result |
//! |----------------|------------|
//! | Empty (no tenant, no resource) | `WHERE 1=0` (deny all) |
//! | Tenants only | `WHERE tenant_id IN (...)` |
//! | Tenants only + entity has no `tenant_col` | `WHERE 1=0` (deny all) |
//! | Resources only | `WHERE resource_col IN (...)` |
//! | Both tenants and resources | `WHERE tenant_col IN (...) AND resource_col IN (...)` |
//!
//! ## Usage Examples
//!
//! ### Example 1: List users for a tenant
//!
//! ```rust,ignore
//! use modkit_db::secure::{AccessScope, SecureEntityExt};
//!
//! pub async fn list_tenant_users(
//! db: &modkit_db::secure::SecureConn,
//! tenant_id: Uuid,
//! ) -> Result<Vec<user::Model>, anyhow::Error> {
//! let scope = AccessScope::for_tenants(vec![tenant_id]);
//!
//! let users = user::Entity::find()
//! .secure()
//! .scope_with(&scope)?
//! .all(db)
//! .await?;
//!
//! Ok(users)
//! }
//! ```
//!
//! ### Example 2: Get specific user by ID (with tenant check)
//!
//! ```rust,ignore
//! use modkit_db::secure::{AccessScope, SecureEntityExt};
//!
//! pub async fn get_user(
//! db: &modkit_db::secure::SecureConn,
//! tenant_id: Uuid,
//! user_id: Uuid,
//! ) -> Result<Option<user::Model>, anyhow::Error> {
//! // This ensures the user belongs to the tenant (implicit AND)
//! let scope = AccessScope::single(ScopeConstraint::new(vec![
//! ScopeFilter::in_uuids(pep_properties::OWNER_TENANT_ID, vec![tenant_id]),
//! ScopeFilter::in_uuids(pep_properties::RESOURCE_ID, vec![user_id]),
//! ]));
//!
//! let user = user::Entity::find()
//! .secure()
//! .scope_with(&scope)?
//! .one(db)
//! .await?;
//!
//! Ok(user)
//! }
//! ```
//!
//! ### Example 3: List specific resources regardless of tenant
//!
//! ```rust,ignore
//! // Useful for admin operations or cross-tenant reports
//! pub async fn get_users_by_ids(
//! db: &modkit_db::secure::SecureConn,
//! user_ids: Vec<Uuid>,
//! ) -> Result<Vec<user::Model>, anyhow::Error> {
//! let scope = AccessScope::for_resources(user_ids);
//!
//! let users = user::Entity::find()
//! .secure()
//! .scope_with(&scope)?
//! .all(db)
//! .await?;
//!
//! Ok(users)
//! }
//! ```
//!
//! ### Example 4: Additional filtering after scoping
//!
//! ```rust,ignore
//! use sea_orm::{ColumnTrait, QueryFilter};
//!
//! pub async fn list_active_users(
//! db: &modkit_db::secure::SecureConn,
//! tenant_id: Uuid,
//! ) -> Result<Vec<user::Model>, anyhow::Error> {
//! let scope = AccessScope::for_tenants(vec![tenant_id]);
//!
//! let users = user::Entity::find()
//! .secure()
//! .scope_with(&scope)?
//! .filter(user::Column::IsActive.eq(true)) // Additional filter
//! .order_by(user::Column::Email, Order::Asc)
//! .limit(100)
//! .all(db)
//! .await?;
//!
//! Ok(users)
//! }
//! ```
//!
//! ### Example 5: Working with global entities
//!
//! ```rust,ignore
//! // Global entities (no tenant column) work with resource IDs only
//! pub async fn get_system_config(
//! db: &modkit_db::secure::SecureConn,
//! config_id: Uuid,
//! ) -> Result<Option<system_config::Model>, anyhow::Error> {
//! let scope = AccessScope::for_resources(vec![config_id]);
//!
//! let config = system_config::Entity::find()
//! .secure()
//! .scope_with(&scope)?
//! .one(db)
//! .await?;
//!
//! Ok(config)
//! }
//! ```
//!
//! ### Example 6: Advanced composition (no raw escape hatch)
//!
//! If you need more advanced query composition, prefer extending the secure wrappers in `modkit-db`
//! (or using higher-level helpers like `OData` pagination). Module code should not unwrap raw `SeaORM`
//! builders.
//!
//! ## Integration with Repository Pattern
//!
//! A typical repository would look like:
//!
//! ```rust,ignore
//! use modkit_db::secure::{AccessScope, SecureConn, SecureEntityExt, ScopeError};
//! use uuid::Uuid;
//!
//! pub struct UserRepository {
//! conn: SecureConn,
//! }
//!
//! impl UserRepository {
//! pub async fn list_for_scope(
//! &self,
//! scope: &AccessScope,
//! ) -> Result<Vec<user::Model>, ScopeError> {
//! user::Entity::find()
//! .secure()
//! .scope_with(scope)?
//! .all(&self.conn)
//! .await
//! }
//!
//! pub async fn find_by_id(
//! &self,
//! tenant_id: Uuid,
//! user_id: Uuid,
//! ) -> Result<Option<user::Model>, ScopeError> {
//! let scope = AccessScope::single(ScopeConstraint::new(vec![
//! ScopeFilter::in_uuids(pep_properties::OWNER_TENANT_ID, vec![tenant_id]),
//! ScopeFilter::in_uuids(pep_properties::RESOURCE_ID, vec![user_id]),
//! ]));
//!
//! user::Entity::find()
//! .secure()
//! .scope_with(&scope)?
//! .one(&self.conn)
//! .await
//! }
//! }
//! ```
//!
//! ## Security Guarantees
//!
//! 1. **No unscoped execution**: Queries cannot be executed without calling `.scope_with()`
//! 2. **Explicit deny-all**: Empty scopes are denied rather than returning all data
//! 3. **Tenant isolation**: When `tenant_ids` are provided, they're always enforced
//! 4. **Type safety**: Typestates prevent misuse at compile time
//! 5. **No runtime overhead**: All checks happen at compile time or query build time
//!
//! ## Phase 2: Planned Enhancements
//!
//! Future versions will include:
//!
//! - `#[derive(Scopable)]` macro to auto-implement `ScopableEntity`
//! - Support for scoped UPDATE and DELETE operations
//! - Row-level security helpers for `PostgreSQL`
//! - Audit logging integration
//! - Policy composition (e.g., role-based filters)
//!
//! ## Error Handling
//!
//! The layer uses [`ScopeError`](crate::secure::ScopeError) for all errors:
//!
//! ```rust,ignore
//! match user::Entity::find().secure().scope_with(&scope) {
//! Ok(scoped) => {
//! // Execute query
//! }
//! Err(ScopeError::Db(msg)) => {
//! // Handle database error
//! }
//! }
//! ```
use crate;