Files
simple-aircraft-manager-rails/app/controllers/aircraft_roles_controller.rb
T
rickyandClaude Opus 5.5 b4c7aaa3ff 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>
2026-11-15 18:50:58 -05:00

40 lines
1.8 KiB
Ruby

# Who can access the aircraft, with which role ("manage people"), and its
# public share links ("manage sharing", see ShareTokensController). The rules
# for changing roles are in AircraftAccess.
class AircraftRolesController < ApplicationController
include AircraftScoped
checks_permission_itself only: :index
requires_permission :manage_people, only: %i[create update destroy]
def index
return permission_denied unless can?(:manage_people) || can?(:manage_sharing) || can?(:export_data)
@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
user = User.find_by(email_address: params[:email_address].to_s.strip.downcase)
return back(alert: t("flash.aircraft_roles.no_user_with_that")) unless user
error = access.add(user, Role.find_by(id: params[:role_id]))
back(error ? { alert: error } : { notice: t("flash.aircraft_roles.added", display_name: user.display_name) })
end
def update
error = access.change(@aircraft.roles.find(params[:id]), Role.find_by(id: params[:role_id]))
back(error ? { alert: error } : { notice: t("flash.aircraft_roles.role_updated") })
end
def destroy
error = access.remove(@aircraft.roles.find(params[:id]))
back(error ? { alert: error } : { notice: t("flash.aircraft_roles.access_removed") })
end
private
def access = AircraftAccess.new(@aircraft, actor: current_user)
def back(flash) = redirect_to(aircraft_aircraft_roles_path(@aircraft), flash)
end