Files
rickyandClaude Opus 5.5 d956cfbe83 Records package viewer: read-only, no account needed
SAM_VIEWER=true (set only by the records package's
viewer/docker-compose.yml) makes the app a read-only copy of an archive,
for whoever receives the records: `docker compose up`, then open
http://localhost:3000.

- At start (bin/docker-entrypoint → sam:viewer:prepare) the package's
  .sam.zip, mounted read-only, is imported once.
- No sign-in: everyone browses as a built-in account. Nothing can be
  changed: any request but GET/HEAD is refused, no role permission
  applies (so no edit buttons), and there's no sign-out or profile. A
  banner names the archive and when it was exported.
- It refuses to start, and every page answers 503, when the database
  has any other account, so turning it on for a real installation stops
  the app instead of exposing its records.
- The viewer listens on 127.0.0.1 only, and skips scheduled jobs
  (reminders, backups, AD searches).
- Docs: the package README (three steps now), Access & sharing, README
  settings and features, the suggested recordkeeping procedures.

Checked end to end with a built image and a real records package: the
archive imported at start, pages browsed without an account, a change
refused, and the container refusing to start once an account existed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 20:03:56 -05:00

69 lines
2.5 KiB
Ruby

class ApplicationController < ActionController::Base
default_form_builder AppFormBuilder
include Authentication
include TimeZones
allow_browser versions: :modern
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
rescue_from ActiveRecord::RecordNotFound do
respond_to do |format|
format.html { render "errors/not_found", status: :not_found, layout: (authenticated? ? "application" : "public") }
format.any { head :not_found }
end
end
# Asked for a format a page doesn't have (/aircraft/1/report.pdf): not a crash.
rescue_from ActionView::MissingTemplate do |error|
raise error if request.format.html?
head :not_acceptable
end
# Input no form produces: a tampered upload reference, a number too big to
# store, a missing required value or a link to a record that isn't there.
# The forms validate what people can enter; this is the safety net.
rescue_from ActiveSupport::MessageVerifier::InvalidSignature, ActiveModel::RangeError,
ActiveRecord::NotNullViolation, ActiveRecord::InvalidForeignKey do |error|
Rails.logger.warn("[input] #{error.class}: #{error.message.truncate(200)}")
respond_to do |format|
format.html { redirect_back_or_to root_path, alert: t("flash.application.invalid_input") }
format.any { head :unprocessable_entity }
end
end
private
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
# Adding or importing an aircraft (User#can_add_aircraft?).
def require_aircraft_adder!
redirect_to root_path, alert: t("flash.aircraft.ask_an_administrator") unless current_user.can_add_aircraft?
end
def require_admin!
redirect_to root_path, alert: t("flash.application.admins_only") unless current_user&.admin?
end
end