Files
simple-aircraft-manager-rails/app/models/signing_invitation.rb
T
rickyandClaude Opus 5.5 9a20c91f5e Owners can invite mechanics again; who adds aircraft is an admin setting
- Admin → Settings → Who can add aircraft: administrators only (the
  default) or everyone. Adding an aircraft or importing a .sam.zip makes
  you its Owner, so this decides who can become one.
- A signing invitation from an Owner of the aircraft (or an
  administrator) lets the outside mechanic create an account for the
  invited address again, with a password or by signing in with SSO;
  checked when it's used. Others' signing invitations still need an
  existing account, and say so.
- Docs: who makes accounts, the new setting.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 19:09:49 -05:00

34 lines
1.4 KiB
Ruby

# An invitation, by email, for someone outside the aircraft (a mechanic) to
# sign one logbook entry with their own account and certificate
# (AC 120-78B 3.2.8.11). The link works once and expires.
class SigningInvitation < ApplicationRecord
VALID_FOR = 14.days
belongs_to :logbook_entry
belongs_to :invited_by, class_name: "User", optional: true
belongs_to :user, optional: true
attr_reader :token
normalizes :email, with: ->(e) { e.strip.downcase }
validates :email, presence: true, format: { with: URI::MailTo::EMAIL_REGEXP }
before_validation(on: :create) do
@token = SecureRandom.urlsafe_base64(32)
self.token_digest = self.class.digest(@token)
self.expires_at ||= VALID_FOR.from_now
end
def self.digest(token) = OpenSSL::Digest::SHA256.hexdigest(token.to_s)
def self.find_usable(token) = where(token_digest: digest(token), used_at: nil).where("expires_at > ?", Time.current).first
def aircraft = logbook_entry.aircraft
# Whether the mechanic can make an account for the invited address with it:
# when it's from an Owner of the aircraft or an administrator (still, when
# it's used). Who can become an Owner is up to the administrator (Admin →
# Settings, who can add aircraft); others' invitations need an account.
def creates_account?
invited_by.present? && !invited_by.disabled? && (invited_by.admin? || invited_by.role_for(aircraft)&.owner?) || false
end
end