Files
docuseal/app/controllers/setup_controller.rb
T

77 lines
2.3 KiB
Ruby
Raw Normal View History

2023-05-21 17:59:36 +03:00
# frozen_string_literal: true
class SetupController < ApplicationController
skip_before_action :maybe_redirect_to_setup
skip_before_action :authenticate_user!
2023-09-17 00:45:57 +03:00
skip_authorization_check
2023-05-21 17:59:36 +03:00
before_action :redirect_to_root_if_signed, if: :signed_in?
before_action :ensure_first_user_not_created!
def index
@account = Account.new(account_params)
@user = @account.users.new(user_params)
@encrypted_config = EncryptedConfig.new(account: @account, key: EncryptedConfig::APP_URL_KEY)
2023-05-21 17:59:36 +03:00
end
def create
@account = Account.new(account_params)
2023-06-30 01:26:13 +03:00
@account.timezone = Accounts.normalize_timezone(@account.timezone)
2023-05-21 17:59:36 +03:00
@user = @account.users.new(user_params)
2023-08-20 12:42:47 +03:00
@encrypted_config = EncryptedConfig.new(encrypted_config_params)
2023-05-21 17:59:36 +03:00
2023-07-04 23:05:22 +03:00
unless URI.parse(encrypted_config_params[:value].to_s).class.in?([URI::HTTP, URI::HTTPS])
2024-09-18 19:47:47 +03:00
@encrypted_config.errors.add(:value, I18n.t('should_be_a_valid_url'))
2023-07-04 23:05:22 +03:00
2025-08-14 09:25:35 +03:00
return render :index, status: :unprocessable_content
2023-07-04 23:05:22 +03:00
end
2025-08-14 09:25:35 +03:00
return render :index, status: :unprocessable_content unless @account.valid?
2023-08-20 12:42:47 +03:00
2023-05-21 17:59:36 +03:00
if @user.save
encrypted_configs = [
{ key: EncryptedConfig::APP_URL_KEY, value: encrypted_config_params[:value] },
2023-06-30 20:31:17 +03:00
{ key: EncryptedConfig::ESIGN_CERTS_KEY, value: GenerateCertificate.call.transform_values(&:to_pem) }
]
@account.encrypted_configs.create!(encrypted_configs)
2025-06-21 22:52:10 +03:00
@account.account_configs.create!(key: :fulltext_search, value: true) if SearchEntry.table_exists?
2023-05-28 23:20:48 +03:00
2023-06-30 01:26:13 +03:00
Docuseal.refresh_default_url_options!
2023-05-21 17:59:36 +03:00
sign_in(@user)
2023-07-01 13:43:46 +03:00
redirect_to newsletter_path
2023-05-21 17:59:36 +03:00
else
2025-08-14 09:25:35 +03:00
render :index, status: :unprocessable_content
2023-05-21 17:59:36 +03:00
end
end
private
def user_params
return {} unless params[:user]
params.require(:user).permit(:first_name, :last_name, :email, :password)
end
def account_params
return {} unless params[:account]
2024-09-28 01:21:35 +03:00
params.require(:account).permit(:name, :timezone, :locale)
2023-05-21 17:59:36 +03:00
end
def encrypted_config_params
return {} unless params[:encrypted_config]
params.require(:encrypted_config).permit(:value)
end
2023-05-21 17:59:36 +03:00
def redirect_to_root_if_signed
2024-09-18 19:47:47 +03:00
redirect_to root_path, notice: I18n.t('you_are_already_signed_in')
2023-05-21 17:59:36 +03:00
end
def ensure_first_user_not_created!
2024-09-18 19:47:47 +03:00
redirect_to new_user_session_path, notice: I18n.t('please_sign_in') if User.exists?
2023-05-21 17:59:36 +03:00
end
end