mirror of
https://github.com/rickbarrette/simple-aircraft-manager-rails.git
synced 2026-11-17 15: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
@@ -1,6 +1,6 @@
|
||||
class Admin::UsersController < ApplicationController
|
||||
before_action :require_admin!
|
||||
before_action :set_user, only: %i[edit update destroy]
|
||||
before_action :set_user, only: %i[edit update destroy disable enable]
|
||||
|
||||
def index
|
||||
@users = User.order(:email_address).includes(:certificates, aircraft_roles: :aircraft)
|
||||
@@ -37,8 +37,25 @@ class Admin::UsersController < ApplicationController
|
||||
end
|
||||
end
|
||||
|
||||
# Ends their access and their ability to sign (AC 120-78B 2.2.6.4) while
|
||||
# keeping their account, so the signatures they made stay attributed.
|
||||
def disable
|
||||
return redirect_to admin_users_path, alert: "You can't disable yourself." if @user == current_user
|
||||
@user.update!(disabled_at: Time.current)
|
||||
@user.sessions.destroy_all
|
||||
redirect_to admin_users_path, notice: "#{@user.email_address} disabled: signed out, can't sign in or sign entries. Their past signatures are kept."
|
||||
end
|
||||
|
||||
def enable
|
||||
@user.update!(disabled_at: nil)
|
||||
redirect_to admin_users_path, notice: "#{@user.email_address} enabled."
|
||||
end
|
||||
|
||||
def destroy
|
||||
return redirect_to admin_users_path, alert: "You can't delete yourself." if @user == current_user
|
||||
if @user.entry_signatures.exists?
|
||||
return redirect_to admin_users_path, alert: "#{@user.email_address} has signed logbook entries; disable the account instead of deleting it."
|
||||
end
|
||||
@user.destroy!
|
||||
redirect_to admin_users_path, notice: "User deleted."
|
||||
end
|
||||
|
||||
@@ -31,7 +31,7 @@ module Authentication
|
||||
def find_session_by_cookie
|
||||
session = Session.find_by(id: cookies.signed[:session_id]) if cookies.signed[:session_id]
|
||||
return unless session
|
||||
if session.updated_at < SESSION_IDLE_TIMEOUT.ago
|
||||
if session.updated_at < SESSION_IDLE_TIMEOUT.ago || session.user.disabled?
|
||||
session.destroy
|
||||
cookies.delete(:session_id)
|
||||
return
|
||||
@@ -50,6 +50,7 @@ module Authentication
|
||||
end
|
||||
|
||||
def start_new_session_for(user)
|
||||
raise ArgumentError, "disabled account" if user.disabled?
|
||||
user.update_column(:last_signed_in_at, Time.current)
|
||||
user.sessions.create!(user_agent: request.user_agent, ip_address: request.remote_ip).tap do |session|
|
||||
Current.session = session
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
# The signing steps shared by aircraft users and invited signers.
|
||||
module EntrySigning
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
private
|
||||
|
||||
def signing_certificates = current_user.certificates.reject(&:expired?)
|
||||
|
||||
def prepare_signing(entry)
|
||||
@certificates = signing_certificates
|
||||
session[:after_reauth] = signing_page_path # where single sign-on returns after signing in again
|
||||
end
|
||||
|
||||
# Password re-entered now, or single sign-on completed in the last few minutes.
|
||||
def reauthenticated_method
|
||||
if params[:password].present?
|
||||
return "password" if current_user.authenticate_password(params[:password])
|
||||
elsif current_user.sso_uid? && (at = session[:reauthenticated_at]) && Time.zone.at(at) > EntrySigner::REAUTH_WINDOW.ago
|
||||
return "sso"
|
||||
end
|
||||
nil
|
||||
end
|
||||
|
||||
def sign_entry(entry)
|
||||
certificate = current_user.certificates.find_by(id: params[:certificate_id])
|
||||
method = reauthenticated_method
|
||||
unless params[:confirm] == "1"
|
||||
return signing_failed(entry, "Tick the box to confirm you're signing this entry.")
|
||||
end
|
||||
return signing_failed(entry, "Re-enter your password (or sign in again with #{Sso.label}) to sign.") unless method
|
||||
|
||||
signature = EntrySigner.new(entry, user: current_user, certificate: certificate, auth_method: method, request: request).sign!
|
||||
session.delete(:reauthenticated_at)
|
||||
SignatureMailer.signed(signature).deliver_later if MailSettings.configured?
|
||||
yield signature
|
||||
rescue EntrySigner::Error => e
|
||||
signing_failed(entry, e.message)
|
||||
end
|
||||
|
||||
def signing_failed(entry, message)
|
||||
prepare_signing(entry)
|
||||
@signing_error = message
|
||||
render :new, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,23 @@
|
||||
# Signing a logbook entry electronically (AC 120-78B chapter 2): the signer
|
||||
# reviews exactly what they're certifying, chooses their certificate, proves
|
||||
# who they are again (password, or single sign-on just now), and confirms.
|
||||
class EntrySignaturesController < ApplicationController
|
||||
include AircraftScoped
|
||||
include EntrySigning
|
||||
before_action :set_entry
|
||||
|
||||
def new
|
||||
prepare_signing(@entry)
|
||||
end
|
||||
|
||||
def create
|
||||
sign_entry(@entry) do |signature|
|
||||
redirect_to aircraft_logbook_entry_path(@aircraft, @entry), notice: "Signed. #{EntrySignature::LABEL} #{signature.signer_name}, #{signature.certificate_label} #{signature.certificate_number}."
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_entry = @entry = @aircraft.logbook_entries.find(params[:logbook_entry_id])
|
||||
def signing_page_path = new_aircraft_logbook_entry_signature_path(@aircraft, @entry)
|
||||
end
|
||||
@@ -0,0 +1,62 @@
|
||||
# The invited signer's side: sign in (or create an account for the invited
|
||||
# address), add a certificate if needed, review the entry and sign it. The
|
||||
# invitation gives access to that one entry only.
|
||||
class InvitedSigningsController < ApplicationController
|
||||
include EntrySigning
|
||||
allow_unauthenticated_access only: %i[show account]
|
||||
before_action :set_invitation
|
||||
rate_limit to: 10, within: 15.minutes, only: :account, with: -> { redirect_to new_session_path, alert: "Try again later." }
|
||||
|
||||
def show
|
||||
@entry = @invitation.logbook_entry
|
||||
@aircraft = @entry.aircraft
|
||||
unless authenticated?
|
||||
session[:return_to_after_authenticating] = request.url
|
||||
return render(:welcome)
|
||||
end
|
||||
return render(:wrong_account) unless current_user.email_address == @invitation.email
|
||||
return render(:signed) if @entry.signed?
|
||||
prepare_signing(@entry)
|
||||
render :new
|
||||
end
|
||||
|
||||
# Creating an account for the invited email address.
|
||||
def account
|
||||
return redirect_to(invited_signing_path(params[:token])) if authenticated?
|
||||
if User.exists?(email_address: @invitation.email) || !Sso.password_login?
|
||||
return redirect_to invited_signing_path(params[:token]), alert: "Sign in to continue."
|
||||
end
|
||||
user = User.new(params.permit(:name, :password, :password_confirmation).merge(email_address: @invitation.email))
|
||||
if user.save
|
||||
reset_session
|
||||
start_new_session_for user
|
||||
redirect_to invited_signing_path(params[:token]), notice: "Account created. Add your certificate, then sign."
|
||||
else
|
||||
@entry = @invitation.logbook_entry
|
||||
@account_errors = user.errors.full_messages
|
||||
render :welcome, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
def create
|
||||
@entry = @invitation.logbook_entry
|
||||
@aircraft = @entry.aircraft
|
||||
return render(:wrong_account) unless current_user.email_address == @invitation.email
|
||||
sign_entry(@entry) do |signature|
|
||||
@invitation.update!(used_at: Time.current, user: current_user)
|
||||
redirect_to invited_signing_path(params[:token]), notice: "Signed. #{EntrySignature::LABEL} #{signature.signer_name}, #{signature.certificate_label} #{signature.certificate_number}."
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_invitation
|
||||
@invitation = SigningInvitation.find_usable(params[:token])
|
||||
# Afterwards, the signer can still see what they signed.
|
||||
@invitation ||= SigningInvitation.where.not(used_at: nil).find_by(token_digest: SigningInvitation.digest(params[:token]), user: current_user) if current_user
|
||||
render(:expired, layout: "public", status: :not_found) unless @invitation
|
||||
end
|
||||
|
||||
def current_user = Current.user || (resume_session && Current.user)
|
||||
def signing_page_path = invited_signing_path(params[:token])
|
||||
end
|
||||
@@ -2,11 +2,12 @@ class LogbookEntriesController < ApplicationController
|
||||
include AircraftScoped
|
||||
before_action :require_owner!, except: %i[index show]
|
||||
before_action :set_entry, only: %i[show edit update destroy verify]
|
||||
before_action :require_unsigned, only: %i[edit update destroy]
|
||||
|
||||
def index
|
||||
@query = params[:q]
|
||||
@log_type = params[:log_type].presence
|
||||
scope = @aircraft.logbook_entries.search(@query).recent_first.includes(:document)
|
||||
scope = @aircraft.logbook_entries.search(@query).recent_first.includes(:document, :signature)
|
||||
scope = scope.where(log_type: @log_type) if @log_type
|
||||
@unverified_only = params[:unverified] == "1"
|
||||
scope = scope.unverified if @unverified_only
|
||||
@@ -27,15 +28,17 @@ class LogbookEntriesController < ApplicationController
|
||||
|
||||
def new
|
||||
LogbookTemplate.ensure_defaults!
|
||||
@entry = @aircraft.logbook_entries.new(date: Date.current, aircraft_hours_at_entry: @aircraft.current_hours)
|
||||
@entry = (original = correctable(params[:supersedes])) ? correction_of(original) : @aircraft.logbook_entries.new(date: Date.current, aircraft_hours_at_entry: @aircraft.current_hours)
|
||||
end
|
||||
|
||||
def create
|
||||
@entry = @aircraft.logbook_entries.new(entry_params)
|
||||
@entry.supersedes = correctable(params.dig(:logbook_entry, :supersedes_id))
|
||||
@entry.assign_parts(parts_params, @aircraft)
|
||||
if @entry.save
|
||||
log_event("logbook", "Logbook entry added (#{@entry.date})")
|
||||
redirect_to aircraft_logbook_entries_path(@aircraft), notice: "Logbook entry added."
|
||||
log_event("logbook", "Logbook entry #{@entry.supersedes ? 'correction ' : ''}added (#{@entry.date})")
|
||||
redirect_to aircraft_logbook_entry_path(@aircraft, @entry),
|
||||
notice: @entry.supersedes ? "Correction saved. Sign it to supersede the original entry." : "Logbook entry added."
|
||||
else
|
||||
render :new, status: :unprocessable_entity
|
||||
end
|
||||
@@ -62,6 +65,29 @@ class LogbookEntriesController < ApplicationController
|
||||
|
||||
private
|
||||
|
||||
# Signed entries are locked (AC 120-78B 2.1.2.8): changes go in a correction.
|
||||
def require_unsigned
|
||||
return unless @entry.signed?
|
||||
redirect_to aircraft_logbook_entry_path(@aircraft, @entry), alert: "This entry is signed, so it can't be changed or deleted. Make a correction entry instead."
|
||||
end
|
||||
|
||||
# A signed, not yet superseded entry of this aircraft.
|
||||
def correctable(id)
|
||||
return if id.blank?
|
||||
entry = @aircraft.logbook_entries.find_by(id: id)
|
||||
entry if entry&.signed? && !entry.superseded?
|
||||
end
|
||||
|
||||
# A new entry starting from the signed one it corrects; signed afresh.
|
||||
def correction_of(original)
|
||||
copy = @aircraft.logbook_entries.new(original.attributes.slice(*%w[date log_type entry_type text aircraft_hours_at_entry signoff_location
|
||||
inspection_result component_id component_hours_at_entry
|
||||
component_since_overhaul_at_entry document_id page_number]))
|
||||
copy.supersedes = original
|
||||
original.parts.each { copy.parts.build(component_id: _1.component_id, hours_at_entry: _1.hours_at_entry, since_overhaul_at_entry: _1.since_overhaul_at_entry) }
|
||||
copy
|
||||
end
|
||||
|
||||
# Sub-components ticked on the form: { component_id => { include, hours, since } }.
|
||||
def parts_params
|
||||
params.fetch(:logbook_entry, {}).permit(parts: {})[:parts]&.to_h || {}
|
||||
|
||||
@@ -18,7 +18,7 @@ class LogbookPrintsController < ApplicationController
|
||||
|
||||
def set_entries
|
||||
ids = Array(params[:ids]).map(&:to_i).first(200)
|
||||
@entries = @aircraft.logbook_entries.where(id: ids).includes(:component, :parts, inspection_records: :inspection_type).order(:date, :id).to_a
|
||||
@entries = @aircraft.logbook_entries.where(id: ids).includes(:component, :parts, :signature, inspection_records: :inspection_type).order(:date, :id).to_a
|
||||
redirect_to aircraft_logbook_entries_path(@aircraft), alert: "Choose the entries to print." if @entries.empty?
|
||||
end
|
||||
end
|
||||
@@ -8,7 +8,9 @@ class SessionsController < ApplicationController
|
||||
def create
|
||||
return redirect_to new_session_path, alert: "Sign in with #{Sso.label}." unless Sso.password_login?
|
||||
|
||||
if user = User.authenticate_by(params.permit(:email_address, :password))
|
||||
if (user = User.authenticate_by(params.permit(:email_address, :password))) && user.disabled?
|
||||
redirect_to new_session_path, alert: "This account is disabled."
|
||||
elsif user
|
||||
return_to = after_authentication_url
|
||||
reset_session # fresh session id after signing in
|
||||
start_new_session_for user
|
||||
|
||||
@@ -14,7 +14,7 @@ class SignInLinksController < ApplicationController
|
||||
end
|
||||
|
||||
def create
|
||||
if (user = User.find_by(email_address: params[:email_address].to_s))
|
||||
if (user = User.active.find_by(email_address: params[:email_address].to_s))
|
||||
SessionsMailer.sign_in_link(user).deliver_later
|
||||
end
|
||||
# Same answer either way, so this can't be used to find accounts.
|
||||
@@ -25,6 +25,7 @@ class SignInLinksController < ApplicationController
|
||||
end
|
||||
|
||||
def consume
|
||||
return redirect_to new_session_path, alert: "This account is disabled." if @user.disabled?
|
||||
reset_session # fresh session id after signing in
|
||||
start_new_session_for @user # also spends the link
|
||||
redirect_to root_path, notice: "Signed in."
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
# An owner asks someone outside the aircraft (a mechanic) to sign an entry.
|
||||
class SigningInvitationsController < ApplicationController
|
||||
include AircraftScoped
|
||||
before_action :require_owner!
|
||||
before_action :set_entry
|
||||
rate_limit to: 10, within: 1.hour, only: :create, with: -> { redirect_back fallback_location: root_path, alert: "Too many invitations; try again later." }
|
||||
|
||||
def new
|
||||
@invitation = @entry.signing_invitations.new
|
||||
end
|
||||
|
||||
def create
|
||||
return redirect_to(aircraft_logbook_entry_path(@aircraft, @entry), alert: "This entry is already signed.") if @entry.signed?
|
||||
unless MailSettings.configured?
|
||||
return redirect_to aircraft_logbook_entry_path(@aircraft, @entry), alert: "Email isn't set up, so invitations can't be sent (Admin → About)."
|
||||
end
|
||||
@invitation = @entry.signing_invitations.new(params.require(:signing_invitation).permit(:email, :message).merge(invited_by: current_user))
|
||||
if @invitation.save
|
||||
SigningInvitationMailer.invite(@invitation, @invitation.token).deliver_later
|
||||
log_event("logbook", "Signature requested from #{@invitation.email} (#{@entry.date} entry)")
|
||||
redirect_to aircraft_logbook_entry_path(@aircraft, @entry), notice: "Invitation sent to #{@invitation.email}. The link works once, for #{SigningInvitation::VALID_FOR.inspect}."
|
||||
else
|
||||
render :new, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_entry = @entry = @aircraft.logbook_entries.find(params[:logbook_entry_id])
|
||||
end
|
||||
@@ -14,6 +14,7 @@ class SsoController < ApplicationController
|
||||
def callback
|
||||
auth = request.env["omniauth.auth"]
|
||||
return failure unless auth && auth.provider.to_s == Sso::PROVIDER.to_s && auth.uid.present?
|
||||
return reauthenticated(auth) if authenticated? && current_user.sso_uid.present? && current_user.sso_uid == auth.uid.to_s
|
||||
return link(auth) if authenticated?
|
||||
|
||||
user = User.find_by(sso_uid: auth.uid) || link_by_email(auth) || create_user(auth)
|
||||
@@ -23,6 +24,8 @@ class SsoController < ApplicationController
|
||||
"Ask an administrator to add you, or sign in with your password and link #{Sso.label} from your profile."
|
||||
end
|
||||
|
||||
return redirect_to new_session_path, alert: "This account is disabled." if user.disabled?
|
||||
|
||||
sync_admin(user, auth)
|
||||
return_to = session[:return_to_after_authenticating]
|
||||
reset_session # fresh session id after signing in
|
||||
@@ -55,6 +58,12 @@ class SsoController < ApplicationController
|
||||
raise ActiveRecord::RecordNotFound unless Sso.enabled?
|
||||
end
|
||||
|
||||
# Signed in again (e.g. to sign a logbook entry): note when, and go back.
|
||||
def reauthenticated(auth)
|
||||
session[:reauthenticated_at] = Time.current.to_i
|
||||
redirect_to session.delete(:after_reauth).presence || edit_profile_path, notice: "Signed in again with #{Sso.label}."
|
||||
end
|
||||
|
||||
def link(auth)
|
||||
owner = User.find_by(sso_uid: auth.uid)
|
||||
if owner && owner != current_user
|
||||
|
||||
@@ -4,10 +4,11 @@ class UserCertificatesController < ApplicationController
|
||||
|
||||
def create
|
||||
certificate = current_user.certificates.new(certificate_params)
|
||||
back = safe_return_to || edit_profile_path(anchor: "certificates")
|
||||
if certificate.save
|
||||
redirect_to edit_profile_path(anchor: "certificates"), notice: "#{certificate.kind_label} certificate added."
|
||||
redirect_to back, notice: "#{certificate.kind_label} certificate added."
|
||||
else
|
||||
redirect_to edit_profile_path(anchor: "certificates"), alert: "Certificate not added: #{certificate.errors.full_messages.to_sentence}."
|
||||
redirect_to back, alert: "Certificate not added: #{certificate.errors.full_messages.to_sentence}."
|
||||
end
|
||||
end
|
||||
|
||||
@@ -26,6 +27,9 @@ class UserCertificatesController < ApplicationController
|
||||
|
||||
private
|
||||
|
||||
# Only back to a signing invitation page (no open redirects).
|
||||
def safe_return_to = params[:return_to].to_s.match?(%r{\A/sign/[\w-]+\z}) ? params[:return_to] : nil
|
||||
|
||||
def set_certificate = @certificate = current_user.certificates.find(params[:id])
|
||||
|
||||
def certificate_params = params.require(:user_certificate).permit(:kind, :number, :expires_on)
|
||||
|
||||
Reference in new issue
Block a user