Invitation codes: a link that gives an account and roles

Ported from the original (InvitationCode), adapted to configurable roles.

- Admin → Invitations: a label, optionally one email address (only it can
  use the link, and it's emailed) and a name, how many uses (or
  unlimited), an expiry (or never), and a role on any aircraft (or none,
  for an account only).
- An aircraft's Access page: whoever manages its people invites to that
  aircraft, with only the roles they can give; they see and revoke its
  invitations.
- /join/<code>: shows what it gives; create an account (when password
  sign-in is on) or sign in, then accept. Existing accounts and SSO users
  accept too. A role someone already has on an aircraft stays.
- Roles are given as the invitation's creator (AircraftAccess), and only
  while they can still manage that aircraft's people. Codes are kept as
  digests and shown once; uses are counted under a lock; account creation
  and accepting are rate-limited; any invalid link gets the same answer.
- Docs: guide (Administration, Access & sharing), README, and why /join
  isn't on the Authentik bypass list. The README's not-yet-ported list
  now names Prometheus metrics, and no longer claims oil analysis import
  as new.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
rickyandClaude Opus 5.5 committed 2026-11-15 18:50:58 -05:00
1 parent a645778f26
commit b4c7aaa3ff
34 files changed
+822 -7

No files matched your search

+74
View File
@@ -0,0 +1,74 @@
# An invitation code: a link someone opens to create an account (or accept
# with the one they have) and get roles on aircraft. Made by administrators
# (Admin → Invitations, any aircraft) or by whoever manages an aircraft's
# people (its Access page, that aircraft). Limited by number of uses, an
# expiry, and optionally to one email address; it can be revoked. Only a
# digest of the code is kept, so the link is shown once, when it's made.
class AccessInvitation < ApplicationRecord
VALID_FOR = 14.days
belongs_to :created_by, class_name: "User", optional: true
has_many :grants, class_name: "AccessInvitationRole", dependent: :delete_all
has_many :redemptions, class_name: "AccessInvitationRedemption", dependent: :delete_all
has_many :aircraft, through: :grants
attr_reader :token
normalizes :email, with: ->(email) { email.strip.downcase.presence }
normalizes :label, :name, with: ->(text) { text.squish.presence }
validates :label, presence: true, length: { maximum: 100 }
validates :email, format: { with: URI::MailTo::EMAIL_REGEXP }, allow_nil: true
validates :max_uses, numericality: { only_integer: true, greater_than: 0, less_than_or_equal_to: 1000 }, allow_nil: true
before_validation(on: :create) do
@token = SecureRandom.urlsafe_base64(24)
self.token_digest = self.class.digest(@token)
end
scope :recent_first, -> { order(created_at: :desc) }
scope :usable, -> {
where(revoked_at: nil).where("expires_at IS NULL OR expires_at > ?", Time.current).where("max_uses IS NULL OR uses_count < max_uses")
}
# A new invitation by creator giving roles ([[aircraft, role], …]); its
# link is emailed when it's for one address and email is set up.
def self.create_for(creator, attributes, grants)
invitation = new(attributes.merge(created_by: creator))
grants.each { |aircraft, role| invitation.grants.build(aircraft: aircraft, role: role) }
if invitation.save && invitation.email && MailSettings.configured?
AccessInvitationMailer.invite(invitation, invitation.token).deliver_later
end
invitation
end
def self.digest(token) = OpenSSL::Digest::SHA256.hexdigest(token.to_s)
def self.find_by_token(token) = token.present? ? find_by(token_digest: digest(token)) : nil
def usable? = !revoked? && !expired? && !used_up?
def revoked? = revoked_at.present?
def expired? = expires_at.present? && expires_at <= Time.current
def used_up? = max_uses.present? && uses_count >= max_uses
def for?(user) = email.nil? || email == user.email_address
def redeemed_by?(user) = redemptions.exists?(user: user)
# Gives the person the invitation's roles, as if its creator added them
# (AircraftAccess: so it can't give more than they could, then or now).
# A role they already have on an aircraft stays as it is. Returns
# [[aircraft, role or nil, message or nil], …].
def redeem!(user)
with_lock do
raise ActiveRecord::RecordInvalid, self unless usable? && for?(user)
redemptions.create!(user: user)
increment!(:uses_count)
grants.includes(:aircraft, :role).map do |grant|
if (existing = grant.aircraft.roles.find_by(user: user))
[ grant.aircraft, nil, I18n.t("models.access_invitation.already_has", role: existing.role.name) ]
elsif created_by.nil? || created_by.disabled? || !created_by.can?(:manage_people, grant.aircraft)
[ grant.aircraft, nil, I18n.t("models.access_invitation.creator_gone") ]
else
error = AircraftAccess.new(grant.aircraft, actor: created_by).add(user, grant.role)
[ grant.aircraft, (grant.role unless error), error ]
end
end
end
end
end
@@ -0,0 +1,7 @@
# Who used an invitation, and when (once per person).
class AccessInvitationRedemption < ApplicationRecord
belongs_to :access_invitation
belongs_to :user
validates :user_id, uniqueness: { scope: :access_invitation_id }
end
+8
View File
@@ -0,0 +1,8 @@
# A role an invitation gives on one aircraft.
class AccessInvitationRole < ApplicationRecord
belongs_to :access_invitation
belongs_to :aircraft
belongs_to :role
validates :aircraft_id, uniqueness: { scope: :access_invitation_id }
end
+1
View File
@@ -9,6 +9,7 @@ class Aircraft < ApplicationRecord
end
has_many :roles, class_name: "AircraftRole", dependent: :destroy
has_many :invitation_grants, class_name: "AccessInvitationRole", dependent: :delete_all
has_many :users, through: :roles
has_many :notes, class_name: "AircraftNote", dependent: :destroy
has_many :events, class_name: "AircraftEvent", dependent: :delete_all
+1
View File
@@ -7,6 +7,7 @@ class Role < ApplicationRecord
BUILT_IN = %w[owner mechanic pilot viewer].freeze
has_many :aircraft_roles, dependent: :restrict_with_error
has_many :invitation_grants, class_name: "AccessInvitationRole", dependent: :delete_all
normalizes :name, with: ->(name) { name.squish }
validates :name, presence: true, uniqueness: { case_sensitive: false }, length: { maximum: 40 }
+2
View File
@@ -2,6 +2,8 @@ class User < ApplicationRecord
has_secure_password
has_many :sessions, dependent: :destroy
has_many :aircraft_roles, dependent: :destroy
has_many :access_invitations, foreign_key: :created_by_id, dependent: :nullify, inverse_of: :created_by
has_many :access_invitation_redemptions, dependent: :delete_all
has_many :push_subscriptions, dependent: :delete_all
has_many :api_tokens, dependent: :destroy