Skip to main content

tablo_audit/
impersonation.rs

1//! [`ImpersonationEvent`]: what an [`AuditEntry`] records when someone starts or stops acting as a
2//! user, read back typed.
3
4use jiff::Timestamp;
5use toasty::stmt::Expr;
6
7use crate::AuditEntry;
8
9/// The event of an entry recording the start of an impersonation.
10pub(crate) const IMPERSONATE: &str = "impersonate";
11/// The event of an entry recording its stop.
12pub(crate) const STOP_IMPERSONATING: &str = "stop_impersonating";
13/// The change naming the account an auth event was done to.
14pub(crate) const TARGET_FIELD: &str = "user";
15/// The change naming when an impersonation ends at the latest.
16pub(crate) const UNTIL_FIELD: &str = "until";
17
18/// A start or a stop of acting as a user, as [`AuditEntry::impersonation`] reads it. The entry's
19/// [`actor`](AuditEntry::actor) is who acted; this names whom they acted as.
20#[derive(Debug, Clone, PartialEq, Eq)]
21#[non_exhaustive]
22pub enum ImpersonationEvent {
23    /// The actor started acting as the user.
24    Started {
25        /// The user's [`PanelUser::user_id`](tablo_core::PanelUser::user_id).
26        user_id: String,
27        /// The user's display name then; `None` when the account was not loaded.
28        user_name: Option<String>,
29        /// When the session ends at the latest: a start no stop follows ended then.
30        until: Option<Timestamp>,
31    },
32    /// The actor stopped acting as the user.
33    Stopped {
34        /// The user's [`PanelUser::user_id`](tablo_core::PanelUser::user_id).
35        user_id: String,
36        /// The user's display name then; `None` when the account was not loaded, as when the
37        /// session ran out.
38        user_name: Option<String>,
39    },
40}
41
42impl AuditEntry {
43    /// The impersonation this entry records; `None` for any other entry.
44    ///
45    /// ```rust
46    /// # use tablo_audit::{AuditEntry, ImpersonationEvent};
47    /// fn whom(entry: &AuditEntry) -> Option<String> {
48    ///     match entry.impersonation()? {
49    ///         ImpersonationEvent::Started { user_name, .. }
50    ///         | ImpersonationEvent::Stopped { user_name, .. } => user_name,
51    ///         _ => None,
52    ///     }
53    /// }
54    /// ```
55    pub fn impersonation(&self) -> Option<ImpersonationEvent> {
56        let started = match self.event.as_str() {
57            IMPERSONATE => true,
58            STOP_IMPERSONATING => false,
59            _ => return None,
60        };
61        let user_id = self.record.clone()?;
62        let after = |field: &str| {
63            self.changes
64                .0
65                .iter()
66                .find(|change| change.field == field)
67                .and_then(|change| change.after.clone())
68        };
69        // The trail writes the id where it has no name.
70        let user_name = after(TARGET_FIELD).filter(|name| *name != user_id);
71        Some(if started {
72            ImpersonationEvent::Started {
73                user_id,
74                user_name,
75                until: after(UNTIL_FIELD).and_then(|until| until.parse().ok()),
76            }
77        } else {
78            ImpersonationEvent::Stopped { user_id, user_name }
79        })
80    }
81}
82
83/// The entries that record an impersonation, started or stopped, for a resource's
84/// [`query`](tablo_core::ResourceDef::query) or a filter:
85///
86/// ```rust
87/// # use tablo_audit::AuditEntry;
88/// let starts_and_stops = AuditEntry::filter(tablo_audit::impersonations());
89/// # let _ = starts_and_stops;
90/// ```
91pub fn impersonations() -> Expr<bool> {
92    AuditEntry::fields()
93        .event()
94        .in_list([IMPERSONATE.to_owned(), STOP_IMPERSONATING.to_owned()])
95}