rustango-cms 0.4.0

A page-tree content management system for Rust, built on rustango
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
//! Per-page permission engine.
//!
//! Three pieces:
//!
//! 1. [`PagePermission`] — `(page_id, role_id, permission)` grants
//!    stored in `cms_page_permission`. Sites grant fine-grained
//!    actions per role per subtree.
//! 2. [`user_can`] — `(user_id, page_id, action) → bool`. Walks the
//!    page's ancestor chain collecting grants; superusers short-circuit
//!    to `true`.
//! 3. [`Action`] — the canonical set of action strings the engine
//!    recognizes. Stored as text so the framework's permission system
//!    can interoperate via codenames if/when we expose them.
//!
//! Permissions **inherit** down the tree: a grant on a page applies
//! to every descendant unless overridden by a closer grant. There are
//! no explicit denies — any matching grant in the chain returns
//! `true`.

use chrono::{DateTime, Utc};
use rustango::sql::Auto;
use rustango::Model;
use serde::{Deserialize, Serialize};

/// Canonical action set the engine recognizes. Stored as text so
/// authors can extend the surface without a schema change.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Action {
    View,
    Add,
    Edit,
    Publish,
    Delete,
    /// Top-level "may use this admin surface at all" verb — used by
    /// the `cms_admin.access` codename + `with_cms_admin_access`
    /// middleware to gate the protected router. Unlike the CRUD
    /// verbs it doesn't pair with a resource model; it pairs with a
    /// router surface.
    Access,
    /// Framework-admin variant of [`Action::Access`]. Maps to the
    /// `auth.access_admin` codename the framework's
    /// `permission_required` middleware enforces on
    /// `/__admin/` + `/admin/`. Surfaced as a separate variant so the
    /// matrix can present "framework admin access" as a one-cell row
    /// without inventing a new codename layout.
    AccessAdmin,
}

impl Action {
    #[must_use]
    pub fn as_str(self) -> &'static str {
        match self {
            Self::View => "view",
            Self::Add => "add",
            Self::Edit => "edit",
            Self::Publish => "publish",
            Self::Delete => "delete",
            Self::Access => "access",
            Self::AccessAdmin => "access_admin",
        }
    }

    /// The canonical set; iteration order is also the order the
    /// admin permission editor renders columns in. `Access` is
    /// deliberately omitted — its column lives in a separate Access
    /// section row, not the CRUD grid.
    #[must_use]
    pub fn all() -> &'static [Action] {
        &[
            Self::View,
            Self::Add,
            Self::Edit,
            Self::Publish,
            Self::Delete,
        ]
    }
}

/// One `(page_id, role_id, permission)` grant. UNIQUE at the SQL
/// layer so a role can't be granted the same action twice on the
/// same page.
#[derive(Model, Debug, Clone, Serialize, Deserialize)]
#[rustango(
    table = "cms_page_permission",
    app = "cms",
    admin(
        list_display = "page_id, role_id, permission",
        ordering = "page_id, role_id, permission",
    )
)]
pub struct PagePermission {
    #[rustango(primary_key)]
    pub id: Auto<i64>,

    #[rustango(fk = "cms_page", on = "id", index)]
    pub page_id: i64,

    #[rustango(fk = "rustango_roles", on = "id", index)]
    pub role_id: i64,

    /// One of the strings in [`Action::as_str`]. Stored verbatim
    /// so adding actions later doesn't need a schema migration.
    #[rustango(max_length = 32)]
    pub permission: String,

    #[rustango(auto_now_add)]
    pub created_at: Auto<DateTime<Utc>>,
}

/// Resolve `(user_id, page_id, action) → bool` against the per-page
/// permission table. Walks ancestors via the `cms_page.path`
/// materialized prefix and collects matching grants in one query.
///
/// Short-circuits to `true` for tenant superusers. Returns `false`
/// when the user has no roles assigned (there is no per-user override
/// table).
///
/// # Errors
/// Propagates driver / query failures from the four underlying
/// fetches (user, page, user-roles, permission grants).
pub async fn user_can(
    pool: &rustango::sql::Pool,
    user_id: i64,
    page_id: i64,
    action: Action,
) -> Result<bool, rustango::sql::ExecError> {
    use rustango::core::Column as _;
    use rustango::sql::FetcherPool as _;

    // 1) Superuser short-circuit.
    let user = rustango::tenancy::auth::User::objects()
        .where_(rustango::tenancy::auth::User::id.eq(user_id))
        .first(pool)
        .await?;
    let Some(user) = user else {
        return Ok(false);
    };
    if !user.active {
        return Ok(false);
    }
    if user.is_superuser {
        return Ok(true);
    }

    // 2) Resolve the user's role ids.
    let memberships: Vec<rustango::tenancy::permissions::UserRole> =
        rustango::tenancy::permissions::UserRole::objects()
            .where_(rustango::tenancy::permissions::UserRole::user_id.eq(user_id))
            .fetch(pool)
            .await?;
    if memberships.is_empty() {
        return Ok(false);
    }
    let role_ids: Vec<i64> = memberships.iter().map(|m| m.role_id).collect();

    // 3) Walk the target page's ancestor chain. `cms_page.path` is a
    //    materialized prefix in path-encoded form (`{depth}-{id}/...`
    //    when the page is at depth N). We use a simpler approach:
    //    fetch the page, then re-fetch every ancestor by id via the
    //    parent_id chain — bounded by tree depth, fine for the admin.
    let target = crate::page::Page::objects()
        .where_(crate::page::Page::id.eq(page_id))
        .first(pool)
        .await?;
    let Some(mut current) = target else {
        return Ok(false);
    };
    let mut chain_ids: Vec<i64> = Vec::new();
    chain_ids.push(current.id.get().copied().unwrap_or_default());
    while let Some(pid) = current.parent_id {
        let parent = crate::page::Page::objects()
            .where_(crate::page::Page::id.eq(pid))
            .first(pool)
            .await?;
        match parent {
            Some(p) => {
                chain_ids.push(pid);
                current = p;
            }
            None => break,
        }
    }

    // 4) Any matching grant in the chain returns true.
    let grants: Vec<PagePermission> = PagePermission::objects()
        .where_(PagePermission::page_id.is_in(chain_ids.iter().copied()))
        .where_(PagePermission::role_id.is_in(role_ids.iter().copied()))
        .where_(PagePermission::permission.eq(action.as_str().to_owned()))
        .fetch(pool)
        .await?;
    Ok(!grants.is_empty())
}

/// One `(collection_id, role_id, permission)` grant — the media
/// collection analogue of [`PagePermission`]. Same UNIQUE
/// shape and same inheritance semantics (parent collection grants
/// flow to children).
#[derive(Model, Debug, Clone, Serialize, Deserialize)]
#[rustango(
    table = "cms_collection_permission",
    app = "cms",
    admin(
        list_display = "collection_id, role_id, permission",
        ordering = "collection_id, role_id, permission",
    )
)]
pub struct CollectionPermission {
    #[rustango(primary_key)]
    pub id: Auto<i64>,

    #[rustango(fk = "cms_media_collection", on = "id", index)]
    pub collection_id: i64,

    #[rustango(fk = "rustango_roles", on = "id", index)]
    pub role_id: i64,

    #[rustango(max_length = 32)]
    pub permission: String,

    #[rustango(auto_now_add)]
    pub created_at: Auto<DateTime<Utc>>,
}

/// Resolve `(user_id, collection_id, action) → bool` against the
/// per-collection permission table. Walks the collection's
/// ancestor chain (via `parent_id`) and short-circuits to `true`
/// for superusers.
///
/// # Errors
/// Driver / query failures from the underlying fetches.
pub async fn user_can_in_collection(
    pool: &rustango::sql::Pool,
    user_id: i64,
    collection_id: i64,
    action: Action,
) -> Result<bool, rustango::sql::ExecError> {
    use rustango::core::Column as _;
    use rustango::sql::FetcherPool as _;

    let user = rustango::tenancy::auth::User::objects()
        .where_(rustango::tenancy::auth::User::id.eq(user_id))
        .first(pool)
        .await?;
    let Some(user) = user else {
        return Ok(false);
    };
    if !user.active {
        return Ok(false);
    }
    if user.is_superuser {
        return Ok(true);
    }

    let memberships: Vec<rustango::tenancy::permissions::UserRole> =
        rustango::tenancy::permissions::UserRole::objects()
            .where_(rustango::tenancy::permissions::UserRole::user_id.eq(user_id))
            .fetch(pool)
            .await?;
    if memberships.is_empty() {
        return Ok(false);
    }
    let role_ids: Vec<i64> = memberships.iter().map(|m| m.role_id).collect();

    // Walk the collection's parent chain.
    let target = crate::media::MediaCollection::objects()
        .where_(crate::media::MediaCollection::id.eq(collection_id))
        .first(pool)
        .await?;
    let Some(mut current) = target else {
        return Ok(false);
    };
    let mut chain_ids: Vec<i64> = Vec::new();
    chain_ids.push(current.id.get().copied().unwrap_or_default());
    while let Some(pid) = current.parent_id {
        let parent = crate::media::MediaCollection::objects()
            .where_(crate::media::MediaCollection::id.eq(pid))
            .first(pool)
            .await?;
        match parent {
            Some(p) => {
                chain_ids.push(pid);
                current = p;
            }
            None => break,
        }
    }

    let grants: Vec<CollectionPermission> = CollectionPermission::objects()
        .where_(CollectionPermission::collection_id.is_in(chain_ids.iter().copied()))
        .where_(CollectionPermission::role_id.is_in(role_ids.iter().copied()))
        .where_(CollectionPermission::permission.eq(action.as_str().to_owned()))
        .fetch(pool)
        .await?;
    Ok(!grants.is_empty())
}

/// Which of `pages` — `(id, parent_id)` pairs — `user_id` may view: the
/// set [`user_can`] with [`Action::View`] would allow, in a bounded number
/// of queries instead of one walk per page. For list views, so a listing
/// never shows a row its detail view refuses.
///
/// Superusers see every page; inactive, unknown and role-less users see
/// none.
///
/// # Errors
/// Driver / query failures.
pub async fn viewable_page_ids(
    pool: &rustango::sql::Pool,
    user_id: i64,
    pages: &[(i64, Option<i64>)],
) -> Result<std::collections::HashSet<i64>, rustango::sql::ExecError> {
    use rustango::core::Column as _;
    use rustango::sql::FetcherPool as _;
    use std::collections::{HashMap, HashSet};

    let user = rustango::tenancy::auth::User::objects()
        .where_(rustango::tenancy::auth::User::id.eq(user_id))
        .first(pool)
        .await?;
    match user {
        Some(u) if u.active && u.is_superuser => return Ok(pages.iter().map(|p| p.0).collect()),
        Some(u) if u.active => {}
        _ => return Ok(HashSet::new()),
    }
    let role_ids: Vec<i64> = rustango::tenancy::permissions::UserRole::objects()
        .where_(rustango::tenancy::permissions::UserRole::user_id.eq(user_id))
        .fetch(pool)
        .await?
        .into_iter()
        .map(|m| m.role_id)
        .collect();
    if role_ids.is_empty() {
        return Ok(HashSet::new());
    }
    let granted: HashSet<i64> = PagePermission::objects()
        .where_(PagePermission::role_id.is_in(role_ids))
        .where_(PagePermission::permission.eq(Action::View.as_str().to_owned()))
        .fetch(pool)
        .await?
        .into_iter()
        .map(|g| g.page_id)
        .collect();
    if granted.is_empty() {
        return Ok(HashSet::new());
    }

    // Every ancestor's parent, fetched a tree level at a time.
    let mut parent_of: HashMap<i64, Option<i64>> = pages.iter().copied().collect();
    loop {
        let missing: HashSet<i64> = parent_of
            .values()
            .flatten()
            .copied()
            .filter(|id| !parent_of.contains_key(id))
            .collect();
        if missing.is_empty() {
            break;
        }
        let found: Vec<crate::page::Page> = crate::page::Page::objects()
            .where_(crate::page::Page::id.is_in(missing.iter().copied()))
            .fetch(pool)
            .await?;
        for id in missing {
            // A dangling parent ends the chain, as it does in `user_can`.
            let parent = found
                .iter()
                .find(|p| p.id.get().copied() == Some(id))
                .and_then(|p| p.parent_id);
            parent_of.insert(id, parent);
        }
    }

    let viewable = |mut id: i64| {
        // Bounded by the map, so a parent cycle cannot spin forever.
        for _ in 0..=parent_of.len() {
            if granted.contains(&id) {
                return true;
            }
            match parent_of.get(&id).copied().flatten() {
                Some(parent) => id = parent,
                None => return false,
            }
        }
        false
    };
    Ok(pages.iter().map(|p| p.0).filter(|&id| viewable(id)).collect())
}

/// Resolve the full action matrix for a user on a page in one shot.
/// Useful for the admin form-state computation where every action
/// is checked together (so the page editor can disable Publish but
/// allow Edit, etc.). Returns a fresh HashMap with one entry per
/// action — `true` when granted, `false` when not.
///
/// # Errors
/// Propagates driver errors as `user_can` would.
pub async fn user_can_matrix(
    pool: &rustango::sql::Pool,
    user_id: i64,
    page_id: i64,
) -> Result<std::collections::HashMap<&'static str, bool>, rustango::sql::ExecError> {
    let mut out = std::collections::HashMap::new();
    for action in Action::all() {
        let allowed = user_can(pool, user_id, page_id, *action).await?;
        out.insert(action.as_str(), allowed);
    }
    Ok(out)
}

/// Resolve the full set of role codenames a user inherits via their
/// `UserRole` memberships. Used by the admin chrome to gate sidebar
/// items + by handlers to enforce CRUD access against the
/// permissions matrix.
///
/// Superusers and inactive users are caller's concern — this helper
/// just returns the codename union for whatever role memberships
/// exist. Empty set when the user has no roles assigned.
///
/// # Errors
/// Propagates driver / query failures from the three underlying
/// fetches (memberships, role permissions).
pub async fn user_codenames(
    pool: &rustango::sql::Pool,
    user_id: i64,
) -> Result<std::collections::HashSet<String>, rustango::sql::ExecError> {
    use rustango::core::Column as _;
    use rustango::sql::FetcherPool as _;

    let memberships: Vec<rustango::tenancy::permissions::UserRole> =
        rustango::tenancy::permissions::UserRole::objects()
            .where_(rustango::tenancy::permissions::UserRole::user_id.eq(user_id))
            .fetch(pool)
            .await?;
    if memberships.is_empty() {
        return Ok(std::collections::HashSet::new());
    }
    let role_ids: Vec<i64> = memberships.iter().map(|m| m.role_id).collect();
    let grants: Vec<rustango::tenancy::permissions::RolePermission> =
        rustango::tenancy::permissions::RolePermission::objects()
            .where_(
                rustango::tenancy::permissions::RolePermission::role_id
                    .is_in(role_ids.iter().copied()),
            )
            .fetch(pool)
            .await?;
    Ok(grants.into_iter().map(|g| g.codename).collect())
}