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

86 lines
3.1 KiB
Ruby

# Scopes a controller to one aircraft the current user can access, and checks
# what their role there allows (Role, Permission). Unknown and inaccessible
# aircraft both 404 (enumeration-safe). Administrators can do everything.
#
# Each controller says which permission its actions need:
#
# requires_permission :manage_components, except: %i[index show]
# requires_permission :report_squawks, only: %i[new create]
# checks_permission_itself only: %i[edit update] # decided in the controller
#
# Looking needs no permission. A request that changes something (anything but
# GET/HEAD) with no permission declared for its action is refused, so nothing
# is left open by mistake; in development and tests it raises instead.
module AircraftScoped
extend ActiveSupport::Concern
SELF_CHECKED = "(checked by the controller)".freeze
class MissingPermission < StandardError; end
included do
class_attribute :permission_rules, instance_accessor: false, default: []
before_action :set_aircraft
before_action :authorize_permission!
helper_method :current_role, :can?, :owner?
end
class_methods do
def requires_permission(permission, only: nil, except: nil)
self.permission_rules += [ [ permission.to_s, only && Array(only).map(&:to_s), except && Array(except).map(&:to_s) ] ]
end
def checks_permission_itself(only:)
requires_permission(SELF_CHECKED, only: only)
end
end
private
def aircraft_param = params[:aircraft_id]
def set_aircraft
@aircraft = current_user.accessible_aircraft.find(aircraft_param)
end
# Their Role here (nil for an administrator without one).
def current_role
return @current_role if defined?(@current_role)
@current_role = current_user.role_for(@aircraft)
end
# 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? = !Viewer.enabled? && (current_user.admin? || current_role&.owner? || false)
def authorize_permission!
rules = self.class.permission_rules.select do |_, only, except|
(only.nil? || only.include?(action_name)) && (except.nil? || except.exclude?(action_name))
end
if rules.empty?
return if request.get? || request.head?
raise MissingPermission, "#{self.class.name}##{action_name} changes data without declaring a permission" if Rails.env.local?
return permission_denied
end
permission_denied unless rules.all? { |permission, *| permission == SELF_CHECKED || can?(permission) }
end
def permission_denied
respond_to do |format|
format.html { redirect_back fallback_location: aircraft_path(@aircraft), alert: t("flash.common.not_allowed") }
format.any { head :forbidden }
end
end
# Returns id only if it belongs to the given (aircraft-scoped) relation; else nil.
def scoped_id(scope, id)
id.present? && scope.where(id: id).exists? ? id : nil
end
def log_event(category, name, notes: nil)
@aircraft.log_event!(category, name, notes: notes)
end
end