Line data Source code
1 4 : (ns matchscore.utils.audit
2 : "Fills the `created_by` / `updated_by` audit columns.
3 :
4 : The acting user is carried in the `*actor-id*` dynamic var instead of being
5 : threaded through every command and repository signature. Entry points that
6 : know who is acting (API routes, realtime events, Telegram bot actions) bind it
7 : with `with-actor`; repositories call `stamp` on their write queries.
8 :
9 : No actor (background jobs, migrations, the bot acting on its own) is a valid
10 : state: the columns are then written as NULL, which reads as \"the system\"."
11 : (:require
12 : [matchscore.utils.uuid :as uuid-utils]))
13 :
14 :
15 8 : (def ^:dynamic *actor-id*
16 : "UUID string of the user the current thread acts for, or nil for the system."
17 : nil)
18 :
19 :
20 116 : (defmacro with-actor
21 : "Runs body with `user-id` as the acting user. A nil or malformed id means the
22 : system acts."
23 : [user-id & body]
24 8 : `(binding [*actor-id* ~user-id] ~@body))
25 :
26 :
27 4 : (defn actor-id
28 : "UUID string of the acting user, or nil. Anything that is not a UUID is dropped
29 : so that a bad id can never fail the write it would be attached to."
30 : []
31 40 : (let [id (some->
32 4 : *actor-id*
33 4 : str)]
34 24 : (when (uuid-utils/uuid-string? id) id)))
35 :
36 :
37 4 : (defn actor
38 : "HoneySQL value of the acting user for a UUID column, or nil."
39 : []
40 48 : (when-let [id (actor-id)] [:cast id :uuid]))
41 :
42 :
43 4 : (defn- has-column?
44 : [row column]
45 76 : (or (contains? row column) (contains? row (keyword (.replace (name column) "_" "-")))))
46 :
47 :
48 4 : (defn- stamp-row
49 : "An explicit `created_by` on the row always wins: on events and templates it is
50 : the owner the access rules rely on, not just an audit trail."
51 : [row actor]
52 16 : (if (map? row)
53 44 : (cond-> (assoc row :updated_by actor)
54 32 : (not (has-column? row :created_by)) (assoc :created_by actor))
55 1 : row))
56 :
57 :
58 4 : (defn- stamp-conflict-update
59 : [do-update-set actor]
60 56 : (cond (and (map? do-update-set) (sequential? (:fields do-update-set)))
61 60 : (update do-update-set :fields #(vec (distinct (conj (vec %) :updated_by))))
62 :
63 44 : (and (map? do-update-set) (map? (:fields do-update-set)))
64 0 : (update do-update-set :fields assoc :updated_by actor)
65 :
66 32 : (map? do-update-set) (assoc do-update-set :updated_by actor)
67 :
68 0 : (sequential? do-update-set) (vec (distinct (conj (vec do-update-set) :updated_by)))
69 :
70 0 : :else do-update-set))
71 :
72 :
73 4 : (defn stamp
74 : "Adds the audit columns to a HoneySQL write query, before `sql/format`.
75 :
76 : - INSERT with map rows: `created_by` (unless the row sets it) and `updated_by`.
77 : - ON CONFLICT ... DO UPDATE: `updated_by` only, so the original creator stays.
78 : - UPDATE: `updated_by`.
79 :
80 : Any other query is returned unchanged. With no acting user the columns are
81 : written as NULL, so a system change never keeps the name of the last person
82 : who touched the row."
83 : [query]
84 12 : (let [actor (actor)]
85 57 : (cond (and (:insert-into query) (sequential? (:values query)))
86 72 : (cond-> (update query :values (partial mapv #(stamp-row % actor)))
87 24 : (:do-update-set query) (update :do-update-set stamp-conflict-update actor))
88 :
89 73 : (and (:update query) (map? (:set query))) (update query :set assoc :updated_by actor)
90 :
91 1 : :else query)))
|