Files
simple-aircraft-manager-rails/app/controllers/admin/users_controller.rb
T
rickyandClaude Opus 5.5 14316bb60f Sign-in hardening: lockout, stronger passwords, authenticator codes
- 10 failed sign-ins in a row (password, authenticator code, or password
  at signing) lock an account for 15 minutes from any address; the user
  is emailed; admins can unlock. Same message as a wrong password.
- Passwords: at least 12 characters (max 72), refused if found in Have I
  Been Pwned (k-anonymity range API; fails open; PWNED_PASSWORDS_CHECK=false)
- Optional TOTP (authenticator app) with 10 recovery codes for password
  and emailed-link sign-in, and at signing (auth method password_totp);
  codes accepted once; admins can reset; SSO sign-in unaffected
- Demo/test passwords updated; guide, README and procedures document

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

82 lines
2.9 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: :aircraft)
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: "User #{@user.email_address} created."
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: "You can't remove your own admin rights."
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: "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: "#{@user.email_address} unlocked."
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: "Two-factor turned off for #{@user.email_address}. Ask them to set it up again from their profile."
end
def disable
return redirect_to admin_users_path, alert: "You can't disable yourself." if @user == current_user
@user.update!(disabled_at: Time.current)
@user.sessions.destroy_all
redirect_to admin_users_path, notice: "#{@user.email_address} disabled: signed out, can't sign in or sign entries. Their past signatures are kept."
end
def enable
@user.update!(disabled_at: nil)
redirect_to admin_users_path, notice: "#{@user.email_address} enabled."
end
def destroy
return redirect_to admin_users_path, alert: "You can't delete yourself." if @user == current_user
if @user.entry_signatures.exists?
return redirect_to admin_users_path, alert: "#{@user.email_address} has signed logbook entries; disable the account instead of deleting it."
end
@user.destroy!
redirect_to admin_users_path, notice: "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