access_trail 0.3.1 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1d0a74eacbb9f280af7c58f1d491ad202beaaf5ffd99df0815f815129d2cacd4
4
- data.tar.gz: ab049ccc8edfc873018abd09fe2e633e654ea4544e5be483cfb74d5c9cf3b843
3
+ metadata.gz: 7205ccadb31d72ae660cf82a1b9e0fd7ec6c6356327f2c534b21a35288209715
4
+ data.tar.gz: 0b8d79cffdc6a8997a7c6cba55fc3cb062866c0ce6e228bc26ee98c6175a110d
5
5
  SHA512:
6
- metadata.gz: 47529c28c307e71adafda42ed3f0aa1ed63dc7b39c872725f063340c443201bd13baefdb1d6dc46b331241fd55b85f1b83a76c131835ddab26fe7def31b5afdf
7
- data.tar.gz: 597af2bf1d70906fecabed9d585ea3dbc526b896a641e768e8953b5044c7a0a6700d5ce149c67566113ba8d5f91ef333efc95cc75412c36902e617078a7b7309
6
+ metadata.gz: 19d56c3ccced24b4b0e97f4aa83fc92663cb247405a172d584ecad7c39613fcde470f258f9d1ebdd151af291c78f5e2fe17f19e250544acdcd7347dad2c6d782
7
+ data.tar.gz: 9e4399910900fb2516cc2ff524b4d8a5feb6338d276f5271c80d57acaf7017d277d16441c4f3f7918b270ebcdffc30615f330732156b5b5e9d451b35ddfda89e
data/README.md CHANGED
@@ -44,10 +44,12 @@ The app refuses to boot while any of the five is unset. It raises
44
44
  `AccessTrail::UnsetSettingError` naming every unset setting in one message, and the
45
45
  check runs again after every code reload.
46
46
 
47
- One more setting is optional:
47
+ Three more settings are optional:
48
48
 
49
49
  ```ruby
50
50
  AccessTrail.export_address = "/csv/exports/access_trail"
51
+ AccessTrail.retention = 2.years
52
+ AccessTrail.session_time_limit = 4.hours
51
53
  ```
52
54
 
53
55
  - `export_address` is the address, as a String, of the host's own export of the
@@ -55,6 +57,12 @@ AccessTrail.export_address = "/csv/exports/access_trail"
55
57
  page's current filters as query parameters, such as
56
58
  `/csv/exports/access_trail?entry_type=Viewed&from=2026-11-01`. When it is not set,
57
59
  the page shows no Export link.
60
+ - `retention` is how long entries are kept before the prune task removes them, as a
61
+ duration. It is one year by default. A host may set a longer period. A period
62
+ shorter than one year stops the app from booting with
63
+ `AccessTrail::RetentionError`.
64
+ - `session_time_limit` is how long a support session may stay open, as a duration.
65
+ It is eight hours by default.
58
66
 
59
67
  ### Recording an entry
60
68
 
@@ -75,12 +83,52 @@ AccessTrail.record(
75
83
  Pass `acted_as:` with the person whose identity was used when it differs from the
76
84
  person who acted. People and accounts are stored by global id, so each must include
77
85
  `GlobalID::Identification`, as every Active Record model does. The entry keeps the
78
- time it was recorded. An entry cannot be changed once it is recorded.
86
+ time it was recorded. `AccessTrail.record` returns the entry, and raises the
87
+ database's own error when the entry could not be written.
79
88
 
80
89
  The record is kept as its type, its public code and its label, copied at the time
81
90
  of recording. Renaming the record later leaves the entry as it was. Never pass a
82
91
  credential, a secret or a request body as the label or the detail.
83
92
 
93
+ `AccessTrail.record!` takes the same arguments and writes the same entry. When the
94
+ entry could not be written, it raises `AccessTrail::NotRecordedError` naming the
95
+ cause, so a host can refuse to show a page whose view was not recorded:
96
+
97
+ ```ruby
98
+ def show
99
+ AccessTrail.record!(person: current_user, account: current_account, entry_type: "Viewed", record_type: "Integration call", record_code: call.public_code, record_label: call.name)
100
+ rescue AccessTrail::NotRecordedError
101
+ render :not_recorded, status: :service_unavailable
102
+ end
103
+ ```
104
+
105
+ ### Entries cannot be changed
106
+
107
+ An entry cannot be changed or removed through the gem once it is recorded. Each of
108
+ these raises `ActiveRecord::ReadOnlyRecord` and leaves the entry as it was:
109
+
110
+ - `update`, `update!`, `update_columns`, `touch`, `increment!`, `destroy` and
111
+ `delete` on an entry.
112
+ - `update_all`, `update_counters`, `delete_all`, `delete_by`, `destroy_all`,
113
+ `upsert` and `upsert_all` on `AccessTrail::Entry` or any query of it.
114
+
115
+ The only way the gem removes entries is the prune task.
116
+
117
+ ### Pruning old entries
118
+
119
+ ```sh
120
+ bin/rails access_trail:prune
121
+ ```
122
+
123
+ The task removes every entry recorded longer ago than `AccessTrail.retention`, and
124
+ keeps every newer one. It then writes a `Pruned` entry whose detail says how many it
125
+ removed, such as `Removed 120 entries older than 1 year`. That entry names
126
+ `access_trail:prune` as who acted and as the account. When the `Pruned` entry cannot
127
+ be written, the task removes nothing.
128
+
129
+ The host schedules the task, for example once a day. The same work is available as
130
+ `AccessTrail.prune`, which returns the `Pruned` entry.
131
+
84
132
  ### Filters
85
133
 
86
134
  The trail is narrowed by five filters, each a query parameter of the same name:
@@ -121,7 +169,8 @@ Each entry carries:
121
169
 
122
170
  The three names come from the host's `name_of` setting. When the person or account
123
171
  was deleted, the name is its stored global id, so the entry still reads and can
124
- still be filtered by it.
172
+ still be filtered by it. On the prune task's `Pruned` entry, both names are
173
+ `access_trail:prune`.
125
174
 
126
175
  ### Recording an export
127
176
 
@@ -200,19 +249,28 @@ person acted as:
200
249
  - `Page opened`, for each request the host's controllers answer while the session is
201
250
  open, with the record type `Page`, the request path as the label and the request
202
251
  method as the detail. The request that starts the session writes none.
203
- - `Support sign-in ended`.
252
+ - `Support sign-in ended`. When the session ended by itself, its detail says why.
253
+
254
+ A session ends by itself on the first page the switch answers after either of these,
255
+ and that page is answered as the staff member:
256
+
257
+ - It has been open longer than `AccessTrail.session_time_limit`. The ended entry's
258
+ detail is `Timed out`.
259
+ - `starter` no longer allows the staff member. The ended entry's detail is
260
+ `No longer allowed to start a support session`.
204
261
 
205
262
  The same calls are available without a controller:
206
263
 
207
264
  ```ruby
208
265
  support_session = AccessTrail.start_support_session(staff: staff, acted_as: user, account: account, reason: "Reproduce a billing error", address: request.remote_ip)
209
- AccessTrail.end_support_session(support_session, address: request.remote_ip)
266
+ AccessTrail.end_support_session(support_session, address: request.remote_ip, detail: "Ticket closed")
210
267
  ```
211
268
 
212
269
  `AccessTrail.start_support_session` records the session and its started entry, and
213
270
  returns the session. It does not keep the session in the Rails session, so the switch
214
271
  does not read it. `AccessTrail.end_support_session` records when the session ended and
215
- writes its ended entry. A session cannot be changed once started, apart from being
272
+ writes its ended entry, with `detail` as the entry's detail when it is given. A
273
+ session cannot be changed once started, apart from being
216
274
  ended once, and cannot be removed.
217
275
 
218
276
  ### The banner
@@ -36,7 +36,25 @@ module AccessTrail
36
36
  def support_session
37
37
  return @support_session if defined?(@support_session)
38
38
 
39
- @support_session = session[SESSION_KEY] && real_person && SupportSession.find_by(id: session[SESSION_KEY], staff_gid: real_person.to_global_id.to_s, ended_at: nil)
39
+ @support_session = open_support_session
40
+ end
41
+
42
+ private
43
+
44
+ def open_support_session
45
+ found = session[SESSION_KEY] && real_person && SupportSession.find_by(id: session[SESSION_KEY], staff_gid: real_person.to_global_id.to_s, ended_at: nil)
46
+ ending = found && support_session_ending(found)
47
+ return found unless ending
48
+
49
+ AccessTrail.end_support_session(found, address: request.remote_ip, detail: ending)
50
+ session.delete(SESSION_KEY)
51
+ nil
52
+ end
53
+
54
+ def support_session_ending(found)
55
+ return "Timed out" if found.started_at < AccessTrail.session_time_limit.ago
56
+
57
+ "No longer allowed to start a support session" unless AccessTrail.starter&.call(real_person)
40
58
  end
41
59
  end
42
60
  end
@@ -1,13 +1,26 @@
1
1
  module AccessTrail
2
2
  class Entry < ActiveRecord::Base
3
+ module Unchangeable
4
+ def update_all(...) = raise(ActiveRecord::ReadOnlyRecord, "#{klass} rows cannot be changed")
5
+
6
+ def delete_all = raise(ActiveRecord::ReadOnlyRecord, "#{klass} rows cannot be removed")
7
+
8
+ def upsert_all(...) = raise(ActiveRecord::ReadOnlyRecord, "#{klass} rows cannot be changed")
9
+ end
10
+
11
+ relation_delegate_class(ActiveRecord::Relation).include(Unchangeable)
12
+
3
13
  def self.name_for(gid)
4
- AccessTrail.name_of.call(GlobalID::Locator.locate(gid))
14
+ located = GlobalID::Locator.locate(gid)
15
+ located ? AccessTrail.name_of.call(located) : gid
5
16
  rescue ActiveRecord::RecordNotFound
6
17
  gid
7
18
  end
8
19
 
9
20
  def readonly? = persisted? || super
10
21
 
22
+ def delete = raise(ActiveRecord::ReadOnlyRecord, "#{self.class} is marked as readonly")
23
+
11
24
  def person_name = self.class.name_for(person_gid)
12
25
 
13
26
  def acted_as_name = acted_as_gid && self.class.name_for(acted_as_gid)
@@ -1,3 +1,3 @@
1
1
  module AccessTrail
2
- VERSION = "0.3.1"
2
+ VERSION = "0.4.0"
3
3
  end
data/lib/access_trail.rb CHANGED
@@ -3,19 +3,31 @@ require "access_trail/engine"
3
3
 
4
4
  module AccessTrail
5
5
  class UnsetSettingError < StandardError; end
6
+ class RetentionError < StandardError; end
7
+ class NotRecordedError < StandardError; end
6
8
 
7
9
  SETTINGS = %i[base_controller person_method layout reader name_of].freeze
8
10
 
9
11
  FILTERS = %i[person account entry_type from to].freeze
10
12
 
13
+ PRUNE_TASK = "access_trail:prune".freeze
14
+
11
15
  mattr_accessor(*SETTINGS)
12
16
  mattr_accessor :export_address
13
17
  mattr_accessor :starter
18
+ mattr_accessor :retention, default: 1.year
19
+ mattr_accessor :session_time_limit, default: 8.hours
14
20
 
15
21
  def self.record(person:, account:, entry_type:, acted_as: nil, record_type: nil, record_code: nil, record_label: nil, detail: nil, address: nil)
16
22
  Entry.create!(person_gid: person.to_global_id.to_s, acted_as_gid: acted_as&.to_global_id&.to_s, account_gid: account.to_global_id.to_s, entry_type: entry_type, record_type: record_type, record_code: record_code, record_label: record_label, detail: detail, address: address)
17
23
  end
18
24
 
25
+ def self.record!(**details)
26
+ record(**details)
27
+ rescue ActiveRecord::ActiveRecordError => error
28
+ raise NotRecordedError, "The access trail entry could not be written: #{error.message}"
29
+ end
30
+
19
31
  def self.start_support_session(staff:, acted_as:, account:, reason:, address: nil)
20
32
  support_session = SupportSession.new(staff_gid: staff.to_global_id.to_s, acted_as_gid: acted_as.to_global_id.to_s, account_gid: account.to_global_id.to_s, reason: reason, started_at: Time.current)
21
33
  return support_session.tap { support_session.errors.add(:base, "#{name_of.call(staff)} may not start a support session.") } unless starter&.call(staff)
@@ -25,9 +37,9 @@ module AccessTrail
25
37
  support_session
26
38
  end
27
39
 
28
- def self.end_support_session(support_session, address: nil)
40
+ def self.end_support_session(support_session, address: nil, detail: nil)
29
41
  support_session.update!(ended_at: Time.current)
30
- record_in(support_session, entry_type: "Support sign-in ended", address: address)
42
+ record_in(support_session, entry_type: "Support sign-in ended", detail: detail, address: address)
31
43
  end
32
44
 
33
45
  def self.record_in(support_session, entry_type:, **details)
@@ -39,6 +51,13 @@ module AccessTrail
39
51
  record(person: person, account: account, entry_type: "Exported", address: address, detail: used.any? ? "Filters: #{used.join(", ")}" : "No filters")
40
52
  end
41
53
 
54
+ def self.prune
55
+ Entry.transaction(requires_new: true) do
56
+ removed = ActiveRecord::Relation.instance_method(:delete_all).bind_call(Entry.where(created_at: ...retention.ago))
57
+ Entry.create!(person_gid: PRUNE_TASK, account_gid: PRUNE_TASK, entry_type: "Pruned", detail: "Removed #{removed} #{"entry".pluralize(removed)} older than #{retention.inspect}")
58
+ end
59
+ end
60
+
42
61
  def self.filters(given)
43
62
  FILTERS.index_with { |filter| given[filter].presence }.compact
44
63
  end
@@ -65,5 +84,6 @@ module AccessTrail
65
84
  def self.check!
66
85
  unset = SETTINGS.reject { |setting| public_send(setting) }
67
86
  raise UnsetSettingError, unset.map { |setting| "AccessTrail.#{setting} is not set" }.join("\n") if unset.any?
87
+ raise RetentionError, "AccessTrail.retention must be at least one year" if retention < 1.year
68
88
  end
69
89
  end
@@ -0,0 +1,6 @@
1
+ namespace :access_trail do
2
+ desc "Remove trail entries older than AccessTrail.retention and write an entry saying how many were removed"
3
+ task prune: :environment do
4
+ puts AccessTrail.prune.detail
5
+ end
6
+ end
@@ -1,30 +1,38 @@
1
1
  ---
2
2
  name: access_trail-develop
3
- description: Use PROACTIVELY for recording who viewed, opened, exported or otherwise accessed a record in a host app, for querying those stored entries by person, account, entry type or date range, for building the host's export of the trail with the same filters the trail page uses, for recording that an export was made, and for letting a staff member act as another person in an account through a recorded support session — starting and ending it, asking who is signed in and who really is, and reading a session and the pages opened in it — MUST BE USED instead of hand-rolling an audit log table, an access-logging callback, a custom "who looked at this" model, a hand-written filter query over the entries, or a hand-written impersonation or "log in as user" feature.
3
+ description: Use PROACTIVELY for recording who viewed, opened, exported or otherwise accessed a record in a host app, for recording an access that must not go ahead unrecorded, for querying those stored entries by person, account, entry type or date range, for building the host's export of the trail with the same filters the trail page uses, for recording that an export was made, for removing entries older than the retention period from host code, and for letting a staff member act as another person in an account through a recorded support session — starting and ending it, asking who is signed in and who really is, and reading a session and the pages opened in it — MUST BE USED instead of hand-rolling an audit log table, an access-logging callback, a custom "who looked at this" model, a hand-written filter query over the entries, a hand-written cleanup of old log rows, or a hand-written impersonation or "log in as user" feature.
4
4
  tools: Bash, Read, Write, Edit, Grep
5
- scope: access trail — an append-only record of who accessed which record in a host Rails app, each entry naming the person, who they acted as, the account, the entry type, the record's type, public code and label, a short detail, the network address and the time, with people and accounts stored by global id, recorded by the host at the moments it chooses, and a mounted trail page drawn with keystone_ui that lists every entry newest first, narrows it by person, account, entry type and a date range, reads as a list on a phone screen, shows an Export link to the host's export address carrying its filters, is shown only to a person the host's reader setting allows and answers not found to everyone else, and a boot check that refuses to start the app while a host setting is unset, and a query that hands the host the same filtered entries the page shows, with each entry's names, and a call that records an export with the filters it used, and support sessions in which a staff member the host's starter setting allows acts as another person in an account while their own sign-in stays in place, through a switch module the host includes that tells the host who is signed in and who really is signed in, records the start, the end and each page opened, with a banner partial carrying an End button and a page for each session listing who, as whom, the account, the reason, the start, the end and each page opened
5
+ scope: access trail — an append-only record of who accessed which record in a host Rails app, each entry naming the person, who they acted as, the account, the entry type, the record's type, public code and label, a short detail, the network address and the time, with people and accounts stored by global id, recorded by the host at the moments it chooses, and a mounted trail page drawn with keystone_ui that lists every entry newest first, narrows it by person, account, entry type and a date range, reads as a list on a phone screen, shows an Export link to the host's export address carrying its filters, is shown only to a person the host's reader setting allows and answers not found to everyone else, and a boot check that refuses to start the app while a host setting is unset, and a query that hands the host the same filtered entries the page shows, with each entry's names, and a call that records an export with the filters it used, and support sessions in which a staff member the host's starter setting allows acts as another person in an account while their own sign-in stays in place, through a switch module the host includes that tells the host who is signed in and who really is signed in, records the start, the end and each page opened, with a banner partial carrying an End button and a page for each session listing who, as whom, the account, the reason, the start, the end and each page opened, and a session that ends by itself on its next page once it is open past the host's time limit or the staff member may no longer start one, and entries that raise an error when anything tries to change or remove them, a prune task the host schedules that removes entries older than the retention period and writes an entry saying how many it removed, a retention period of one year that a host may lengthen and that refuses to boot when shortened, and a strict record call that raises an error when the entry could not be written
6
6
  ---
7
7
 
8
8
  This local adds access trail entries to host code, reads them back with the trail
9
- page's filters, builds the host's export of the trail, and wires support sessions
10
- into the host's actions. It follows these steps exactly, and where a step needs a
11
- choice only the host's owner can make, it asks the developer rather than picking.
9
+ page's filters, builds the host's export of the trail, removes old entries from
10
+ host code, and wires support sessions into the host's actions. It follows these
11
+ steps exactly, and where a step needs a choice only the host's owner can make, it
12
+ asks the developer rather than picking.
12
13
 
13
14
  ## What access_trail is
14
15
 
15
16
  A Rails engine that keeps an append-only table of who accessed which record, who
16
17
  they acted as, in which account, and when. The host calls it at the moments it
17
18
  decides are worth recording, and the gem's trail page lists and filters those
18
- entries. It also runs support sessions, in which a staff member acts as another
19
- person in an account while their own sign-in stays in place, and records the
20
- start, the end and every page opened during the session. Fire this local when host
21
- code needs to record an access, read or filter recorded accesses, export them, or
22
- let staff act as another person.
19
+ entries. Entries cannot be changed or removed, apart from the prune call removing
20
+ those older than the retention period. It also runs support sessions, in which a
21
+ staff member acts as another person in an account while their own sign-in stays in
22
+ place, and records the start, the end and every page opened during the session.
23
+ Fire this local when host code needs to record an access, read or filter recorded
24
+ accesses, export them, remove old ones, or let staff act as another person.
23
25
 
24
26
  ## Interface
25
27
 
26
28
  - `AccessTrail.record` — records one entry and returns it as an
27
29
  `AccessTrail::Entry`.
30
+ - `AccessTrail.record!` — records one entry the same way, and raises
31
+ `AccessTrail::NotRecordedError` when the entry could not be written.
32
+ - `AccessTrail::NotRecordedError` — the error `AccessTrail.record!` raises when the
33
+ entry could not be written, its message naming the reason.
34
+ - `AccessTrail.prune` — removes every entry older than the retention period, records
35
+ a `Pruned` entry saying how many it removed, and returns that entry.
28
36
  - `AccessTrail.entries` — returns the entries matching the five filters, newest
29
37
  first, as an Active Record relation, the same entries the trail page shows for
30
38
  the same parameters.
@@ -32,8 +40,9 @@ let staff act as another person.
32
40
  a Hash, as a Hash with Symbol keys, leaving out blank ones.
33
41
  - `AccessTrail.record_export` — records an `Exported` entry whose detail names
34
42
  the filters the export used, and returns it as an `AccessTrail::Entry`.
35
- - `AccessTrail::Entry` — the Active Record model of a stored entry, read-only once
36
- saved, with the names of its person, acted-as person and account.
43
+ - `AccessTrail::Entry` — the Active Record model of a stored entry, which cannot be
44
+ changed or removed once saved, with the names of its person, acted-as person and
45
+ account.
37
46
  - `AccessTrail.start_support_session` — starts a support session for a given staff
38
47
  member outside a controller, records its started entry, and returns the
39
48
  `AccessTrail::SupportSession`, unsaved with errors when refused.
@@ -60,16 +69,23 @@ let staff act as another person.
60
69
  1. Ask the developer which moments are recorded. Name each candidate as an action
61
70
  and a record, such as "showing an integration call" or "downloading an
62
71
  invoice PDF". The gem records nothing on its own, apart from the support
63
- session entries described below.
72
+ session entries and the `Pruned` entry described below.
64
73
  2. Ask the developer for the entry type of each moment, such as `"Viewed"` or
65
74
  `"Downloaded"`. It is a free String shown on the trail page and offered in its
66
- entry type filter, so grep the host for existing `AccessTrail.record` calls and
67
- reuse the spelling already there rather than coining a second one. `"Exported"`
68
- is the type `AccessTrail.record_export` writes, and `"Support sign-in started"`,
69
- `"Page opened"` and `"Support sign-in ended"` are the types support sessions
70
- write, so never record those types by hand.
71
- 3. In the controller action, job or service where the moment happens, after the
72
- access has succeeded, call:
75
+ entry type filter, so grep the host for existing `AccessTrail.record` and
76
+ `AccessTrail.record!` calls and reuse the spelling already there rather than
77
+ coining a second one. Never record these types by hand: `"Exported"`, which
78
+ `AccessTrail.record_export` writes, `"Pruned"`, which `AccessTrail.prune`
79
+ writes, and `"Support sign-in started"`, `"Page opened"` and
80
+ `"Support sign-in ended"`, which support sessions write.
81
+ 3. Ask the developer, for each moment, whether the access may go ahead when its
82
+ entry could not be written:
83
+ - It may not, as for showing a record whose every viewing must be on the trail:
84
+ call `AccessTrail.record!` before the record is shown, and rescue
85
+ `AccessTrail::NotRecordedError` to refuse the access, as in step 5.
86
+ - It may: call `AccessTrail.record` after the access has succeeded, and ask the
87
+ developer whether to rescue its failure or let the request fail.
88
+ 4. In the controller action, job or service where the moment happens, call:
73
89
  ```ruby
74
90
  AccessTrail.record(
75
91
  person: real_person,
@@ -83,6 +99,7 @@ let staff act as another person.
83
99
  address: request.remote_ip
84
100
  )
85
101
  ```
102
+ `AccessTrail.record!` takes exactly the same arguments.
86
103
  - `person:` — required. The person who acted, any object that responds to
87
104
  `to_global_id`, which every Active Record model does. In a controller, pass
88
105
  `real_person`, so an entry made during a support session names the staff
@@ -106,14 +123,31 @@ let staff act as another person.
106
123
  The record's type, code and label are copied at the moment of recording, so
107
124
  renaming the record later leaves the entry as it was. An entry recorded this way
108
125
  is not tied to the support session, even when one is open.
109
- 4. Never pass a credential, a token, a secret or a request or response body as
126
+ 5. Where the access may not go ahead unrecorded, record first and refuse on the
127
+ error:
128
+ ```ruby
129
+ def show
130
+ AccessTrail.record!(person: real_person, account: current_account, entry_type: "Viewed", record_type: "Integration call", record_code: call.public_code, record_label: call.name, address: request.remote_ip)
131
+ render :show
132
+ rescue AccessTrail::NotRecordedError
133
+ head :service_unavailable
134
+ end
135
+ ```
136
+ Ask the developer what the refused person sees. `AccessTrail.record!` raises
137
+ `AccessTrail::NotRecordedError` for every database failure to write the entry,
138
+ including a `nil` entry type. A `nil` person, account or non-`nil` `acted_as:`
139
+ that does not respond to `to_global_id` raises `NoMethodError` instead, from
140
+ both calls, so pass only objects that are known to be present.
141
+ 6. Never pass a credential, a token, a secret or a request or response body as
110
142
  `record_label:` or `detail:`. The trail page shows both values to every reader.
111
- 5. Write a test that performs the moment and asserts one new entry exists with the
112
- expected `entry_type`, `person_gid` and `record_code`.
143
+ 7. Write a test that performs the moment and asserts one new entry exists with the
144
+ expected `entry_type`, `person_gid` and `record_code`. Where the moment uses
145
+ `AccessTrail.record!`, write one that makes the write fail and asserts the
146
+ access is refused.
113
147
 
114
148
  ### Reading entries
115
149
 
116
- 6. Read entries with `AccessTrail.entries`, never with a hand-written query that
150
+ 8. Read entries with `AccessTrail.entries`, never with a hand-written query that
117
151
  repeats the filters:
118
152
  ```ruby
119
153
  AccessTrail.entries(params)
@@ -135,7 +169,7 @@ let staff act as another person.
135
169
  `YYYY-MM-DD`. Days are read in the app's time zone. With no argument it returns
136
170
  every entry. A Hash with String keys is read as having no filters, so build one
137
171
  with Symbol keys.
138
- 7. The result is a relation ordered newest first, so chain onto it as needed:
172
+ 9. The result is a relation ordered newest first, so chain onto it as needed:
139
173
  ```ruby
140
174
  AccessTrail.entries(params)
141
175
  .where(account_gid: current_account.to_global_id.to_s)
@@ -143,28 +177,30 @@ let staff act as another person.
143
177
  ```
144
178
  `AccessTrail.entries` does not scope by account. Where host code shows entries
145
179
  to anyone other than a trail reader, chain the current account onto it.
146
- 8. To get the filters a request used, call `AccessTrail.filters(params)`. It
147
- returns only the five filters that are present, such as
148
- `{ entry_type: "Viewed", from: "2026-11-01" }`, and passing that Hash to
149
- `AccessTrail.entries` gives the same result as passing `params`.
150
- 9. Read each entry through its attributes and its three names:
151
- - `created_at` — the time it was recorded. There is no `updated_at`.
152
- - `person_name` — who acted.
153
- - `acted_as_name` — who they acted as, or `nil` when no `acted_as:` was
154
- recorded.
155
- - `account_name` — the account.
156
- - `entry_type`, `record_type`, `record_code`, `record_label`, `detail` and
157
- `address` — as recorded.
158
- - `person_gid`, `acted_as_gid` and `account_gid` — the stored global id
159
- Strings, for matching and grouping.
160
- - `support_session_id` — the id of the support session that wrote the entry, or
161
- `nil` for every entry the host recorded itself.
180
+ 10. To get the filters a request used, call `AccessTrail.filters(params)`. It
181
+ returns only the five filters that are present, such as
182
+ `{ entry_type: "Viewed", from: "2026-11-01" }`, and passing that Hash to
183
+ `AccessTrail.entries` gives the same result as passing `params`.
184
+ 11. Read each entry through its attributes and its three names:
185
+ - `created_at` — the time it was recorded. There is no `updated_at`.
186
+ - `person_name` — who acted.
187
+ - `acted_as_name` — who they acted as, or `nil` when no `acted_as:` was
188
+ recorded.
189
+ - `account_name` — the account.
190
+ - `entry_type`, `record_type`, `record_code`, `record_label`, `detail` and
191
+ `address` — as recorded.
192
+ - `person_gid`, `acted_as_gid` and `account_gid` — the stored global id
193
+ Strings, for matching and grouping.
194
+ - `support_session_id` — the id of the support session that wrote the entry, or
195
+ `nil` for every entry the host recorded itself.
162
196
 
163
- The names come from the naming setting the host configured at install. When
164
- the person or account has been deleted, its name is its stored global id
165
- String, so the entry still reads. Each name call looks the person or account
166
- up, so in a loop over many entries keep a Hash of names keyed by the global id
167
- rather than calling the name method for every row.
197
+ The names come from the naming setting the host configured at install. When
198
+ the person or account has been deleted, its name is its stored global id
199
+ String, so the entry still reads. On a `Pruned` entry, the person and the
200
+ account are both the String `access_trail:prune`, and so are both names. Each
201
+ name call looks the person or account up, so in a loop over many entries keep
202
+ a Hash of names keyed by the global id rather than calling the name method for
203
+ every row.
168
204
 
169
205
  ### Building the host's export
170
206
 
@@ -172,13 +208,13 @@ The trail page's Export link points at an address the host serves itself, and it
172
208
  carries the page's filters as query parameters with the same five names. The
173
209
  gem serves no export. When the host needs one:
174
210
 
175
- 10. Ask the developer the export's format, such as CSV, and which columns it
211
+ 12. Ask the developer the export's format, such as CSV, and which columns it
176
212
  holds.
177
- 11. Ask the developer who may download it. The gem's reader check guards only the
213
+ 13. Ask the developer who may download it. The gem's reader check guards only the
178
214
  trail page and the support session pages, not the host's export address, so
179
215
  the export action must refuse everyone the trail page refuses. Reuse the
180
216
  host's own rule for trail readers rather than writing a second one.
181
- 12. In the export action, list the entries and record the export:
217
+ 14. In the export action, list the entries and record the export:
182
218
  ```ruby
183
219
  entries = AccessTrail.entries(params)
184
220
  AccessTrail.record_export(
@@ -193,24 +229,51 @@ gem serves no export. When the host needs one:
193
229
  detail names each filter used in the order person, account, entry type, from,
194
230
  to, such as `"Filters: entry_type Viewed, from 2026-11-01"`, or
195
231
  `"No filters"` when none was used. `address:` may be left out.
196
- 13. Route the export action at the export address the host configured at
232
+ 15. Route the export action at the export address the host configured at
197
233
  install, so the trail page's Export link reaches it. If no export address is
198
234
  configured yet, hand that setting to `access_trail-install`.
199
- 14. Write a test that requests the export with a filter and asserts it holds only
235
+ 16. Write a test that requests the export with a filter and asserts it holds only
200
236
  the matching entries, and one that asserts an `Exported` entry was recorded
201
237
  with that filter in its detail.
202
238
 
239
+ ### Removing old entries
240
+
241
+ The install local schedules the gem's prune task and sets the retention period,
242
+ which is one year unless the host lengthened it. Call `AccessTrail.prune` from host
243
+ code only when the developer wants the removal run from somewhere else, such as
244
+ the host's own scheduled job.
245
+
246
+ 17. Ask the developer where the removal runs and how often, before writing it.
247
+ 18. Call it with no arguments:
248
+ ```ruby
249
+ pruned = AccessTrail.prune
250
+ pruned.detail # => "Removed 42 entries older than 1 year"
251
+ ```
252
+ It removes every entry recorded before the retention period ago, and keeps
253
+ every newer one. It then records a `Pruned` entry and returns it. Its detail
254
+ names how many it removed and the retention period, and says `0 entries` when
255
+ none was old enough. The removal and the `Pruned` entry are written together,
256
+ so when the `Pruned` entry cannot be written nothing is removed and the call
257
+ raises.
258
+ 19. Never remove entries any other way. `delete_all`, `update_all` and
259
+ `upsert_all` on `AccessTrail::Entry` or any relation of it raise
260
+ `ActiveRecord::ReadOnlyRecord`, and so do `update`, `save`, `delete` and
261
+ `destroy` on a saved entry.
262
+ 20. Write a test that records one entry older than the retention period and one
263
+ newer, calls the host's removal, and asserts only the newer one is left beside
264
+ one new `Pruned` entry.
265
+
203
266
  ### Running support sessions
204
267
 
205
268
  The steps below need the switch module the install local adds to the host's base
206
- controller, the starter setting, and the banner in the layout. If any is missing,
207
- hand that work to `access_trail-install` first.
269
+ controller, the starter setting, the session time limit, and the banner in the
270
+ layout. If any is missing, hand that work to `access_trail-install` first.
208
271
 
209
- 15. Ask the developer where a staff member starts a session: which page offers it,
272
+ 21. Ask the developer where a staff member starts a session: which page offers it,
210
273
  how the staff member picks the person to act as and the account, and where
211
274
  they type the reason. Build that as its own page with its own form, posting to
212
275
  a host action.
213
- 16. In that host action, start the session with the controller method and redirect:
276
+ 22. In that host action, start the session with the controller method and redirect:
214
277
  ```ruby
215
278
  def create
216
279
  support_session = start_support_session(
@@ -238,44 +301,55 @@ hand that work to `access_trail-install` first.
238
301
  while that setting is unset. A refused start returns an unsaved session with
239
302
  the message in `errors`, opens nothing and records nothing. A started session
240
303
  records a `Support sign-in started` entry with the reason as its detail.
241
- 17. Always redirect after a start. The new session is read from the next request
304
+ 23. Always redirect after a start. The new session is read from the next request
242
305
  on, so in the request that starts it `support_session` stays `nil` and
243
306
  `effective_person` is still the staff member.
244
- 18. Call the controller method only while someone is signed in, since it raises
307
+ 24. Call the controller method only while someone is signed in, since it raises
245
308
  when `real_person` is `nil`. Starting while a session is already open leaves
246
309
  the earlier one open in the table, so call `end_support_session` before
247
310
  starting another.
248
- 19. Ask the developer which of the host's checks of who is signed in should follow
311
+ 25. Ask the developer which of the host's checks of who is signed in should follow
249
312
  the person acted as. Change each of those to `effective_person`, in
250
313
  controllers and views, and leave on `real_person` every check that must stay
251
314
  with the staff member, such as the check guarding the action that starts a
252
315
  session. The gem never changes the host's own sign-in method.
253
- 20. Ask the developer how the host picks the current account during a session. The
316
+ 26. Ask the developer how the host picks the current account during a session. The
254
317
  gem does not change it. To follow the session's account, read it from the
255
318
  session:
256
319
  ```ruby
257
320
  GlobalID::Locator.locate(support_session.account_gid) if support_session
258
321
  ```
259
- 21. While a session is open, every request answered by a controller that has the
322
+ 27. While a session is open, every request answered by a controller that has the
260
323
  switch module records a `Page opened` entry tied to the session, with the
261
324
  record type `Page`, the request path as the label and the request method as
262
325
  the detail. Add no recording of your own for page views during a session.
263
- 22. End a session from host code with `end_support_session`. It ends the open
264
- session, records a `Support sign-in ended` entry, stops reading the session for
265
- the rest of the request, and does nothing when no session is open. The banner's
266
- End button already ends the session and redirects to `/`, so add a second end
267
- action only when the developer asks for one.
268
- 23. Outside a controller, such as in a console task or a job, use the module calls:
326
+ 28. A session ends by itself when it is first read in a request after either of
327
+ these holds:
328
+ - It has been open longer than the session time limit. Its ended entry's detail
329
+ is `Timed out`.
330
+ - The starter setting no longer allows the staff member. Its ended entry's
331
+ detail is `No longer allowed to start a support session`.
332
+
333
+ In that request `support_session` is `nil`, `effective_person` is the staff
334
+ member, and no `Page opened` entry is recorded for the page. Write no timer or
335
+ check of your own to end a session.
336
+ 29. End a session from host code with `end_support_session`. It ends the open
337
+ session, records a `Support sign-in ended` entry with no detail, stops reading
338
+ the session for the rest of the request, and does nothing when no session is
339
+ open. The banner's End button already ends the session and redirects to `/`,
340
+ so add a second end action only when the developer asks for one.
341
+ 30. Outside a controller, such as in a console task or a job, use the module calls:
269
342
  ```ruby
270
343
  support_session = AccessTrail.start_support_session(staff: staff, acted_as: user, account: account, reason: "Reproduce a billing error", address: request.remote_ip)
271
- AccessTrail.end_support_session(support_session, address: request.remote_ip)
344
+ AccessTrail.end_support_session(support_session, address: request.remote_ip, detail: "Billing error reproduced")
272
345
  ```
273
- `staff:`, `acted_as:`, `account:` and `reason:` are required and `address:` may
274
- be left out, on both calls. The start is refused the same way as in step 16.
275
- It does not keep the session open across requests, so `support_session`,
346
+ `staff:`, `acted_as:`, `account:` and `reason:` are required on the start, and
347
+ `address:` may be left out on both calls. `detail:` on the end is optional and
348
+ becomes the ended entry's detail. The start is refused the same way as in step
349
+ 22. It does not keep the session open across requests, so `support_session`,
276
350
  `effective_person` and the banner never see a session started this way. Ending
277
351
  a session that is already ended raises `ActiveRecord::ReadOnlyRecord`.
278
- 24. Read a session through `AccessTrail::SupportSession`:
352
+ 31. Read a session through `AccessTrail::SupportSession`:
279
353
  - `staff_gid`, `acted_as_gid` and `account_gid` — the stored global id Strings.
280
354
  - `staff_name`, `acted_as_name` and `account_name` — the names, from the same
281
355
  naming setting as entries, falling back to the global id when deleted.
@@ -283,35 +357,43 @@ hand that work to `access_trail-install` first.
283
357
 
284
358
  Its pages opened are
285
359
  `AccessTrail::Entry.where(support_session_id: support_session.id, entry_type: "Page opened")`.
360
+ How it ended is the detail of
361
+ `AccessTrail::Entry.find_by(support_session_id: support_session.id, entry_type: "Support sign-in ended")`.
286
362
  The gem's own page for a session is at
287
363
  `access_trail.support_session_path(support_session)` and is shown only to trail
288
364
  readers.
289
- 25. Write a request test that signs in as an allowed staff member, starts a session,
365
+ 32. Write a request test that signs in as an allowed staff member, starts a session,
290
366
  requests a host page, and asserts `effective_person` is the person acted as,
291
367
  one `Support sign-in started` entry exists and one `Page opened` entry exists
292
368
  for that page. Write one that a disallowed staff member's start opens nothing.
369
+ Write one that travels past the session time limit, requests a page, and
370
+ asserts the session has ended with the detail `Timed out`.
293
371
 
294
372
  ## Conventions
295
373
 
296
- - Record through `AccessTrail.record` or `AccessTrail.record_export`, never through
297
- `AccessTrail::Entry.create` or `new`, so every entry is stored the same way.
374
+ - Record through `AccessTrail.record`, `AccessTrail.record!` or
375
+ `AccessTrail.record_export`, never through `AccessTrail::Entry.create` or `new`,
376
+ so every entry is stored the same way.
377
+ - Remove entries only through `AccessTrail.prune` or the prune task the install
378
+ local schedules. Entries cannot be changed or removed any other way, and every
379
+ attempt raises `ActiveRecord::ReadOnlyRecord`. Never write code that tries.
298
380
  - Start and end support sessions through the calls above, never through
299
381
  `AccessTrail::SupportSession.create` or `update`, and never by writing the Rails
300
382
  session key yourself, so every start, end and page is recorded.
383
+ - A started session is read-only apart from being ended once, and cannot be
384
+ destroyed.
301
385
  - Filter through `AccessTrail.entries`, never by rewriting its date or filter
302
386
  rules, so the host's export always holds what the trail page shows.
303
- - A saved entry is read-only: `update`, `save` and `destroy` on it raise
304
- `ActiveRecord::ReadOnlyRecord`. A started session is read-only apart from being
305
- ended once, and cannot be destroyed. Never write code that changes or removes
306
- either.
307
387
  - A `nil` person or account raises, and so does a `nil` entry type. Record only
308
388
  where a signed-in person and an account are both known.
309
- - `AccessTrail.record` raises on failure rather than returning `false`. Ask the
310
- developer whether a failed recording should stop the access, which is the
311
- result when the call is left unrescued.
389
+ - `AccessTrail.record` raises on failure rather than returning `false`, and
390
+ `AccessTrail.record!` raises `AccessTrail::NotRecordedError`. Ask the developer
391
+ whether a failed recording should stop the access, as in step 3.
312
392
  - Never sign the staff member out or in as the person acted as through the host's
313
393
  sign-in. A support session leaves the host's sign-in as it is.
314
394
  - Adding the gem, the migrations, mounting the engine, the host settings including
315
- who may start a support session, adding the switch module to the base
316
- controller, rendering the banner and the boot error for an unset setting are out
317
- of scope here. Hand that work to `access_trail-install`.
395
+ who may start a support session, the retention period and the session time
396
+ limit, scheduling the prune task, adding the switch module to the base
397
+ controller, rendering the banner, and the boot errors for an unset setting or a
398
+ shortened retention period are out of scope here. Hand that work to
399
+ `access_trail-install`.
@@ -1,8 +1,8 @@
1
1
  ---
2
2
  name: access_trail-info
3
- description: Use to learn what access_trail offers — an append-only record of who accessed which record in a host Rails app, the trail page that lists it and narrows it by filters, the filtered entries a host can ask for to build its own export, recording that an export was made, who may read the trail, support sessions in which a staff member acts as another person while their own sign-in stays in place, and the host settings it needs before the app will start.
3
+ description: Use to learn what access_trail offers — an append-only record of who accessed which record in a host Rails app, the trail page that lists it and narrows it by filters, the filtered entries a host can ask for to build its own export, recording that an export was made, who may read the trail, support sessions in which a staff member acts as another person while their own sign-in stays in place and which end by themselves after a time limit, entries that cannot be changed or removed, pruning entries older than the retention period, a strict way to record that fails loudly, and the host settings it needs before the app will start.
4
4
  tools: Read
5
- scope: access trail — an append-only record of who accessed which record in a host Rails app, each entry naming the person, who they acted as, the account, the entry type, the record's type, public code and label, a short detail, the network address and the time, with people and accounts stored by global id, recorded by the host at the moments it chooses, and a mounted trail page drawn with keystone_ui that lists every entry newest first, narrows it by person, account, entry type and a date range, reads as a list on a phone screen, shows an Export link to the host's export address carrying its filters, is shown only to a person the host's reader setting allows and answers not found to everyone else, and a boot check that refuses to start the app while a host setting is unset, and a query that hands the host the same filtered entries the page shows, with each entry's names, and a call that records an export with the filters it used, and support sessions in which a staff member the host's starter setting allows acts as another person in an account while their own sign-in stays in place, through a switch module the host includes that tells the host who is signed in and who really is signed in, records the start, the end and each page opened, with a banner partial carrying an End button and a page for each session listing who, as whom, the account, the reason, the start, the end and each page opened
5
+ scope: access trail — an append-only record of who accessed which record in a host Rails app, each entry naming the person, who they acted as, the account, the entry type, the record's type, public code and label, a short detail, the network address and the time, with people and accounts stored by global id, recorded by the host at the moments it chooses, and a mounted trail page drawn with keystone_ui that lists every entry newest first, narrows it by person, account, entry type and a date range, reads as a list on a phone screen, shows an Export link to the host's export address carrying its filters, is shown only to a person the host's reader setting allows and answers not found to everyone else, and a boot check that refuses to start the app while a host setting is unset, and a query that hands the host the same filtered entries the page shows, with each entry's names, and a call that records an export with the filters it used, and support sessions in which a staff member the host's starter setting allows acts as another person in an account while their own sign-in stays in place, through a switch module the host includes that tells the host who is signed in and who really is signed in, records the start, the end and each page opened, with a banner partial carrying an End button and a page for each session listing who, as whom, the account, the reason, the start, the end and each page opened, and a session that ends by itself on its next page once it is open past the host's time limit or the staff member may no longer start one, and entries that raise an error when anything tries to change or remove them, a prune task the host schedules that removes entries older than the retention period and writes an entry saying how many it removed, a retention period of one year that a host may lengthen and that refuses to boot when shortened, and a strict record call that raises an error when the entry could not be written
6
6
  ---
7
7
 
8
8
  This local explains access_trail and makes no changes.
@@ -12,14 +12,15 @@ This local explains access_trail and makes no changes.
12
12
  access_trail is a Rails engine that keeps an append-only record of who accessed
13
13
  which record in a host app. Each entry says who acted, who they acted as when
14
14
  that was someone else, for which account, what type of access it was, which
15
- record it was about and when it happened. Once an entry is saved it cannot be
16
- changed.
15
+ record it was about and when it happened. Once an entry is saved, any attempt to
16
+ change or remove it raises an error.
17
17
 
18
18
  Reach for it when a host app has to answer "who looked at or touched this, and
19
19
  when" — for an audit, a client's question, or a compliance requirement. The gem
20
20
  does not watch the app on its own. The host records an entry at each moment it
21
21
  decides is worth recording, such as opening a record's page or downloading a
22
- file.
22
+ file. Where losing an entry is not acceptable, the host can record in a strict
23
+ way that raises the gem's own error when the entry could not be written.
23
24
 
24
25
  The gem adds a trail page that lists every entry newest first and can be
25
26
  narrowed by person, account, entry type and a date range. It is drawn with
@@ -32,7 +33,13 @@ The gem also runs support sessions. A staff member the host allows can act as
32
33
  another person in an account, with a reason, while their own sign-in stays in
33
34
  place. The session's start, its end and every page opened during it are
34
35
  recorded in the trail, a banner with an End button shows while it is open, and
35
- each session has its own page listing what happened in it.
36
+ each session has its own page listing what happened in it. A session ends by
37
+ itself on its next page once it has been open past the host's time limit, or
38
+ once the staff member is no longer allowed to start one.
39
+
40
+ Entries are kept for a retention period, one year unless the host lengthens it.
41
+ A prune task the host schedules removes entries older than that period and
42
+ writes an entry saying how many it removed.
36
43
 
37
44
  ## Interface
38
45
 
@@ -40,24 +47,29 @@ This local declares no commands. The surface is split between the other two:
40
47
 
41
48
  - **access_trail-install** owns adding the gem to a host: the gem line, copying
42
49
  its migrations, mounting the engine, the five required host settings, the
43
- optional export address and starter settings, including the switch module in
44
- the host's controllers, rendering the banner, and the error raised when a
45
- required setting is unset.
50
+ optional export address, starter, retention and time limit settings,
51
+ scheduling the prune task, including the switch module in the host's
52
+ controllers, rendering the banner, and the errors raised at boot when a
53
+ required setting is unset or the retention period is too short.
46
54
  - **access_trail-develop** owns writing code against the gem: recording an
47
- entry, asking for the filtered entries, picking the filters out of a request,
55
+ entry, recording one strictly and the error that raises, pruning from code,
56
+ asking for the filtered entries, picking the filters out of a request,
48
57
  recording an export, reading a stored entry and its names, starting and ending
49
58
  a support session, and asking who is signed in and who really is signed in.
50
59
 
51
60
  ## How to use it
52
61
 
53
62
  - The gem is not in the host yet, the engine is not mounted, the app refuses to
54
- start because a setting is unset, the trail page should link to the host's
55
- export, or the host is being set up to allow support sessions and show their
56
- banner — use **access_trail-install**.
63
+ start because a setting is unset or the retention period is shorter than one
64
+ year, the trail page should link to the host's export, the host is being set
65
+ up to allow support sessions and show their banner, the retention period or
66
+ the session time limit should change, or old entries should be pruned on a
67
+ schedule — use **access_trail-install**.
57
68
  - The gem is installed and the work is recording access at a new moment,
58
- building an export of the trail, reading what was recorded, building the
59
- screen where staff start a support session, or making the host's own code act
60
- for the right person during one — use **access_trail-develop**.
69
+ choosing between plain and strict recording, building an export of the trail,
70
+ reading what was recorded, building the screen where staff start a support
71
+ session, or making the host's own code act for the right person during one —
72
+ use **access_trail-develop**.
61
73
 
62
74
  ## Conventions
63
75
 
@@ -65,8 +77,12 @@ This local declares no commands. The surface is split between the other two:
65
77
  account, the entry type, the record's type, public code and label, a short
66
78
  detail, the network address and the time it was saved. Only the person, the
67
79
  account and the entry type are required.
68
- - **Append-only** — an entry is written once and never edited or removed through
69
- the gem.
80
+ - **Append-only** — an entry is written once. Editing, saving again, deleting or
81
+ destroying a saved entry raises an error, and so does changing or removing
82
+ entries in bulk. Pruning is the only way entries leave the trail.
83
+ - **Strict recording** — recording that raises the gem's own not-recorded error,
84
+ carrying the reason, when the entry could not be written, so the host can
85
+ stop the access it was about to allow.
70
86
  - **Person and account** — stored by their global id rather than by a foreign
71
87
  key, so any model in the host can be either. The host's naming setting turns
72
88
  each back into a display name. When the person or account has been deleted,
@@ -76,8 +92,9 @@ This local declares no commands. The surface is split between the other two:
76
92
  differs from the person who acted, such as staff in a support session.
77
93
  - **Entry type** — a short label the host chooses for the type of access, such
78
94
  as `Viewed`. The gem does not fix the list, apart from the labels it writes
79
- itself: `Exported` for a recorded export, and `Support sign-in started`,
80
- `Support sign-in ended` and `Page opened` for support sessions.
95
+ itself: `Exported` for a recorded export, `Pruned` for a prune, and
96
+ `Support sign-in started`, `Support sign-in ended` and `Page opened` for
97
+ support sessions.
81
98
  - **Record type, public code and label** — how an entry names the record that
82
99
  was accessed. They are plain text copied in at the time of recording, so an
83
100
  entry still reads correctly after the record changes or is deleted. The code
@@ -93,6 +110,13 @@ This local declares no commands. The surface is split between the other two:
93
110
  When it is not, the page shows no Export link.
94
111
  - **Recorded export** — an `Exported` entry whose detail lists the filters the
95
112
  export used, or says `No filters`.
113
+ - **Retention period** — how long entries are kept, one year by default. A host
114
+ may lengthen it, and the app will not boot when it is set shorter than one
115
+ year.
116
+ - **Prune** — removes every entry older than the retention period and, in the
117
+ same step, writes a `Pruned` entry whose detail says how many entries it
118
+ removed and the period used. The prune task names itself as that entry's
119
+ person and account, since no person ran it.
96
120
  - **Reader** — a person the host's reader setting allows to see the trail page
97
121
  and each support session's page. Everyone else, including a visitor who is not
98
122
  signed in, gets not found rather than a refusal, so the pages' existence is
@@ -105,6 +129,12 @@ This local declares no commands. The surface is split between the other two:
105
129
  - **Starter** — the host's rule for which staff members may start a support
106
130
  session. While it is unset, nobody can start one, and a refused start comes
107
131
  back with an error naming the staff member.
132
+ - **Session time limit** — how long a support session may stay open, eight hours
133
+ by default. On the first page the staff member opens after that, the session
134
+ is ended with the detail `Timed out` and the page is answered as the staff
135
+ member. A session whose staff member the starter no longer allows ends the
136
+ same way, with the detail
137
+ `No longer allowed to start a support session`.
108
138
  - **Signed-in person and real person** — during an open support session the
109
139
  signed-in person is the one acted as, and the real person is the staff member.
110
140
  With no session open the two are the same. The host's own code decides which
@@ -124,4 +154,5 @@ This local declares no commands. The surface is split between the other two:
124
154
  inherit from, the method that returns the signed-in person, the layout, the
125
155
  reader rule, and how a person or account is named. The app will not boot
126
156
  while any of them is unset, and the error lists each one that is missing. The
127
- export address and the starter are optional.
157
+ export address, the starter, the retention period and the session time limit
158
+ are optional.
@@ -1,8 +1,8 @@
1
1
  ---
2
2
  name: access_trail-install
3
- description: Use to hook access_trail into a project — adding the gem, copying and running its migrations, mounting the trail page, setting the five required host settings (base controller, person method, layout, reader, name of), the optional export address and the optional support session starter, including the switch in the host's base controller, rendering the support session banner in the host's layout, and clearing the boot error that names an unset setting.
3
+ description: Use to hook access_trail into a project — adding the gem, copying and running its migrations, mounting the trail page, setting the five required host settings (base controller, person method, layout, reader, name of), the optional export address, support session starter, retention period and support session time limit, scheduling the prune task, including the switch in the host's base controller, rendering the support session banner in the host's layout, and clearing the boot errors that name an unset setting or a retention period shorter than one year.
4
4
  tools: Bash, Read, Edit
5
- scope: access trail — an append-only record of who accessed which record in a host Rails app, each entry naming the person, who they acted as, the account, the entry type, the record's type, public code and label, a short detail, the network address and the time, with people and accounts stored by global id, recorded by the host at the moments it chooses, and a mounted trail page drawn with keystone_ui that lists every entry newest first, narrows it by person, account, entry type and a date range, reads as a list on a phone screen, shows an Export link to the host's export address carrying its filters, is shown only to a person the host's reader setting allows and answers not found to everyone else, and a boot check that refuses to start the app while a host setting is unset, and a query that hands the host the same filtered entries the page shows, with each entry's names, and a call that records an export with the filters it used, and support sessions in which a staff member the host's starter setting allows acts as another person in an account while their own sign-in stays in place, through a switch module the host includes that tells the host who is signed in and who really is signed in, records the start, the end and each page opened, with a banner partial carrying an End button and a page for each session listing who, as whom, the account, the reason, the start, the end and each page opened
5
+ scope: access trail — an append-only record of who accessed which record in a host Rails app, each entry naming the person, who they acted as, the account, the entry type, the record's type, public code and label, a short detail, the network address and the time, with people and accounts stored by global id, recorded by the host at the moments it chooses, and a mounted trail page drawn with keystone_ui that lists every entry newest first, narrows it by person, account, entry type and a date range, reads as a list on a phone screen, shows an Export link to the host's export address carrying its filters, is shown only to a person the host's reader setting allows and answers not found to everyone else, and a boot check that refuses to start the app while a host setting is unset, and a query that hands the host the same filtered entries the page shows, with each entry's names, and a call that records an export with the filters it used, and support sessions in which a staff member the host's starter setting allows acts as another person in an account while their own sign-in stays in place, through a switch module the host includes that tells the host who is signed in and who really is signed in, records the start, the end and each page opened, with a banner partial carrying an End button and a page for each session listing who, as whom, the account, the reason, the start, the end and each page opened, and a session that ends by itself on its next page once it is open past the host's time limit or the staff member may no longer start one, and entries that raise an error when anything tries to change or remove them, a prune task the host schedules that removes entries older than the retention period and writes an entry saying how many it removed, a retention period of one year that a host may lengthen and that refuses to boot when shortened, and a strict record call that raises an error when the entry could not be written
6
6
  ---
7
7
 
8
8
  This local follows these steps exactly and invents none. Where a step needs a
@@ -11,20 +11,22 @@ value only the host's owner knows, it asks the developer rather than picking one
11
11
  ## What access_trail is
12
12
 
13
13
  A Rails engine that stores an append-only table of who accessed which record,
14
- shows it on a mounted, filterable trail page, and runs support sessions in which a
15
- staff member acts as another person; hook it in when a host app has to answer who
16
- accessed a record and when, or has to let staff act as a customer with every page
17
- they open recorded.
14
+ shows it on a mounted, filterable trail page, removes entries past a retention
15
+ period through a scheduled task, and runs support sessions in which a staff member
16
+ acts as another person; hook it in when a host app has to answer who accessed a
17
+ record and when, or has to let staff act as a customer with every page they open
18
+ recorded.
18
19
 
19
20
  ## Interface
20
21
 
21
22
  - `gem "access_trail"` — the Gemfile line that adds the gem; it brings in
22
23
  keystone_ui, which draws the trail page, the support session page and the
23
24
  banner.
24
- - `bin/rails access_trail:install:migrations` — copies the gem's three
25
- migrations into the host's `db/migrate`: they create the `access_trail_entries`
26
- table, add its `acted_as_gid` column, create the support sessions table and add
27
- a column on each entry naming the support session it was written in.
25
+ - `bin/rails access_trail:install:migrations` — copies the gem's six migrations
26
+ into the host's `db/migrate`: they create the `access_trail_entries` table, add
27
+ its `acted_as_gid` column, create the `access_trail_support_sessions` table, add
28
+ a `support_session_id` column and index on each entry, and add and then validate
29
+ a foreign key from that column to the support sessions table.
28
30
  - `mount AccessTrail::Engine` — the line in the host's `config/routes.rb` that
29
31
  puts the trail page, each support session's page and the address the banner's
30
32
  End button sends to, under a path the host chooses.
@@ -46,9 +48,22 @@ they open recorded.
46
48
  page shows no Export link.
47
49
  - `AccessTrail.starter=` — optional; a callable given the staff member's own
48
50
  signed-in person that returns whether that person may start a support session.
49
- While it is unset, every start is refused.
51
+ While it is unset, every start is refused. An open session whose staff member
52
+ it no longer allows ends on that staff member's next page.
53
+ - `AccessTrail.retention=` — optional; how long entries are kept before the prune
54
+ task removes them, as a duration such as `2.years`. It is `1.year` when unset.
55
+ A period shorter than one year stops the app from booting.
56
+ - `AccessTrail.session_time_limit=` — optional; how long a support session may
57
+ stay open, as a duration such as `4.hours`. It is `8.hours` when unset. A
58
+ session open longer ends on the staff member's next page.
59
+ - `bin/rails access_trail:prune` — removes every entry recorded longer ago than
60
+ the retention period, writes a `Pruned` entry whose detail says how many it
61
+ removed, and prints that detail, such as
62
+ `Removed 120 entries older than 1 year`. When the `Pruned` entry cannot be
63
+ written, it removes nothing. The host schedules it.
50
64
  - `include AccessTrail::Switch` — the line in a host controller that makes it
51
- read the open support session, tell the host who is signed in during one, and
65
+ read the open support session, tell the host who is signed in during one, end a
66
+ session past its time limit or whose staff member may no longer start one, and
52
67
  record a `Page opened` entry for each request it answers while a session is
53
68
  open.
54
69
  - `render "access_trail/banner"` — the line in a host layout that shows
@@ -56,8 +71,13 @@ they open recorded.
56
71
  session is open, and renders nothing otherwise.
57
72
  - `AccessTrail::UnsetSettingError` — raised at boot, and again after every code
58
73
  reload, while any of the five required settings is unset, with one line per
59
- unset setting in the form `AccessTrail.<setting> is not set`. The export address
60
- and the starter are not checked.
74
+ unset setting in the form `AccessTrail.<setting> is not set`. The export
75
+ address, the starter, the retention period and the time limit are not checked
76
+ by it.
77
+ - `AccessTrail::RetentionError` — raised at boot, and again after every code
78
+ reload, with the message `AccessTrail.retention must be at least one year`
79
+ while the retention period is shorter than one year. It is checked only once
80
+ every required setting is set.
61
81
 
62
82
  ## How to use it
63
83
 
@@ -75,9 +95,10 @@ they open recorded.
75
95
  bin/rails access_trail:install:migrations
76
96
  bin/rails db:migrate
77
97
  ```
78
- This adds three migration files to `db/migrate`, and adds the
79
- `access_trail_entries` and `access_trail_support_sessions` tables to
80
- `db/schema.rb`.
98
+ This adds six migration files to `db/migrate`, and adds the
99
+ `access_trail_entries` and `access_trail_support_sessions` tables and the
100
+ foreign key between them to `db/schema.rb`. On PostgreSQL the index on
101
+ `support_session_id` is built concurrently, outside a transaction.
81
102
  4. Ask the developer for each of the five required settings. None has a safe
82
103
  default:
83
104
  - **Base controller** — which host controller signs people in and should run
@@ -99,10 +120,15 @@ they open recorded.
99
120
  `"/csv/exports/access_trail"`. The link appends the page's filters as query
100
121
  parameters named `person`, `account`, `entry_type`, `from` and `to`, so the
101
122
  host's export must read those. Building that export is out of scope here.
102
- 6. Ask the developer whether the host will run support sessions. If yes, ask for
103
- the rule for who may start one, such as `->(person) { person.staff? }`. If no,
104
- skip steps 9 and 10 and leave the starter unset.
105
- 7. Create `config/initializers/access_trail.rb` with the developer's answers:
123
+ 6. Ask the developer how long entries must be kept. One year is the default and
124
+ the shortest period allowed. If the host's own rules require longer, use that
125
+ period, such as `2.years`.
126
+ 7. Ask the developer whether the host will run support sessions. If yes, ask for
127
+ the rule for who may start one, such as `->(person) { person.staff? }`, and
128
+ whether a session may stay open for the default eight hours or needs a
129
+ different limit, such as `4.hours`. If no, skip steps 11 and 12 and leave the
130
+ starter and the time limit unset.
131
+ 8. Create `config/initializers/access_trail.rb` with the developer's answers:
106
132
  ```ruby
107
133
  AccessTrail.base_controller = "ApplicationController"
108
134
  AccessTrail.person_method = :current_user
@@ -111,12 +137,15 @@ they open recorded.
111
137
  AccessTrail.name_of = ->(person_or_account) { person_or_account.name }
112
138
  AccessTrail.export_address = "/csv/exports/access_trail"
113
139
  AccessTrail.starter = ->(person) { person.staff? }
140
+ AccessTrail.retention = 2.years
141
+ AccessTrail.session_time_limit = 4.hours
114
142
  ```
115
- Leave out the `export_address` line if the developer wants no Export link, and
116
- the `starter` line if the host will not run support sessions. The settings must
117
- be set in an initializer, since the boot check runs once initializers have
118
- loaded.
119
- 8. Ask the developer for the path the gem's pages should live at, then add the
143
+ Leave out the `export_address` line if the developer wants no Export link, the
144
+ `starter` and `session_time_limit` lines if the host will not run support
145
+ sessions, the `retention` line if one year is enough, and the
146
+ `session_time_limit` line if eight hours is enough. The settings must be set in
147
+ an initializer, since the boot check runs once initializers have loaded.
148
+ 9. Ask the developer for the path the gem's pages should live at, then add the
120
149
  mount to `config/routes.rb`, inside any scope or constraint that should guard
121
150
  it:
122
151
  ```ruby
@@ -125,15 +154,23 @@ they open recorded.
125
154
  Do not pass `as:`; the banner reaches the engine's routes by the default name
126
155
  `access_trail`. The trail page is the root of that path, and each support
127
156
  session's page is at `support_sessions/<id>` under it.
128
- 9. Include the switch in the host controller every signed-in page inherits from,
129
- usually the one named as the base controller:
130
- ```ruby
131
- class ApplicationController < ActionController::Base
132
- include AccessTrail::Switch
133
- end
134
- ```
135
- The person method must be callable on that controller.
136
- 10. Ask the developer which layout or layouts staff use while acting as someone
157
+ 10. Ask the developer how the host runs scheduled jobs, such as cron, a
158
+ `config/recurring.yml` for Solid Queue, or the hosting platform's scheduler,
159
+ and how often the prune task should run, such as once a day. Add the task to
160
+ that scheduler in the form it uses:
161
+ ```sh
162
+ bin/rails access_trail:prune
163
+ ```
164
+ Until it is scheduled, no entry is ever removed.
165
+ 11. Include the switch in the host controller every signed-in page inherits from,
166
+ usually the one named as the base controller:
167
+ ```ruby
168
+ class ApplicationController < ActionController::Base
169
+ include AccessTrail::Switch
170
+ end
171
+ ```
172
+ The person method must be callable on that controller.
173
+ 12. Ask the developer which layout or layouts staff use while acting as someone
137
174
  else, then add the banner near the top of each one's `<body>`:
138
175
  ```erb
139
176
  <%= render "access_trail/banner" %>
@@ -145,7 +182,12 @@ they open recorded.
145
182
 
146
183
  - Boot the app with `bin/rails runner 'puts :ok'`. An
147
184
  `AccessTrail::UnsetSettingError` lists every required setting still unset, one
148
- per line; set each one in the initializer and boot again.
185
+ per line; set each one in the initializer and boot again. An
186
+ `AccessTrail::RetentionError` means the retention period is under one year;
187
+ raise it to `1.year` or longer, or remove the line.
188
+ - Run `bin/rails access_trail:prune` once by hand and confirm it prints a line
189
+ such as `Removed 0 entries older than 1 year`, and that the trail page then
190
+ lists a `Pruned` entry.
149
191
  - Visit the mount path signed in as a person the reader rule allows and confirm
150
192
  the page titled "Access trail" loads, with its Person, Account, Entry type,
151
193
  From and To filters. Signed out, or signed in as anyone else, the same path
@@ -163,9 +205,14 @@ they open recorded.
163
205
  - The people and accounts later passed to the gem are stored by global id, so
164
206
  their models must include `GlobalID::Identification`, which every Active Record
165
207
  model does.
208
+ - Entries cannot be changed or removed by host code, a console, or a data fix;
209
+ every such attempt raises `ActiveRecord::ReadOnlyRecord`. The prune task is the
210
+ only way entries are removed, so do not add a host task or migration that
211
+ deletes from `access_trail_entries`.
166
212
  - After upgrading the gem, run `bin/rails access_trail:install:migrations` again
167
213
  to copy any migration the new version adds, then `bin/rails db:migrate`.
168
- - Recording entries, recording an export, reading filtered entries, building the
169
- host's export, making host code ask the switch who is signed in, and starting
170
- and ending support sessions from host actions are out of scope here; hand that
171
- work to `access_trail-develop`.
214
+ - Recording entries, recording entries strictly, recording an export, reading
215
+ filtered entries, building the host's export, calling the prune from host code,
216
+ making host code ask the switch who is signed in, and starting and ending
217
+ support sessions from host actions are out of scope here; hand that work to
218
+ `access_trail-develop`.
@@ -1,4 +1,4 @@
1
- scope: access trail — an append-only record of who accessed which record in a host Rails app, each entry naming the person, who they acted as, the account, the entry type, the record's type, public code and label, a short detail, the network address and the time, with people and accounts stored by global id, recorded by the host at the moments it chooses, and a mounted trail page drawn with keystone_ui that lists every entry newest first, narrows it by person, account, entry type and a date range, reads as a list on a phone screen, shows an Export link to the host's export address carrying its filters, is shown only to a person the host's reader setting allows and answers not found to everyone else, and a boot check that refuses to start the app while a host setting is unset, and a query that hands the host the same filtered entries the page shows, with each entry's names, and a call that records an export with the filters it used, and support sessions in which a staff member the host's starter setting allows acts as another person in an account while their own sign-in stays in place, through a switch module the host includes that tells the host who is signed in and who really is signed in, records the start, the end and each page opened, with a banner partial carrying an End button and a page for each session listing who, as whom, the account, the reason, the start, the end and each page opened
1
+ scope: access trail — an append-only record of who accessed which record in a host Rails app, each entry naming the person, who they acted as, the account, the entry type, the record's type, public code and label, a short detail, the network address and the time, with people and accounts stored by global id, recorded by the host at the moments it chooses, and a mounted trail page drawn with keystone_ui that lists every entry newest first, narrows it by person, account, entry type and a date range, reads as a list on a phone screen, shows an Export link to the host's export address carrying its filters, is shown only to a person the host's reader setting allows and answers not found to everyone else, and a boot check that refuses to start the app while a host setting is unset, and a query that hands the host the same filtered entries the page shows, with each entry's names, and a call that records an export with the filters it used, and support sessions in which a staff member the host's starter setting allows acts as another person in an account while their own sign-in stays in place, through a switch module the host includes that tells the host who is signed in and who really is signed in, records the start, the end and each page opened, with a banner partial carrying an End button and a page for each session listing who, as whom, the account, the reason, the start, the end and each page opened, and a session that ends by itself on its next page once it is open past the host's time limit or the staff member may no longer start one, and entries that raise an error when anything tries to change or remove them, a prune task the host schedules that removes entries older than the retention period and writes an entry saying how many it removed, a retention period of one year that a host may lengthen and that refuses to boot when shortened, and a strict record call that raises an error when the entry could not be written
2
2
 
3
3
  install:
4
4
  - gem "access_trail"
@@ -11,12 +11,19 @@ install:
11
11
  - AccessTrail.name_of=
12
12
  - AccessTrail.export_address=
13
13
  - AccessTrail.starter=
14
+ - AccessTrail.retention=
15
+ - AccessTrail.session_time_limit=
16
+ - bin/rails access_trail:prune
14
17
  - include AccessTrail::Switch
15
18
  - render "access_trail/banner"
16
19
  - AccessTrail::UnsetSettingError
20
+ - AccessTrail::RetentionError
17
21
 
18
22
  develop:
19
23
  - AccessTrail.record
24
+ - AccessTrail.record!
25
+ - AccessTrail::NotRecordedError
26
+ - AccessTrail.prune
20
27
  - AccessTrail.entries
21
28
  - AccessTrail.filters
22
29
  - AccessTrail.record_export
@@ -33,6 +40,7 @@ develop:
33
40
  sources:
34
41
  - lib/access_trail.rb
35
42
  - lib/access_trail/engine.rb
43
+ - lib/tasks/access_trail.rake
36
44
  - config/routes.rb
37
45
  - db/migrate/20261130022543_create_access_trail_entries.rb
38
46
  - db/migrate/20261130190029_add_acted_as_gid_to_access_trail_entries.rb
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: access_trail
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - tylercschneider
@@ -68,6 +68,7 @@ files:
68
68
  - lib/access_trail.rb
69
69
  - lib/access_trail/engine.rb
70
70
  - lib/access_trail/version.rb
71
+ - lib/tasks/access_trail.rake
71
72
  - the_local/agents/access_trail-develop.md
72
73
  - the_local/agents/access_trail-info.md
73
74
  - the_local/agents/access_trail-install.md