diff --git a/README.md b/README.md
index f4dee99..b48aacc 100644
--- a/README.md
+++ b/README.md
@@ -94,6 +94,8 @@ The app is a standard Rails app. Run `bin/rails db:prepare && bin/thrust bin/rai
| `PWNED_PASSWORDS_CHECK` | `false` turns off the check of new passwords against Have I Been Pwned (only the first 5 characters of the password's SHA-1 are sent). Use it if the server has no outbound internet access; the check also allows the password if the service can't be reached. |
| `PASSWORD_LOGIN` | `false` hides email/password sign-in, emailed sign-in links and password resets once SSO works. It's ignored while SSO isn't configured, so you can't lock yourself out. |
| `APP_REPOSITORY_URL` | Optional. The source code link on Admin → About, for a fork. |
+| `APP_IMAGE` | Optional. The image the records package's viewer runs, used as is (for a fork). Default: the published image at the release that made the package. |
+| `SAM_VIEWER`, `SAM_VIEWER_ARCHIVE` | **Only for the records package's viewer** (its `viewer/docker-compose.yml` sets them). `SAM_VIEWER=true` makes the app a read-only copy with no sign-in: it imports the `.sam.zip` at `SAM_VIEWER_ARCHIVE` at start, everyone browses as a built-in account, and nothing can be changed. It refuses to start on a database with any user account, so turning it on for a real installation stops the app instead of exposing its records. |
| `APP_REVISION`, `APP_BUILT_AT` | Set as Docker build arguments (see the `Dockerfile`) so Admin → About shows the version and the records keep a software version history. |
### Single sign-on (Authentik example)
@@ -150,7 +152,7 @@ To back up or move a single aircraft with all its files, use **More → Download
| Logbook | Maintenance entries with search and a link to a scanned document page. Records the signer's certificate kind and number and an inspection's result, and flags what 43.9 / 43.11 needs that's missing. Entries transcribed by Claude stay "unverified" until someone checks them against the scan. Entry templates (built-in pilot preventive maintenance per 43.3(g) and Part 43 Appendix A(c); admins add and edit their own under Admin → Logbook templates). Engine and propeller entries link the component and record its total time and time since overhaul (SMOH/SFOH/SNEW) at the entry, worked out from the entry's airframe total time; sub-components (an oil filter on the engine…) can be included with their own times. Print entries as a PDF on Avery 5163/8163 or 5164/8164 labels or half-letter/letter pages (boxed) to sign and put in the paper logbook. |
| Documents | Collections, multi-file documents with a page viewer (PDFs drawn page by page with PDF.js on phones, fetching only the pages viewed), per-document share visibility, bulk upload, and expiration dates (registration, insurance…). Their text is read for search: PDF text layers directly, scans by Tesseract OCR (in the image) or optionally Claude. |
| Electronic signatures | Logbook entries can be signed electronically along the lines of AC 120-78B chapter 2: the signer reviews the entry, picks their certificate, re-enters their password (or signs in again with SSO) and confirms a "Certified by" statement. The signature records who, which certificate, the statement, the time and a SHA-256 fingerprint of the entry, sealed by the server; the signer gets an email. Signed entries are locked: a correction is a new signed entry that supersedes the original. Owners can invite an outside mechanic to sign one entry with their own account. Disabled accounts can't sign; their past signatures stay. |
-| Records integrity | Every change to maintenance records is kept (who, when, field by field) in a hash chain per aircraft (Activity → Record history). Nightly: a verified SQLite backup in `storage/backups` (how many are kept: Admin → Settings), then a check of the history chains and every signature; admins are emailed on a problem (Admin → About). Software versions are recorded. A records package (Access & Sharing) bundles every entry as a PDF with signatures, a signature register, the history, the status report and the `.sam.zip` for the FAA or a buyer, with a README (and a ready-to-run `viewer/docker-compose.yml`) so the recipient can browse the data in their own local copy of the app. Scans of paper logbooks can be marked as legacy records, with an "electronic records since" date per aircraft. |
+| Records integrity | Every change to maintenance records is kept (who, when, field by field) in a hash chain per aircraft (Activity → Record history). Nightly: a verified SQLite backup in `storage/backups` (how many are kept: Admin → Settings), then a check of the history chains and every signature; admins are emailed on a problem (Admin → About). Software versions are recorded. A records package (Access & Sharing) bundles every entry as a PDF with signatures, a signature register, the history, the status report and the `.sam.zip` for the FAA or a buyer, with a README and a ready-to-run `viewer/docker-compose.yml`: `docker compose up` gives the recipient a read-only copy of the app on their own computer, with the archive imported and no sign-in (`SAM_VIEWER`). Scans of paper logbooks can be marked as legacy records, with an "electronic records since" date per aircraft. |
| Off-site backup | Nightly copy of the verified database snapshot (newest 30 kept) and every new uploaded file to Google Drive, using your own Google OAuth client and the `drive.file` scope (the app sees only its own files). Restore test from the admin page; failures emailed to admins; `bin/rails offsite:restore_files` brings files back after a restore. |
| Recordkeeping procedures and audits | Admin → Recordkeeping Procedures generates the procedures document AC 120-78B asks for (who is responsible, who may sign, signing, corrections, backups, access, audits, transfer, legacy records) from the system setup plus a few details you fill in (with suggested text built from the installation's settings; a revision can't be adopted while a [bracketed] blank is left); adopt it as numbered, unchangeable revisions, included in the records package. Admin → Records Audits is a periodic QC checklist (app checks plus a restore test, a sample of entries against source documents and a user review); admins get the due date in the weekly reminder. The users' instructions are the guide's [Electronic records and signing](docs/guide/records.md) page. |
| Major repairs and alterations | Form 337, STC, and ICA tracking; an STC's ICA can become a tracked recurring inspection. |
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb
index d28aa1a..c3e1ff8 100644
--- a/app/controllers/application_controller.rb
+++ b/app/controllers/application_controller.rb
@@ -6,6 +6,7 @@ class ApplicationController < ActionController::Base
include RejectsNullBytes
prepend_before_action :redirect_to_setup
+ prepend_before_action :viewer_guard # first (see Viewer)
before_action { Current.ip_address = request.remote_ip } # for record history
helper_method :current_user
@@ -40,6 +41,18 @@ class ApplicationController < ActionController::Base
def current_user = Current.user
# First run: no users yet → create the initial admin.
+ # Viewer mode: never with real accounts, and nothing can be changed.
+ def viewer_guard
+ return unless Viewer.enabled?
+ return render(plain: Viewer::RealAccounts.new.message, status: :service_unavailable) if Viewer.real_accounts?
+ Viewer.user
+ return if request.get? || request.head?
+ respond_to do |format|
+ format.html { redirect_back fallback_location: root_path, alert: t("viewer.read_only") }
+ format.any { head :forbidden }
+ end
+ end
+
def redirect_to_setup
redirect_to new_setup_path if !User.exists? && !is_a?(SetupController)
end
diff --git a/app/controllers/concerns/aircraft_scoped.rb b/app/controllers/concerns/aircraft_scoped.rb
index 00970fc..990a4b3 100644
--- a/app/controllers/concerns/aircraft_scoped.rb
+++ b/app/controllers/concerns/aircraft_scoped.rb
@@ -49,10 +49,11 @@ module AircraftScoped
@current_role = current_user.role_for(@aircraft)
end
- def can?(permission) = current_user.admin? || current_role&.can?(permission) || false
+ # Nothing in viewer mode (a read-only copy, see Viewer).
+ def can?(permission) = !Viewer.enabled? && (current_user.admin? || current_role&.can?(permission) || false)
# The Owner role (or an administrator): everything.
- def owner? = current_user.admin? || current_role&.owner? || false
+ def owner? = !Viewer.enabled? && (current_user.admin? || current_role&.owner? || false)
def authorize_permission!
rules = self.class.permission_rules.select do |_, only, except|
diff --git a/app/controllers/concerns/authentication.rb b/app/controllers/concerns/authentication.rb
index ea09382..3130404 100644
--- a/app/controllers/concerns/authentication.rb
+++ b/app/controllers/concerns/authentication.rb
@@ -21,7 +21,12 @@ module Authentication
end
def require_authentication
- resume_session || request_authentication
+ resume_session || start_viewer_session || request_authentication
+ end
+
+ # Viewer mode: everyone is its built-in account, without signing in.
+ def start_viewer_session
+ start_new_session_for(Viewer.user) if Viewer.enabled? && !Viewer.real_accounts?
end
def resume_session
diff --git a/app/models/user.rb b/app/models/user.rb
index 9bc5979..e40b9ca 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -123,10 +123,11 @@ class User < ApplicationRecord
# Adding an aircraft (or importing one) makes you its Owner: administrators,
# or everyone when Admin → Settings allows it.
- def can_add_aircraft? = admin? || Setting.anyone_adds_aircraft?
+ def can_add_aircraft? = !Viewer.enabled? && (admin? || Setting.anyone_adds_aircraft?)
# Administrators can do everything on every aircraft.
def can?(permission, aircraft)
+ return false if Viewer.enabled?
admin? || role_for(aircraft)&.can?(permission) || false
end
diff --git a/app/models/viewer.rb b/app/models/viewer.rb
new file mode 100644
index 0000000..ecca3f8
--- /dev/null
+++ b/app/models/viewer.rb
@@ -0,0 +1,63 @@
+# Viewer mode (SAM_VIEWER=true): a read-only copy of an aircraft archive for
+# whoever receives a records package (the FAA, a buyer), with no sign-in. It's
+# what the package's viewer/docker-compose.yml runs, on that person's own
+# computer.
+#
+# - At start (bin/rails sam:viewer:prepare, from bin/docker-entrypoint), the
+# archive at SAM_VIEWER_ARCHIVE is imported, owned by a built-in account.
+# - Everyone is that account (Authentication): no sign-in.
+# - Nothing can be changed: requests other than GET/HEAD are refused
+# (ApplicationController), and no role permission applies (AircraftScoped).
+#
+# So that it can never open a real installation, it refuses to run when the
+# database has any account other than the built-in one: setting SAM_VIEWER on
+# a real server stops it rather than exposing its records.
+module Viewer
+ EMAIL = "viewer@sam-viewer.invalid".freeze
+
+ class RealAccounts < StandardError
+ def message = "SAM_VIEWER is on, but this database has user accounts: viewer mode is only for an empty " \
+ "copy made to look at an archive. Turn SAM_VIEWER off for a real installation."
+ end
+
+ def self.enabled? = ENV["SAM_VIEWER"] == "true"
+ def self.archive = ENV["SAM_VIEWER_ARCHIVE"].presence
+ # What was imported, for the banner (kept with the data).
+ def self.info_path = Pathname(ENV["SAM_VIEWER_INFO"].presence || Rails.root.join("storage/viewer.json"))
+ def self.real_accounts? = User.where.not(email_address: EMAIL).exists?
+
+ # The built-in account everyone browses as (made when first needed). It
+ # can't sign in by password: nobody knows it.
+ def self.user
+ User.find_or_create_by!(email_address: EMAIL) do |u|
+ u.name = I18n.t("viewer.account_name")
+ u.password = SecureRandom.base58(40)
+ end
+ end
+
+ # Checks the database, then imports the archive the first time.
+ def self.prepare!(output: $stdout)
+ raise RealAccounts if real_accounts?
+ owner = user
+ if Aircraft.exists?
+ output.puts "Viewer: already imported (#{Aircraft.ordered.pluck(:tail_number).join(', ')})."
+ elsif archive.nil? || !File.file?(archive)
+ output.puts "Viewer: no archive at SAM_VIEWER_ARCHIVE (#{archive.inspect}); nothing to show."
+ else
+ output.puts "Viewer: importing #{File.basename(archive)}…"
+ preview = AircraftArchive::Importer.check(archive)
+ result = AircraftArchive::Importer.new(archive, owner: owner).run
+ File.write(info_path, { file: File.basename(archive), tail_number: result[:aircraft].tail_number,
+ exported_at: preview.exported_at&.iso8601 }.to_json)
+ output.puts "Viewer: imported #{result[:aircraft].tail_number}: #{result[:counts].map { |k, n| "#{n} #{k.tr('_', ' ')}" }.join(', ')}"
+ end
+ output.puts "Viewer ready: open http://localhost:3000"
+ end
+
+ # What the banner says: the archive's file and when it was exported.
+ def self.info
+ JSON.parse(info_path.read) if info_path.file?
+ rescue JSON::ParserError
+ nil
+ end
+end
diff --git a/app/services/records_package.rb b/app/services/records_package.rb
index 646fba3..a0ec2aa 100644
--- a/app/services/records_package.rb
+++ b/app/services/records_package.rb
@@ -76,30 +76,35 @@ class RecordsPackage
"#{image.sub(/:[^:\/]+\z/, '')}:#{version}"
end
- # A ready-to-run local viewer, with its own throwaway secret key (never this
- # installation's), plain HTTP on localhost.
+ # A ready-to-run local viewer of the archive: read-only, no account needed
+ # (viewer mode, see Viewer), reachable only from that computer, with its own
+ # throwaway secret key (never this installation's).
def viewer_compose
image = viewer_image
+ archive = "#{@aircraft.tail_number.downcase}.sam.zip"
<<~YAML
- # Simple Aircraft Manager on your own computer, to browse #{@aircraft.tail_number.downcase}.sam.zip.
- # 1. docker compose up -d
- # 2. Open http://localhost:3000 and create the first (admin) account. It asks
- # for a one-time setup code: docker compose logs web | grep "Setup code"
- # 3. Fleet -> More -> Import from a .sam.zip archive: choose #{@aircraft.tail_number.downcase}.sam.zip
- # Stop: docker compose down (add -v to delete this local copy)
+ # Simple Aircraft Manager on your own computer, showing #{archive} read-only.
+ # 1. In this folder: docker compose up
+ # The first start imports the records; wait for "Viewer ready".
+ # 2. Open http://localhost:3000 (no account needed).
+ # Stop: Ctrl+C, then docker compose down (add -v to delete this local copy).
# Prepared with software version #{SystemInfo.new.version_label}.#{" Source: #{repository}" if repository}
services:
web:
#{image ? "image: #{image}" : "build: . # no published image known: build from the source code"}
restart: "no"
ports:
- - "3000:80"
+ - "127.0.0.1:3000:80" # this computer only
environment:
+ SAM_VIEWER: "true"
+ SAM_VIEWER_ARCHIVE: /import/#{archive}
SECRET_KEY_BASE: #{SecureRandom.hex(64)} # for this local copy only
APP_HOST: localhost:3000
FORCE_SSL: "false"
SOLID_QUEUE_IN_PUMA: "true"
+ SOLID_QUEUE_SKIP_RECURRING: "true" # no reminders, backups or AD searches
volumes:
+ - ../#{archive}:/import/#{archive}:ro # the archive next to this folder, in the package
- viewer_storage:/rails/storage
volumes:
viewer_storage:
diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb
index c058f7e..30a6034 100644
--- a/app/views/layouts/application.html.erb
+++ b/app/views/layouts/application.html.erb
@@ -45,7 +45,7 @@
<%= current_user.email_address %>
- <%= link_to t(".profile"), edit_profile_path, class: "block px-4 py-2.5 hover:bg-slate-100" %> + <% unless Viewer.enabled? %><%= link_to t(".profile"), edit_profile_path, class: "block px-4 py-2.5 hover:bg-slate-100" %><% end %> <%= link_to t(".search"), search_path, class: "block px-4 py-2.5 hover:bg-slate-100" %> <%= link_to t(".help"), help_path, class: "block px-4 py-2.5 hover:bg-slate-100" %> <% if current_user.admin? %> @@ -54,7 +54,7 @@ <%= link_to label, path, class: "block px-4 py-2.5 hover:bg-slate-100" %> <% end %> <% end %> -