Files
simple-aircraft-manager-rails/app/controllers/admin/users_controller.rb
T
rickyandClaude Opus 5.5 d8cd462194 Configurable roles: Owner, Mechanic, Pilot, Viewer and custom roles
What people can do on an aircraft now comes from their role there, and
administrators set what each role allows (Admin → Roles).

- Roles are installation-wide sets of permissions (31, grouped as flying,
  squawks and notes, maintenance records, imports and exports, the
  aircraft, notifications). Seeing an aircraft's records needs none.
- Built in: Owner (always every permission, so an aircraft can always be
  managed), Mechanic (shop maintenance: logbook, signing, components,
  inspections, ADs, squawks, reminders), Pilot (what pilots could do
  before) and Viewer (view only). Administrators can change all but
  Owner's, and add their own; a role can be deleted once nobody has it.
- Existing owners and pilots keep exactly what they could do.
- Admin → Users → a user lists their role on every aircraft, to change,
  remove or add. On the aircraft's Access page, anyone who manages people
  hands out roles, but only an Owner can give or take away Owner, nobody
  changes their own (except administrators), and an aircraft always
  keeps an Owner (AircraftAccess).
- Controllers declare the permission each action needs; a request that
  changes something with none declared is refused (and raises in
  development and tests), and a test walks every route to check.
- Weekly reminders and overdue alerts go to roles that get reminders;
  the DRS reminder also needs Manage ADs. The API follows the person's
  role; its owners_only error is now not_permitted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 17:43:38 -05:00

82 lines
3.0 KiB
Ruby

class Admin::UsersController < ApplicationController
before_action :require_admin!
before_action :set_user, only: %i[edit update destroy disable enable unlock reset_two_factor]
def index
@users = User.order(:email_address).includes(:certificates, aircraft_roles: %i[aircraft role])
end
def new
@user = User.new
end
def create
@user = User.new(user_params)
@user.admin = admin_flag
if @user.save
redirect_to admin_users_path, notice: t("flash.admin_users.user_created", email_address: @user.email_address)
else
render :new, status: :unprocessable_entity
end
end
def edit; end
def update
attrs = user_params
attrs = attrs.except(:password, :password_confirmation) if attrs[:password].blank?
if @user == current_user && !admin_flag
return redirect_to admin_users_path, alert: t("flash.admin_users.you_can_t_remove")
end
@user.admin = admin_flag
if @user.update(attrs)
@user.sessions.where.not(id: Current.session.id).destroy_all if attrs.key?(:password) # new password signs them out
redirect_to admin_users_path, notice: t("flash.admin_users.user_updated")
else
render :edit, status: :unprocessable_entity
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 unlock
@user.unlock!
redirect_to admin_users_path, notice: t("flash.admin_users.unlocked", email_address: @user.email_address)
end
# For a user who lost their authenticator: they sign in with their password
# alone and can set it up again.
def reset_two_factor
@user.disable_otp!
Rails.logger.info("[auth] #{current_user.email_address} turned off two-factor for #{@user.email_address}")
redirect_to admin_users_path, notice: t("flash.admin_users.two_factor_turned_off", email_address: @user.email_address)
end
def disable
return redirect_to admin_users_path, alert: t("flash.admin_users.you_can_t_disable") if @user == current_user
@user.update!(disabled_at: Time.current)
@user.sessions.destroy_all
redirect_to admin_users_path, notice: t("flash.admin_users.disabled_signed_out_can", email_address: @user.email_address)
end
def enable
@user.update!(disabled_at: nil)
redirect_to admin_users_path, notice: t("flash.admin_users.enabled", email_address: @user.email_address)
end
def destroy
return redirect_to admin_users_path, alert: t("flash.admin_users.you_can_t_delete") if @user == current_user
if @user.entry_signatures.exists?
return redirect_to admin_users_path, alert: t("flash.admin_users.has_signed_logbook_entries", email_address: @user.email_address)
end
@user.destroy!
redirect_to admin_users_path, notice: t("flash.admin_users.user_deleted")
end
private
def set_user = @user = User.find(params[:id])
def user_params = params.require(:user).permit(:name, :email_address, :password, :password_confirmation)
def admin_flag = params.dig(:user, :admin) == "1"
end