Files
James Smith 4ed82e2c44 skip first run setup checks on session controller
otherwise we can't actually create a user
2026-05-15 13:30:48 +01:00

69 lines
1.8 KiB
Ruby

# frozen_string_literal: true
class Users::SessionsController < Devise::SessionsController
rate_limit to: 10, within: 3.minutes, only: :create
before_action :random_delay, only: [:create]
skip_before_action :register_admin_user
skip_before_action :set_up_first_library
before_action :auto_login_single_user
# before_action :configure_sign_in_params, only: [:create]
# GET /resource/sign_in
def new
authorize :"users/sessions"
super
end
# POST /resource/sign_in
def create
authorize :"users/sessions"
super
end
# DELETE /resource/sign_out
def destroy
authorize :"users/sessions"
super
reset_session # Completely reset the session
end
protected
# The path used after sign in.
def after_sign_in_path_for(resource)
helpers.landing_page_path
end
# If you have extra params to permit, append them to the sanitizer.
# def configure_sign_in_params
# devise_parameter_sanitizer.permit(:sign_in, keys: [:attribute])
# end
def auto_login_single_user
return if ENV.fetch("FORCE_OIDC", nil) == "enabled"
# Autocreate an admin user if there isn't one
create_admin_user if User.with_role(:administrator).empty?
# If in single user mode, or on first run,
# automatically sign in with an admin account
if !SiteSettings.multiuser_enabled? || User.with_role(:administrator).first.first_use?
sign_in(:user, User.with_role(:administrator).first)
flash.discard
redirect_back_or_to helpers.landing_page_path, alert: nil
end
end
def create_admin_user
password = helpers.random_password
u = User.create!(
username: SecureRandom.hex(4),
email: "root@localhost",
password:,
password_confirmation: password,
reset_password_token: "first_use"
)
u.add_role :administrator
u
end
end