runique 3.0.2

A Django-inspired web framework for Rust with ORM, templates, and comprehensive security middleware
Documentation
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
//! Admin actions reified as values (Command pattern).
//!
//! Each action carries its own authorization rule, so the access policy lives
//! in one place instead of being duplicated across the four entry points
//! (`admin_get`/`admin_post`/`admin_get_id`/`admin_post_id`). Parsing also
//! rejects actions that are invalid for the HTTP method, mirroring the
//! `not_found` arms of the previous string matches.

use super::ResourcePerms;

/// Outcome of authorizing an action against the resolved permissions.
#[derive(Debug, PartialEq, Eq)]
pub(super) enum Access {
    Granted,
    /// Insufficient rights to even see the resource → redirect to dashboard.
    DeniedDashboard,
    /// Resource visible but this operation is forbidden → redirect to its list.
    DeniedResource,
}

/// Actions on a collection URL: `/admin/{resource}/{action}`.
pub(super) enum CollectionAction {
    List,
    Create,
    Bulk,
}

/// Actions on a member URL: `/admin/{resource}/{id}/{action}`.
pub(super) enum MemberAction {
    Detail,
    Edit,
    Delete,
    ResetPassword,
}

impl CollectionAction {
    /// Valid GET actions on a collection (`None` → 404).
    pub(super) fn parse_get(action: &str) -> Option<Self> {
        match action {
            "list" => Some(Self::List),
            "create" => Some(Self::Create),
            "bulk" => Some(Self::Bulk),
            _ => None,
        }
    }

    /// Valid POST actions on a collection (`None` → 404). `list` is GET-only.
    pub(super) fn parse_post(action: &str) -> Option<Self> {
        match action {
            "create" => Some(Self::Create),
            "bulk" => Some(Self::Bulk),
            _ => None,
        }
    }

    pub(super) fn authorize_get(&self, perms: &ResourcePerms) -> Access {
        match self {
            Self::List => {
                if perms.can_read {
                    Access::Granted
                } else {
                    Access::DeniedDashboard
                }
            }
            Self::Create => {
                if !perms.can_read {
                    Access::DeniedDashboard
                } else if perms.can_create {
                    Access::Granted
                } else {
                    Access::DeniedResource
                }
            }
            Self::Bulk => {
                if perms.can_update {
                    Access::Granted
                } else {
                    Access::DeniedResource
                }
            }
        }
    }

    /// `bulk_action` is the `bulk_action` form field ("delete" selects the
    /// delete right, anything else the update right).
    ///
    /// The `can_create` requirement on the collection POST is the pre-existing
    /// behavior, kept verbatim so this refactor changes nothing.
    pub(super) fn authorize_post(&self, perms: &ResourcePerms, bulk_action: &str) -> Access {
        if !perms.can_create {
            return Access::DeniedResource;
        }
        match self {
            Self::Create => Access::Granted,
            Self::Bulk => {
                let can_bulk = if bulk_action == "delete" {
                    perms.can_delete
                } else {
                    perms.can_update
                };
                if can_bulk {
                    Access::Granted
                } else {
                    Access::DeniedResource
                }
            }
            // `list` is rejected at parse time; unreachable in practice.
            Self::List => Access::DeniedResource,
        }
    }
}

impl MemberAction {
    /// Valid GET actions on a member (`None` → 404).
    pub(super) fn parse_get(action: &str) -> Option<Self> {
        match action {
            "detail" => Some(Self::Detail),
            "edit" => Some(Self::Edit),
            "delete" => Some(Self::Delete),
            _ => None,
        }
    }

    /// Valid POST actions on a member (`None` → 404).
    pub(super) fn parse_post(action: &str) -> Option<Self> {
        match action {
            "edit" => Some(Self::Edit),
            "delete" => Some(Self::Delete),
            "reset-password" => Some(Self::ResetPassword),
            _ => None,
        }
    }

    /// Per-action rule for a member operation. The `can_read` dashboard gate is
    /// applied separately by the GET entry point (so `owns_record` keeps being
    /// resolved only after that gate, as before). `reset-password` is a
    /// sensitive write → same rule as edit.
    pub(super) fn authorize(&self, perms: &ResourcePerms, owns: bool) -> Access {
        match self {
            Self::Detail => Access::Granted,
            Self::Edit | Self::ResetPassword => grant_if(perms.can_edit(owns)),
            Self::Delete => grant_if(perms.can_remove(owns)),
        }
    }
}

fn grant_if(allowed: bool) -> Access {
    if allowed {
        Access::Granted
    } else {
        Access::DeniedResource
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::auth::permissions::{Groupe, Permission};
    use crate::auth::session::CurrentUser;

    /// Builds a `ResourcePerms` from the six flags in declaration order:
    /// create, read, update, delete, update_own, delete_own.
    fn perms(c: bool, r: bool, u: bool, d: bool, uo: bool, dorm: bool) -> ResourcePerms {
        ResourcePerms {
            can_create: c,
            can_read: r,
            can_update: u,
            can_delete: d,
            can_update_own: uo,
            can_delete_own: dorm,
        }
    }

    const NONE: ResourcePerms = ResourcePerms {
        can_create: false,
        can_read: false,
        can_update: false,
        can_delete: false,
        can_update_own: false,
        can_delete_own: false,
    };

    // ── Parsing rejects actions invalid for the method ───────────────

    #[test]
    fn collection_parse_rejects_cross_method() {
        assert!(CollectionAction::parse_get("list").is_some());
        assert!(CollectionAction::parse_post("list").is_none()); // list is GET-only
        assert!(CollectionAction::parse_get("nope").is_none());
        assert!(CollectionAction::parse_post("create").is_some());
    }

    #[test]
    fn member_parse_rejects_cross_method() {
        assert!(MemberAction::parse_get("detail").is_some());
        assert!(MemberAction::parse_post("detail").is_none()); // detail is GET-only
        assert!(MemberAction::parse_get("reset-password").is_none()); // POST-only
        assert!(MemberAction::parse_post("reset-password").is_some());
    }

    // ── Collection GET ───────────────────────────────────────────────

    #[test]
    fn list_requires_read() {
        assert_eq!(
            CollectionAction::List.authorize_get(&perms(false, true, false, false, false, false)),
            Access::Granted
        );
        assert_eq!(
            CollectionAction::List.authorize_get(&NONE),
            Access::DeniedDashboard
        );
    }

    #[test]
    fn create_get_is_two_stage() {
        // no read at all → dashboard
        assert_eq!(
            CollectionAction::Create.authorize_get(&NONE),
            Access::DeniedDashboard
        );
        // read but no create → resource list
        assert_eq!(
            CollectionAction::Create.authorize_get(&perms(false, true, false, false, false, false)),
            Access::DeniedResource
        );
        // read + create → granted
        assert_eq!(
            CollectionAction::Create.authorize_get(&perms(true, true, false, false, false, false)),
            Access::Granted
        );
    }

    #[test]
    fn bulk_get_requires_update_no_dashboard() {
        assert_eq!(
            CollectionAction::Bulk.authorize_get(&perms(false, false, true, false, false, false)),
            Access::Granted
        );
        // no update → resource (never dashboard)
        assert_eq!(
            CollectionAction::Bulk.authorize_get(&NONE),
            Access::DeniedResource
        );
    }

    // ── Collection POST (preserves the can_create upfront gate) ──────

    #[test]
    fn create_post_requires_create() {
        assert_eq!(
            CollectionAction::Create
                .authorize_post(&perms(true, false, false, false, false, false), ""),
            Access::Granted
        );
        assert_eq!(
            CollectionAction::Create.authorize_post(&NONE, ""),
            Access::DeniedResource
        );
    }

    #[test]
    fn bulk_post_needs_create_plus_operation_right() {
        // create + update, non-delete action → granted
        assert_eq!(
            CollectionAction::Bulk
                .authorize_post(&perms(true, false, true, false, false, false), "activate"),
            Access::Granted
        );
        // create + delete-right, delete action → granted
        assert_eq!(
            CollectionAction::Bulk
                .authorize_post(&perms(true, false, false, true, false, false), "delete"),
            Access::Granted
        );
        // has update but NOT create → denied (preserved upfront gate)
        assert_eq!(
            CollectionAction::Bulk
                .authorize_post(&perms(false, false, true, false, false, false), "activate"),
            Access::DeniedResource
        );
        // create but delete action without delete right → denied
        assert_eq!(
            CollectionAction::Bulk
                .authorize_post(&perms(true, false, true, false, false, false), "delete"),
            Access::DeniedResource
        );
    }

    // ── Member actions ───────────────────────────────────────────────

    #[test]
    fn detail_always_granted_after_gate() {
        assert_eq!(
            MemberAction::Detail.authorize(&NONE, false),
            Access::Granted
        );
    }

    #[test]
    fn edit_global_vs_own() {
        // global update → granted regardless of ownership
        assert_eq!(
            MemberAction::Edit.authorize(&perms(false, false, true, false, false, false), false),
            Access::Granted
        );
        // only update_own + owns the record → granted
        assert_eq!(
            MemberAction::Edit.authorize(&perms(false, false, false, false, true, false), true),
            Access::Granted
        );
        // update_own but does NOT own → denied
        assert_eq!(
            MemberAction::Edit.authorize(&perms(false, false, false, false, true, false), false),
            Access::DeniedResource
        );
        // nothing → denied
        assert_eq!(
            MemberAction::Edit.authorize(&NONE, true),
            Access::DeniedResource
        );
    }

    #[test]
    fn delete_global_vs_own() {
        assert_eq!(
            MemberAction::Delete.authorize(&perms(false, false, false, true, false, false), false),
            Access::Granted
        );
        assert_eq!(
            MemberAction::Delete.authorize(&perms(false, false, false, false, false, true), true),
            Access::Granted
        );
        assert_eq!(
            MemberAction::Delete.authorize(&perms(false, false, false, false, false, true), false),
            Access::DeniedResource
        );
    }

    #[test]
    fn reset_password_follows_edit_rule() {
        // same rule as edit: global update grants it
        assert_eq!(
            MemberAction::ResetPassword
                .authorize(&perms(false, false, true, false, false, false), false),
            Access::Granted
        );
        // update_own + owns
        assert_eq!(
            MemberAction::ResetPassword
                .authorize(&perms(false, false, false, false, true, false), true),
            Access::Granted
        );
        // no update right → denied (delete right must NOT grant a reset)
        assert_eq!(
            MemberAction::ResetPassword
                .authorize(&perms(false, false, false, true, false, false), true),
            Access::DeniedResource
        );
    }

    // ── ResourcePerms::resolve ───────────────────────────────────────

    fn user(is_superuser: bool, groupes: Vec<Groupe>) -> CurrentUser {
        CurrentUser {
            id: crate::utils::pk::Pk::default(),
            username: "tester".to_string(),
            is_staff: true,
            is_superuser,
            groupes,
        }
    }

    #[test]
    fn resolve_superuser_grants_everything() {
        let p = ResourcePerms::resolve(&user(true, vec![]), "anything");
        assert!(
            p.can_create
                && p.can_read
                && p.can_update
                && p.can_delete
                && p.can_update_own
                && p.can_delete_own
        );
    }

    #[test]
    fn resolve_reads_matching_permission() {
        let mut perm = Permission::zeroed("blog".to_string());
        perm.can_read = true;
        perm.can_update = true;
        let groupe = Groupe {
            id: 1,
            nom: "editors".to_string(),
            permissions: vec![perm],
        };
        let p = ResourcePerms::resolve(&user(false, vec![groupe]), "blog");
        assert!(p.can_read && p.can_update);
        assert!(!p.can_create && !p.can_delete);
    }

    #[test]
    fn resolve_unknown_resource_is_all_false() {
        let p = ResourcePerms::resolve(&user(false, vec![]), "ghost");
        assert_eq!(
            (
                p.can_create,
                p.can_read,
                p.can_update,
                p.can_delete,
                p.can_update_own,
                p.can_delete_own
            ),
            (false, false, false, false, false, false)
        );
    }
}