summaryrefslogtreecommitdiffstats
path: root/src/db/audit.rs
blob: 23b20bbdd410c7d87be02d6e73eab3796577e80d (plain)
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
use rusqlite::params;

use crate::db::{Db, Writer};
use crate::error::Result;
use crate::time;

// ------------------------------------------------------------------- audit --

pub fn audit(
    writer: &Writer,
    actor_kind: &str,
    actor: &str,
    action: &str,
    entity: &str,
    entity_id: &str,
    detail: serde_json::Value,
) -> Result<()> {
    let at = time::now();
    let actor_kind = actor_kind.to_string();
    let actor = actor.to_string();
    let action = action.to_string();
    let entity = entity.to_string();
    let entity_id = entity_id.to_string();
    writer.tx(move |tx| {
        audit_in(
            tx,
            &AuditEntry {
                at: &at,
                actor_kind: &actor_kind,
                actor: &actor,
                action: &action,
                entity: &entity,
                entity_id: &entity_id,
                detail,
            },
        )
    })
}

#[derive(Debug)]
pub struct AuditEntry<'a> {
    pub at: &'a str,
    pub actor_kind: &'a str,
    pub actor: &'a str,
    pub action: &'a str,
    pub entity: &'a str,
    pub entity_id: &'a str,
    pub detail: serde_json::Value,
}

impl<'a> AuditEntry<'a> {
    pub fn human(
        at: &'a str,
        actor: &'a str,
        action: &'a str,
        entity: &'a str,
        entity_id: &'a str,
        detail: serde_json::Value,
    ) -> Self {
        Self { at, actor_kind: "human", actor, action, entity, entity_id, detail }
    }
}

pub fn audit_in(tx: &rusqlite::Transaction<'_>, e: &AuditEntry<'_>) -> Result<()> {
    tx.execute(
        "INSERT INTO audit (at, actor_kind, actor, action, entity, entity_id, detail_json)
         VALUES (?1,?2,?3,?4,?5,?6,?7)",
        params![e.at, e.actor_kind, e.actor, e.action, e.entity, e.entity_id, e.detail.to_string()],
    )?;
    Ok(())
}

pub fn audit_system(
    writer: &Writer,
    action: &str,
    entity: &str,
    entity_id: &str,
    detail: serde_json::Value,
) -> Result<()> {
    audit(writer, "system", "", action, entity, entity_id, detail)
}

// ------------------------------------------------------------------ audit --

#[derive(Debug, Clone)]
pub struct AuditRow {
    pub id: i64,
    pub at: String,
    pub actor_kind: String,
    pub actor: String,
    pub action: String,
    pub entity: String,
    pub entity_id: String,
    pub detail_json: String,
}

pub fn list_audit(db: &Db, limit: i64) -> Result<Vec<AuditRow>> {
    db.read(|c| {
        let mut stmt = c.prepare(
            "SELECT id, at, actor_kind, actor, action, entity, entity_id, detail_json
             FROM audit ORDER BY id DESC LIMIT ?1",
        )?;
        let rows = stmt.query_map(params![limit], |r| {
            Ok(AuditRow {
                id: r.get(0)?,
                at: r.get(1)?,
                actor_kind: r.get(2)?,
                actor: r.get(3)?,
                action: r.get(4)?,
                entity: r.get(5)?,
                entity_id: r.get(6)?,
                detail_json: r.get(7)?,
            })
        })?;
        let mut out = Vec::new();
        for r in rows {
            out.push(r?);
        }
        Ok(out)
    })
}