# Handles the one-time web-based installation wizard. # # The wizard is only accessible when no admin account exists yet. Once an # admin has been created the controller redirects every request to the root # path, so it can never be used to overwrite an existing installation. class SetupController < ActionController::Base # Use the full application layout (header, flash, etc.) so the page looks # consistent with the rest of the site. layout "application" before_action :redirect_if_installed def new end def create email = params[:admin_email].to_s.strip password = params[:admin_password].to_s confirm = params[:admin_password_confirmation].to_s errors = [] errors << t(".email_blank") if email.blank? errors << t(".password_blank") if password.blank? errors << t(".password_mismatch") if password != confirm if errors.any? flash.now[:alert] = errors.join(" ") return render :new, status: :unprocessable_entity end user = User.new(email: email, password: password, status: :admin) user.skip_confirmation! unless user.save flash.now[:alert] = user.errors.full_messages.join(" ") return render :new, status: :unprocessable_entity end # Persist runtime settings chosen during setup. Setting.set("skip_email_confirmation", params[:skip_email_confirmation] == "1") # Optionally seed the built-in default units. if params[:seed_units] == "1" load Rails.root.join("db/seeds/units.rb") end redirect_to new_user_session_path, notice: t(".success") end private def redirect_if_installed redirect_to root_path if User.exists?(status: :admin) rescue ActiveRecord::StatementInvalid # Tables are not yet migrated — stay on the setup page so the user sees a # meaningful error rather than a crash. end end