Skip to main content

tablo_audit/
log.rs

1//! [`AuditLog`]: the read-only resource that lists the [`AuditEntry`] rows of its panel.
2
3use tablo_core::{
4    NoForm, ReadOnly, Resource, ResourceDef, Tenancy, Text, WriteError, mounted_resources,
5};
6use toasty::Executor;
7use topcoat::context::Cx;
8
9use crate::AuditEntry;
10
11mod columns;
12
13/// The read-only resource listing the [`AuditEntry`] rows of its panel, newest first, at
14/// `{prefix}/audit-log`, or under the area that places it.
15/// [`audit_log`](crate::PanelAuditExt::audit_log) registers it.
16///
17/// It lists what happened on its own panel only, for the tenant the reader acts for, and to the
18/// resources the reader may list: an entry about a resource whose list the reader may not open
19/// stays out of it, and a field [`field_access`](tablo_core::ResourceDef::field_access) hides
20/// from the reader reads "Hidden". Whatever policy an app gives it, it allows reads only.
21pub struct AuditLog;
22
23impl Resource for AuditLog {
24    type Model = AuditEntry;
25    type Form = NoForm<AuditEntry>;
26
27    fn declare() -> ResourceDef<Self> {
28        ResourceDef::new()
29            .slug("audit-log")
30            .label(Text::message("tablo-audit-entry"))
31            .plural_label(Text::message("tablo-audit-log"))
32            // The log keeps what happened: no policy the app sets writes through its pages.
33            .restrict(ReadOnly)
34            // Placed without `audit_trail`, it would list nothing, ever: the mount refuses it.
35            .requires_commit_hook::<crate::trail::Trail>()
36            // Every entry carries its tenant; `scope` keeps the reader's own.
37            .tenancy(Tenancy::none())
38            .table(columns::table())
39            .detail(columns::detail())
40            .query_fn(scope)
41    }
42
43    // Past `ReadOnly`, which every policy the def holds passes through, a delete refuses too.
44    async fn delete_record(
45        _cx: &Cx,
46        _record: &AuditEntry,
47        _ex: &mut dyn Executor,
48    ) -> Result<(), WriteError> {
49        Err(immutable())
50    }
51
52    async fn restore_record(
53        _cx: &Cx,
54        _record: &AuditEntry,
55        _ex: &mut dyn Executor,
56    ) -> Result<(), WriteError> {
57        Err(immutable())
58    }
59
60    async fn force_delete_record(
61        _cx: &Cx,
62        _record: &AuditEntry,
63        _ex: &mut dyn Executor,
64    ) -> Result<(), WriteError> {
65        Err(immutable())
66    }
67}
68
69/// The refusal of every write through the log's pages.
70fn immutable() -> WriteError {
71    WriteError::declined(Text::message("tablo-audit-immutable"))
72}
73
74/// The entries `cx` may read: its panel's, for the tenant it acts for, about the resources it
75/// may list or about none.
76fn scope(cx: &Cx) -> toasty::stmt::Expr<bool> {
77    let Some(panel) = tablo_core::url::panel(cx) else {
78        return toasty::stmt::IntoExpr::into_expr(false);
79    };
80    let fields = AuditEntry::fields();
81    let tenant = match tablo_core::tenant_id(cx) {
82        Some(tenant) => fields.tenant().eq(Some(tenant)),
83        None => fields.tenant().is_none(),
84    };
85    let listable = listable_slugs(cx);
86    let resource = if listable.is_empty() {
87        fields.resource().is_none()
88    } else {
89        fields
90            .resource()
91            .is_none()
92            .or(fields.resource().in_list(listable))
93    };
94    fields.panel().eq(panel).and(tenant).and(resource)
95}
96
97/// The slugs of the panel's resources `cx` may list. A slug two resources share, in two areas,
98/// counts only when `cx` may list both: an entry names the slug alone.
99fn listable_slugs(cx: &Cx) -> Vec<Option<String>> {
100    let mounted = mounted_resources(cx);
101    let mut slugs: Vec<Option<String>> = Vec::new();
102    for resource in &mounted {
103        let slug = Some(resource.slug().to_owned());
104        let shared = mounted
105            .iter()
106            .filter(|other| other.slug() == resource.slug());
107        if !slugs.contains(&slug) && shared.clone().all(|other| other.can_list(cx)) {
108            slugs.push(slug);
109        }
110    }
111    slugs
112}