mirror of
https://github.com/rickbarrette/simple-aircraft-manager-rails.git
synced 2026-11-17 23:50:36 -05:00
Electronic signatures on logbook entries (AC 120-78B chapter 2)
- Sign an entry: review exactly what's certified, choose a current certificate (eligibility checked: pilot only for preventive maintenance, inspections need their result and time), re-enter the password or sign in again with SSO (prompt=login, within 5 minutes), and confirm the "Certified by" statement (43.9 return to service, 43.11(a)(5) for inspections, preventive maintenance for pilots) - The signature stores signer, certificate, statement, UTC time, how they authenticated, IP/browser, a SHA-256 fingerprint of the entry's content and a readable snapshot; an HMAC seal covers the signature record. Status shows Valid / Superseded / Altered since signing. The signer is emailed - Signed entries (and their sub-components) can't be edited or deleted; a correction entry, once signed, supersedes the original (kept) - Owners invite an outside mechanic by email to sign one entry: one-time, 14-day link; they create or use their own account and certificate and see only that entry - Admins can disable accounts (signed out, can't sign in or sign; past signatures kept); users with signatures can't be deleted - Printouts show the electronic signature and fingerprint; archives carry signatures (marked imported, with their origin) and corrections Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
7caa213e7a
commit
d719f663bc
47 files changed
+1059
-17
No files matched your search
@@ -26,6 +26,12 @@ class LogbookEntry < ApplicationRecord
|
||||
# Sub-components it also covers (the oil filter on the engine…).
|
||||
has_many :parts, -> { includes(component: :component_type).order(:id) }, class_name: "LogbookEntryPart",
|
||||
dependent: :destroy, autosave: true
|
||||
# Electronic signature (AC 120-78B): once signed the entry is locked; a
|
||||
# correction is a new entry that supersedes it (2.2.8).
|
||||
has_one :signature, class_name: "EntrySignature", dependent: :destroy
|
||||
has_many :signing_invitations, dependent: :destroy
|
||||
belongs_to :supersedes, class_name: "LogbookEntry", optional: true
|
||||
has_one :superseded_by, class_name: "LogbookEntry", foreign_key: :supersedes_id, inverse_of: :supersedes, dependent: :nullify
|
||||
has_many :inspection_records, dependent: :nullify
|
||||
has_many :ad_compliances, dependent: :nullify
|
||||
has_many :squawks, dependent: :nullify
|
||||
@@ -38,6 +44,12 @@ class LogbookEntry < ApplicationRecord
|
||||
validates :component_hours_at_entry, :component_since_overhaul_at_entry, numericality: { greater_than_or_equal_to: 0 }, allow_nil: true
|
||||
normalizes :signoff_certificate_type, :inspection_result, :signoff_certificate_number, with: ->(v) { v.strip.presence }
|
||||
|
||||
# Fields that may still change on a signed entry: its link to a scanned
|
||||
# page, the transcription check, and being superseded by a correction.
|
||||
UNSIGNED_FIELDS = %w[document_id page_number verified superseded_at updated_at].freeze
|
||||
before_update :keep_signed_content
|
||||
before_destroy(prepend: true) { throw :abort if signed? && !destroyed_by_association }
|
||||
|
||||
scope :recent_first, -> { order(date: :desc, created_at: :desc) }
|
||||
scope :unverified, -> { where(verified: false) }
|
||||
|
||||
@@ -49,6 +61,90 @@ class LogbookEntry < ApplicationRecord
|
||||
|
||||
def to_s = "#{date} — #{LOG_TYPES[log_type]}"
|
||||
|
||||
def signed? = signature.present?
|
||||
def superseded? = superseded_at.present?
|
||||
|
||||
# What a signature covers: the entry's own content, in a fixed form, so
|
||||
# the fingerprint changes if anything in it does.
|
||||
def content_payload
|
||||
num = ->(v) { v.nil? ? nil : format("%.1f", v) }
|
||||
{
|
||||
"v" => 1, "id" => id, "aircraft_id" => aircraft_id, "date" => date&.iso8601, "log_type" => log_type, "entry_type" => entry_type,
|
||||
"text" => text.to_s, "airframe_hours" => num.(aircraft_hours_at_entry), "location" => signoff_location.to_s,
|
||||
"signer" => [ signoff_person.to_s, signoff_certificate_type.to_s, signoff_certificate_number.to_s ],
|
||||
"inspection_result" => inspection_result.to_s, "supersedes_id" => supersedes_id,
|
||||
"component" => component_id && [ component_id, num.(component_hours_at_entry), num.(component_since_overhaul_at_entry) ],
|
||||
"parts" => parts.reject(&:marked_for_destruction?).map { [ _1.component_id, num.(_1.hours_at_entry), num.(_1.since_overhaul_at_entry) ] }.sort_by { _1.first.to_i }
|
||||
}
|
||||
end
|
||||
|
||||
def content_digest = OpenSSL::Digest::SHA256.hexdigest(content_payload.to_json)
|
||||
|
||||
# Readable copy of what was signed, kept with the signature.
|
||||
def content_snapshot
|
||||
c = component
|
||||
{
|
||||
"aircraft" => { "tail_number" => aircraft.tail_number, "make" => aircraft.make, "model" => aircraft.model, "serial_number" => aircraft.serial_number },
|
||||
"date" => date&.iso8601, "log" => LOG_TYPES[log_type], "entry_type" => ENTRY_TYPES[entry_type], "text" => text,
|
||||
"airframe_total_time" => aircraft_hours_at_entry&.to_s, "time_basis" => aircraft.time_basis, "location" => signoff_location,
|
||||
"inspection_result" => inspection_result_label,
|
||||
"component" => c && { "type" => c.component_type&.name, "make" => c.manufacturer, "model" => c.model, "serial_number" => c.serial_number,
|
||||
"total_time" => component_hours_at_entry&.to_s, "since_overhaul" => component_since_overhaul_at_entry&.to_s,
|
||||
"since_label" => c.since_overhaul_abbreviation },
|
||||
"parts" => parts.map { |p| { "type" => p.component.component_type&.name, "model" => p.component.model, "serial_number" => p.component.serial_number,
|
||||
"total_time" => p.hours_at_entry&.to_s, "since" => p.since_overhaul_at_entry&.to_s } },
|
||||
"supersedes" => supersedes && { "date" => supersedes.date&.iso8601, "fingerprint" => supersedes.signature&.content_digest }
|
||||
}
|
||||
end
|
||||
|
||||
# "an annual" / "a 100-hour": from a linked inspection record, else the text.
|
||||
def inspection_kind_phrase
|
||||
name = inspection_records.first&.inspection_type&.name.to_s.sub(/\s*\(.*\)\z/, "").sub(/\s*inspection\z/i, "").strip.downcase
|
||||
name = "annual" if name.empty? && text.to_s.match?(/annual/i)
|
||||
name = "100-hour" if name.empty? && text.to_s.match?(/100[- ]?h(ou)?r/i)
|
||||
return "a ______________" if name.empty?
|
||||
"#{name.match?(/\A[aeiou]/) ? 'an' : 'a'} #{name}"
|
||||
end
|
||||
|
||||
# What the signer certifies, by the kind of entry and certificate.
|
||||
def certification_statement(certificate_kind)
|
||||
if entry_type == "INSPECTION"
|
||||
if inspection_result == "discrepancies"
|
||||
"I certify that this aircraft has been inspected in accordance with #{inspection_kind_phrase} inspection and a list of discrepancies " \
|
||||
"and unairworthy items dated #{date&.strftime('%b %-d, %Y')} has been provided for the aircraft owner or operator."
|
||||
else
|
||||
"I certify that this aircraft has been inspected in accordance with #{inspection_kind_phrase} inspection and was determined to be in airworthy condition."
|
||||
end
|
||||
elsif certificate_kind == "PILOT"
|
||||
"I certify that the preventive maintenance described in this entry was performed in accordance with 14 CFR Part 43, and I approve it for return to service."
|
||||
elsif entry_type == "MAINTENANCE"
|
||||
"I certify that the work described in this entry was performed in accordance with 14 CFR Part 43, and I approve it for return to service with respect to that work."
|
||||
else
|
||||
"I certify that this entry is correct."
|
||||
end
|
||||
end
|
||||
|
||||
# Why this certificate can't sign this entry ([] if it can).
|
||||
def signing_problems(certificate)
|
||||
problems = []
|
||||
problems << "it's already signed" if signed?
|
||||
problems << "it has been superseded by a correction" if superseded?
|
||||
problems << "it has no description" if text.blank?
|
||||
problems << "it has no date" if date.blank?
|
||||
if entry_type == "INSPECTION"
|
||||
problems << "an inspection entry needs its result (airworthy, or discrepancy list)" if inspection_result.blank?
|
||||
problems << "an inspection entry needs the aircraft's total time" if aircraft_hours_at_entry.blank?
|
||||
end
|
||||
if certificate
|
||||
problems << "that certificate has expired" if certificate.expired?
|
||||
problems << "a pilot certificate can sign preventive maintenance only" if certificate.kind == "PILOT" && entry_type != "MAINTENANCE"
|
||||
problems << "\"Other\" certificates can't sign maintenance or inspections" if certificate.kind == "OTHER" && RECORD_TYPES.include?(entry_type)
|
||||
else
|
||||
problems << "choose a certificate to sign with"
|
||||
end
|
||||
problems
|
||||
end
|
||||
|
||||
# From the form: { component_id => { "include" => "1", "hours" => …, "since" => … } }.
|
||||
# Only the aircraft's sub-components of the entry's component are kept.
|
||||
def assign_parts(params, aircraft)
|
||||
@@ -79,4 +175,14 @@ class LogbookEntry < ApplicationRecord
|
||||
end
|
||||
missing
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def keep_signed_content
|
||||
return unless signed? && signature.persisted?
|
||||
locked = changed - UNSIGNED_FIELDS
|
||||
return if locked.empty?
|
||||
errors.add(:base, "This entry is signed, so it can't be changed. Make a correction entry instead")
|
||||
throw :abort
|
||||
end
|
||||
end
|
||||
Reference in new issue
Block a user