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
use EntityTrait;
/// Defines the contract for entities that can be scoped by tenant, resource, owner, and type.
///
/// Each entity implementing this trait must explicitly declare all four scope dimensions:
/// - `tenant_col()`: Column for tenant-based isolation (multi-tenancy)
/// - `resource_col()`: Column for resource-level access (typically the primary key)
/// - `owner_col()`: Column for owner-based filtering
/// - `type_col()`: Column for type-based filtering
///
/// **Important**: No implicit defaults are allowed. Every scope dimension must be explicitly
/// specified as `Some(Column::...)` or `None` to enforce compile-time safety in secure systems.
///
/// # Example (Manual Implementation)
/// ```rust,ignore
/// impl ScopableEntity for user::Entity {
/// fn tenant_col() -> Option<Self::Column> {
/// Some(user::Column::TenantId)
/// }
/// fn resource_col() -> Option<Self::Column> {
/// Some(user::Column::Id)
/// }
/// fn owner_col() -> Option<Self::Column> {
/// None
/// }
/// fn type_col() -> Option<Self::Column> {
/// None
/// }
/// fn resolve_property(property: &str) -> Option<Self::Column> {
/// match property {
/// "owner_tenant_id" => Self::tenant_col(),
/// "id" => Self::resource_col(),
/// "owner_id" => Self::owner_col(),
/// _ => None,
/// }
/// }
/// }
/// ```
///
/// # Example (Using Derive Macro)
/// ```rust,ignore
/// use modkit_db::secure::Scopable;
///
/// #[derive(Clone, Debug, PartialEq, DeriveEntityModel, Scopable)]
/// #[sea_orm(table_name = "users")]
/// #[secure(
/// tenant_col = "tenant_id",
/// resource_col = "id",
/// no_owner,
/// no_type
/// )]
/// pub struct Model {
/// #[sea_orm(primary_key)]
/// pub id: Uuid,
/// pub tenant_id: Uuid,
/// pub email: String,
/// }
/// // Macro auto-generates resolve_property:
/// // "owner_tenant_id" => Some(Column::TenantId) (from tenant_col)
/// // "id" => Some(Column::Id) (from resource_col)
/// // _ => None
/// ```
///
/// # Custom PEP Properties
/// ```rust,ignore
/// #[derive(Clone, Debug, PartialEq, DeriveEntityModel, Scopable)]
/// #[sea_orm(table_name = "resources")]
/// #[secure(
/// tenant_col = "tenant_id",
/// resource_col = "id",
/// no_owner,
/// no_type,
/// pep_prop(department_id = "department_id"),
/// )]
/// pub struct Model {
/// #[sea_orm(primary_key)]
/// pub id: Uuid,
/// pub tenant_id: Uuid,
/// pub department_id: Uuid,
/// }
/// // Macro auto-generates resolve_property:
/// // "owner_tenant_id" => Some(Column::TenantId) (from tenant_col)
/// // "id" => Some(Column::Id) (from resource_col)
/// // "department_id" => Some(Column::DepartmentId) (from pep_prop)
/// // _ => None
/// ```
///
/// # Unrestricted Entities
/// ```rust,ignore
/// #[derive(Clone, Debug, PartialEq, DeriveEntityModel, Scopable)]
/// #[sea_orm(table_name = "system_config")]
/// #[secure(unrestricted)]
/// pub struct Model {
/// #[sea_orm(primary_key)]
/// pub id: Uuid,
/// pub config_key: String,
/// }
/// ```