Files
simple-aircraft-manager-rails/app/services/records_audit.rb
T
rickyandClaude Opus 5.5 d8cd462194 Configurable roles: Owner, Mechanic, Pilot, Viewer and custom roles
What people can do on an aircraft now comes from their role there, and
administrators set what each role allows (Admin → Roles).

- Roles are installation-wide sets of permissions (31, grouped as flying,
  squawks and notes, maintenance records, imports and exports, the
  aircraft, notifications). Seeing an aircraft's records needs none.
- Built in: Owner (always every permission, so an aircraft can always be
  managed), Mechanic (shop maintenance: logbook, signing, components,
  inspections, ADs, squawks, reminders), Pilot (what pilots could do
  before) and Viewer (view only). Administrators can change all but
  Owner's, and add their own; a role can be deleted once nobody has it.
- Existing owners and pilots keep exactly what they could do.
- Admin → Users → a user lists their role on every aircraft, to change,
  remove or add. On the aircraft's Access page, anyone who manages people
  hands out roles, but only an Owner can give or take away Owner, nobody
  changes their own (except administrators), and an aircraft always
  keeps an Owner (AircraftAccess).
- Controllers declare the permission each action needs; a request that
  changes something with none declared is refused (and raises in
  development and tests), and a test walks every route to check.
- Weekly reminders and overdue alerts go to roles that get reminders;
  the DRS reminder also needs Manage ADs. The API follows the person's
  role; its owners_only error is now not_permitted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 17:43:38 -05:00

85 lines
4.6 KiB
Ruby

# The QC audit checklist (AC 120-78B 3.2.8.5): items the app checks itself,
# and items the auditor confirms (restore test, comparing a sample of entries
# with their source documents, reviewing users).
class RecordsAudit
Item = Data.define(:key, :title, :auto, :passed, :detail)
MANUAL = LabelSet.new("records_audit.manual", %w[restore_test sample users legacy])
def initialize(since: AuditRun.latest&.performed_at)
@since = since
@policy = RecordkeepingPolicy.current
end
def auto_items
check = IntegrityCheck.latest
backup = BackupRun.latest
revision = ProceduresRevision.current
expired = UserCertificate.joins(:user).merge(User.active).where(expires_on: ...Date.current)
[
Item.new("integrity", t(:integrity), true,
check.present? && check.ok && check.created_at > 2.days.ago,
check ? "#{check.created_at.to_date}: #{check.ok ? t(:no_problems) : check.problems.first(3).join('; ')}" : t(:never_run)),
Item.new("backup", t(:backup), true,
backup.present? && backup.ok && backup.created_at > 2.days.ago,
backup ? "#{backup.created_at.to_date}: #{backup.message}" : t(:never_run)),
*offsite_items,
drs_item,
Item.new("procedures", t(:procedures), true,
revision.present? && @policy.complete?,
revision ? t(:revision, number: revision.number, date: I18n.l(revision.adopted_at.to_date, format: :app)) : t(:not_adopted)),
Item.new("signatures", t(:signatures), true,
check.present? && check.ok, t(:signed_since, count: signatures_since.count, since: since_label)),
Item.new("certificates", t(:certificates), true, true,
expired.any? ? t(:expired_certificates, names: expired.map { "#{_1.user.display_name} #{_1.short_label}" }.join(", ")) : t(:none_expired)),
Item.new("versions", t(:versions), true, true,
versions_since.map { [ _1.version, t(:build, revision: _1.revision) ].compact.join(" ") }.join(", ").presence || t(:no_changes))
]
end
# Entries to compare with source documents: a random few since the last audit, signed ones first.
def sample
pool = LogbookEntry.where(created_at: (@since || Time.at(0))..).includes(:aircraft, :signature)
signed = pool.joins(:signature).order(Arel.sql("RANDOM()")).limit(@policy.audit_sample_size).to_a
(signed + pool.where.not(id: signed.map(&:id)).order(Arel.sql("RANDOM()")).limit(@policy.audit_sample_size - signed.size).to_a).first(@policy.audit_sample_size)
end
def users = User.order(:email_address).includes(:certificates, aircraft_roles: %i[aircraft role])
# Saves the audit from the auditor's answers: { key => { "passed" => "1", "note" => … } }.
def record!(user:, answers:, notes:, sample_ids:)
autos = auto_items
manual = MANUAL.map do |key, title|
a = answers.fetch(key, {})
{ "key" => key, "title" => title, "auto" => false, "passed" => a["passed"] == "1", "detail" => a["note"].to_s.strip.first(1000) }
end
results = autos.map { { "key" => _1.key, "title" => _1.title, "auto" => true, "passed" => _1.passed, "detail" => _1.detail } } + manual
results << { "key" => "sample_entries", "title" => t(:entries_sampled), "auto" => true, "passed" => true,
"detail" => LogbookEntry.where(id: sample_ids).includes(:aircraft).map { "#{_1.aircraft.tail_number} #{_1.date}" }.join(", ") }
AuditRun.create!(performed_by: user, performed_at: Time.current, passed: results.all? { _1["passed"] }, results: results, notes: notes.to_s.strip.first(5000))
end
# The latest automatic restore test (shown next to the manual restore item).
def restore_test = OffsiteConnection.connected? ? OffsiteRun.latest_restore_test : nil
private
def drs_item
due = Aircraft.ordered.select { DrsImport.renewal_due?(_1) || DrsImport.missing_models(_1).any? }
Item.new("drs", t(:drs), true, due.empty?,
due.any? ? t(:needs_drs, aircraft: due.map(&:tail_number).join(", ")) : t(:up_to_date))
end
def offsite_items
return [] unless OffsiteConnection.connected?
run = OffsiteRun.latest_backup
[ Item.new("offsite", t(:offsite), true, run.present? && run.ok && run.recent?,
run ? "#{run.created_at.to_date}: #{run.message}" : t(:never_run)) ]
end
def since_label = @since ? @since.to_date.to_s : t(:the_start)
def t(key, **opts) = I18n.t(key, scope: "services.records_audit", **opts)
def signatures_since = EntrySignature.where(signed_at: (@since || Time.at(0))..)
def versions_since = AppRevision.where(first_seen_at: (@since || Time.at(0))..).order(:first_seen_at)
end