access_trail
Records who accessed which record in a host app, and lists those entries on a page. Each entry names the person who acted, who they acted as, the account they acted in, the type of entry, the record touched and when. The host decides which moments are recorded and who may read the trail. The trail page can be narrowed by filters, and the host can ask the gem for the same filtered entries to build its own export.
The gem also runs support sessions, in which a staff member acts as another person in an account and then returns to their own sign-in. The start, the end and every page opened during a session are recorded.
Usage
Copy and run the migrations that create the access_trail_entries and
access_trail_support_sessions tables:
bin/rails access_trail:install:migrations
bin/rails db:migrateIn an initializer, set the five host settings:
AccessTrail.base_controller = "ApplicationController"
AccessTrail.person_method = :current_user
AccessTrail.layout = "application"
AccessTrail.reader = ->(person) { person.admin? }
AccessTrail.name_of = ->(person_or_account) { person_or_account.name }-
base_controlleris the name, as a String, of the host controller the trail page inherits from. Its before-actions, including sign-in, run before the page. -
person_methodis the name, as a Symbol, of the method on that controller that returns the signed-in person, ornilwhen no one is signed in. -
layoutis the name of the host layout the trail page is drawn in. -
readeris called with the signed-in person and returns whether that person may read the trail. -
name_ofis called with a person or an account and returns the name the trail page shows for it.
The app refuses to boot while any of the five is unset. It raises
AccessTrail::UnsetSettingError naming every unset setting in one message, and the
check runs again after every code reload.
Three more settings are optional:
AccessTrail.export_address = "/csv/exports/access_trail"
AccessTrail.retention = 2.years
AccessTrail.session_time_limit = 4.hours-
export_addressis the address, as a String, of the host's own export of the trail. When it is set, the trail page shows an Export link to it carrying the page's current filters as query parameters, such as/csv/exports/access_trail?entry_type=Viewed&from=2026-11-01. When it is not set, the page shows no Export link. -
retentionis how long entries are kept before the prune task removes them, as a duration. It is one year by default. A host may set a longer period. A period shorter than one year stops the app from booting withAccessTrail::RetentionError. -
session_time_limitis how long a support session may stay open, as a duration. It is eight hours by default.
Recording an entry
AccessTrail.record(
person: current_user,
account: current_account,
entry_type: "Viewed",
record_type: "Integration call",
record_code: call.public_code,
record_label: call.name,
detail: "Opened its request and response",
address: request.remote_ip
)person, account and entry_type are required, and the rest may be left out.
Pass acted_as: with the person whose identity was used when it differs from the
person who acted. People and accounts are stored by global id, so each must include
GlobalID::Identification, as every Active Record model does. The entry keeps the
time it was recorded. AccessTrail.record returns the entry, and raises the
database's own error when the entry could not be written.
The record is kept as its type, its public code and its label, copied at the time of recording. Renaming the record later leaves the entry as it was. Never pass a credential, a secret or a request body as the label or the detail.
AccessTrail.record! takes the same arguments and writes the same entry. When the
entry could not be written, it raises AccessTrail::NotRecordedError naming the
cause, so a host can refuse to show a page whose view was not recorded:
def show
AccessTrail.record!(person: current_user, account: current_account, entry_type: "Viewed", record_type: "Integration call", record_code: call.public_code, record_label: call.name)
rescue AccessTrail::NotRecordedError
render :not_recorded, status: :service_unavailable
endEntries cannot be changed
An entry cannot be changed or removed through the gem once it is recorded. Each of
these raises ActiveRecord::ReadOnlyRecord and leaves the entry as it was:
-
update,update!,update_columns,touch,increment!,destroyanddeleteon an entry. -
update_all,update_counters,delete_all,delete_by,destroy_all,upsertandupsert_allonAccessTrail::Entryor any query of it.
The only way the gem removes entries is the prune task.
Pruning old entries
bin/rails access_trail:pruneThe task removes every entry recorded longer ago than AccessTrail.retention, and
keeps every newer one. It then writes a Pruned entry whose detail says how many it
removed, such as Removed 120 entries older than 1 year. That entry names
access_trail:prune as who acted and as the account. When the Pruned entry cannot
be written, the task removes nothing.
The host schedules the task, for example once a day. The same work is available as
AccessTrail.prune, which returns the Pruned entry.
Filters
The trail is narrowed by five filters, each a query parameter of the same name:
-
person— the global id of the person who acted, such asgid://app/User/42. -
account— the global id of the account. -
entry_type— the entry type, such asViewed. -
from— a date asYYYY-MM-DD, keeping entries recorded on or after it. -
to— a date asYYYY-MM-DD, keeping entries recorded on or before it.
A blank filter and a date that cannot be read narrow nothing. Dates are read in the app's time zone.
Asking for entries
AccessTrail.entries(params)
AccessTrail.entries(entry_type: "Viewed", from: "2026-11-01")AccessTrail.entries takes a request's parameters or a Hash with Symbol keys, reads
only the five filters from it, and returns the matching AccessTrail::Entry
records newest first, as an Active Record relation. Given the parameters of a
trail page address, it returns the same entries that page shows. With no argument it
returns every entry.
AccessTrail.filters(params) returns the filters picked out of a request's
parameters as a Hash with Symbol keys, leaving out blank ones.
Each entry carries:
-
created_at— the time it was recorded. -
person_name— who acted. -
acted_as_name— who they acted as, ornilwhen they acted as themselves. -
account_name— the account. -
entry_type,record_type,record_code,record_label,detailandaddress— as recorded.
The three names come from the host's name_of setting. When the person or account
was deleted, the name is its stored global id, so the entry still reads and can
still be filtered by it. On the prune task's Pruned entry, both names are
access_trail:prune.
Recording an export
AccessTrail.record_export(person: current_user, account: current_account, filters: params, address: request.remote_ip)AccessTrail.record_export records an Exported entry naming who made the export,
with the filters it used in the detail, such as Filters: entry_type Viewed, from 2026-11-01, or No filters. address may be left out.
Support sessions
A support session lets a staff member act as another person in an account. The staff member's own sign-in stays in place, and the gem never signs anyone in or out through the host's sign-in. While the session is open, the host asks the gem who is signed in and is told the person acted as.
Set who may start a support session in the initializer:
AccessTrail.starter = ->(person) { person.staff? }-
starteris called with the staff member's own signed-in person and returns whether that person may start a support session. While it is unset, every start is refused.
Include the switch in the host's base controller:
class ApplicationController < ActionController::Base
include AccessTrail::Switch
endAccessTrail::Switch gives the controller and its views these methods:
-
effective_person— the person acted as while a support session is open, and otherwise the personperson_methodreturns. Ask this wherever the host asks who is signed in. -
real_person— always the personperson_methodreturns, the staff member during a support session. -
support_session— the openAccessTrail::SupportSession, ornil. A session is read only while the staff member who started it is the one signed in. -
start_support_session(acted_as:, account:, reason:)— starts a session forreal_personand returns it. -
end_support_session— ends the open session, if there is one.
The open session's id is kept in the Rails session under
:access_trail_support_session_id. Nothing else in the Rails session is changed.
Starting a session from a host action:
def create
support_session = start_support_session(acted_as: User.find(params[:user_id]), account: Account.find(params[:account_id]), reason: params[:reason])
if support_session.persisted?
redirect_to root_path
else
redirect_back_or_to root_path, alert: support_session.errors.full_messages.to_sentence
end
endA start is refused when starter does not allow the staff member, with the message
<name> may not start a support session., and when the reason is blank, with the
message Reason can't be blank. A refused start returns an unsaved session carrying
the message in errors and opens nothing.
Each session writes these entries, all naming the staff member as who acted and the person acted as:
-
Support sign-in started, with the reason as its detail. -
Page opened, for each request the host's controllers answer while the session is open, with the record typePage, the request path as the label and the request method as the detail. The request that starts the session writes none. -
Support sign-in ended. When the session ended by itself, its detail says why.
A session ends by itself on the first page the switch answers after either of these, and that page is answered as the staff member:
- It has been open longer than
AccessTrail.session_time_limit. The ended entry's detail isTimed out. -
starterno longer allows the staff member. The ended entry's detail isNo longer allowed to start a support session.
The same calls are available without a controller:
support_session = AccessTrail.start_support_session(staff: staff, acted_as: user, account: account, reason: "Reproduce a billing error", address: request.remote_ip)
AccessTrail.end_support_session(support_session, address: request.remote_ip, detail: "Ticket closed")AccessTrail.start_support_session records the session and its started entry, and
returns the session. It does not keep the session in the Rails session, so the switch
does not read it. AccessTrail.end_support_session records when the session ended and
writes its ended entry, with detail as the entry's detail when it is given. A
session cannot be changed once started, apart from being
ended once, and cannot be removed.
The banner
Render the banner in the host's layout:
<%= render "access_trail/banner" %>While a support session is open, it says Signed in as <person> in <account> and
carries an End button. With no session open it renders nothing. The layout's
controller must include AccessTrail::Switch.
The End button sends DELETE to the engine's support_session address, which ends the
open session and redirects to /.
The trail page
Mount the engine:
mount AccessTrail::Engine => "/access_trail"The page at the mount path lists every entry, newest first, showing who acted, the
entry type, the record's label, the account and the time. Above the list, a filter
form narrows it by person, account, entry type and a from and to date. The person and
account filters offer each person and account on the trail by name, and the entry
type filter offers each entry type recorded. The page reads its filters from its
query parameters and lists its entries through AccessTrail.entries, so a host's
export given the same parameters holds the same entries. On a phone screen each
entry is shown as a list item with each value under its heading. The page is drawn
with keystone_ui, so the host must run keystone_ui.
A person the reader setting does not allow, and a visitor who is not signed in, is
answered not found.
A support session's page
Each support session has a page at support_sessions/<id> under the mount path,
showing who started it, as whom, the account, the reason, when it started and when it
ended, or Open while it is open. Below them it lists each page opened during the
session, oldest first, with its path, its request method and the time. It is shown to
the same people as the trail page and answers not found to everyone else.
Installation
gem "access_trail"License
The gem is available as open source under the terms of the MIT License.