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

@@ -0,0 +1,32 @@
# Invitation codes for any aircraft, or none (an account only), by
# administrators (AccessInvitation).
class Admin::InvitationsController < ApplicationController
before_action :require_admin!
def index
@invitations = AccessInvitation.recent_first.includes(:created_by, grants: %i[aircraft role]).limit(200)
end
def new
@invitation = AccessInvitation.new(max_uses: 1, expires_at: AccessInvitation::VALID_FOR.from_now.change(sec: 0))
end
def create
roles = Role.all.index_by(&:id)
grants = Aircraft.where(id: params.fetch(:roles, {}).keys).filter_map do |aircraft|
role = roles[params[:roles][aircraft.id.to_s].to_i]
[ aircraft, role ] if role
end
@invitation = AccessInvitation.create_for(current_user, params.require(:access_invitation).permit(:label, :email, :name, :max_uses, :expires_at), grants)
if @invitation.persisted?
render :created
else
render :new, status: :unprocessable_entity
end
end
def destroy
AccessInvitation.find(params[:id]).update!(revoked_at: Time.current)
redirect_to admin_invitations_path, notice: t("flash.admin_invitations.revoked")
end
end
@@ -0,0 +1,34 @@
# Invitation codes for one aircraft (AccessInvitation), from its Access page,
# by whoever manages its people: only the roles they can give (AircraftAccess).
class AircraftInvitationsController < ApplicationController
include AircraftScoped
requires_permission :manage_people
def create
role = AircraftAccess.new(@aircraft, actor: current_user).assignable_roles.find { _1.id == params[:role_id].to_i }
return back(alert: t("services.aircraft_access.owner_only")) unless role
attributes = params.permit(:label, :email, :name, :max_uses, :expires_at).to_h.symbolize_keys
attributes[:label] = t("flash.aircraft_invitations.default_label", tail_number: @aircraft.tail_number) if attributes[:label].blank?
@invitation = AccessInvitation.create_for(current_user, attributes, [ [ @aircraft, role ] ])
return back(alert: @invitation.errors.full_messages.to_sentence) unless @invitation.persisted?
log_event("role", t("flash.aircraft_invitations.created_event", role: role.name), notes: @invitation.label)
render :created
end
# Only invitations for this aircraft alone; others are managed in Admin → Invitations.
def destroy
invitation = AccessInvitation.where(id: self.class.only_for(@aircraft)).find(params[:id])
invitation.update!(revoked_at: Time.current)
log_event("role", t("flash.aircraft_invitations.revoked_event"), notes: invitation.label)
back(notice: t("flash.aircraft_invitations.revoked"))
end
# Ids of the invitations that give a role on this aircraft and no other.
def self.only_for(aircraft)
AccessInvitationRole.group(:access_invitation_id).having("COUNT(*) = 1 AND MAX(aircraft_id) = ?", aircraft.id).select(:access_invitation_id)
end
private
def back(flash) = redirect_to(aircraft_aircraft_roles_path(@aircraft, anchor: "invitations"), flash)
end
@@ -11,6 +11,8 @@ class AircraftRolesController < ApplicationController
@access = AircraftAccess.new(@aircraft, actor: current_user)
@roles = @aircraft.roles.includes(:user, :role).joins(:role).order("roles.position", :created_at)
@share_tokens = @aircraft.share_tokens.order(created_at: :desc).includes(:created_by)
@invitations = AccessInvitation.where(id: AircraftInvitationsController.only_for(@aircraft), revoked_at: nil)
.recent_first.includes(:created_by, grants: :role).limit(20)
end
def create
+71
View File
@@ -0,0 +1,71 @@
# Opening an invitation link (AccessInvitation): see what it gives, then
# create an account (when sign-in by password is on) or sign in, and accept.
# An invitation for one email address only works for that address. An
# invalid link (unknown, expired, used up, revoked) gets one answer for all.
class JoinsController < ApplicationController
allow_unauthenticated_access only: %i[show account]
before_action :set_invitation
rate_limit to: 10, within: 15.minutes, only: %i[account accept],
with: -> { redirect_to join_path(params[:token]), alert: t("flash.joins.try_again_later") }
def show
unless current_user
session[:return_to_after_authenticating] = request.url
return render(:welcome)
end
return redirect_to(root_path, notice: t("flash.joins.already_accepted")) if @invitation.redeemed_by?(current_user)
return render(:wrong_account) unless @invitation.for?(current_user)
render :accept
end
# A new account, then the invitation accepted with it.
def account
return redirect_to(join_path(params[:token])) if current_user
return redirect_to(join_path(params[:token]), alert: t("flash.joins.sign_in_to_continue")) unless Sso.password_login?
email = @invitation.email || params[:email_address].to_s.strip.downcase
if User.exists?(email_address: email)
return redirect_to join_path(params[:token]), alert: t("flash.joins.account_exists")
end
user = User.new(params.permit(:name, :password, :password_confirmation).merge(email_address: email))
results = nil
User.transaction do
raise ActiveRecord::Rollback unless user.save
results = @invitation.redeem!(user)
end
if results
reset_session
start_new_session_for user
redirect_to root_path, notice: summary(results, t("flash.joins.welcome"))
else
@account_errors = user.errors.full_messages
render :welcome, status: :unprocessable_entity
end
rescue ActiveRecord::RecordInvalid
render :invalid, layout: "public", status: :not_found
end
def accept
return redirect_to(root_path, notice: t("flash.joins.already_accepted")) if @invitation.redeemed_by?(current_user)
return render(:wrong_account) unless @invitation.for?(current_user)
redirect_to root_path, notice: summary(@invitation.redeem!(current_user), t("flash.joins.accepted"))
rescue ActiveRecord::RecordInvalid
render :invalid, layout: "public", status: :not_found
end
private
def set_invitation
@invitation = AccessInvitation.find_by_token(params[:token])
render :invalid, layout: "public", status: :not_found unless @invitation&.usable?
end
# "Invitation accepted. N12345: Mechanic. N54321: already Owner, unchanged."
def summary(results, lead)
parts = results.map do |aircraft, role, message|
"#{aircraft.tail_number}: #{[ role&.name, message ].compact.join(', ')}"
end
[ lead, *parts ].join(" ")
end
def current_user = Current.user || (resume_session && Current.user)
end