From 0ac30a5fc090abcb4e1582b790cf5849fbb5a1a3 Mon Sep 17 00:00:00 2001 From: Alex Turchyn Date: Tue, 20 Jun 2023 23:42:56 +0300 Subject: [PATCH] add the app url input on the first setup page --- app/controllers/application_controller.rb | 4 ++++ app/controllers/profile_controller.rb | 23 +++++++++++++++++++++++ app/controllers/setup_controller.rb | 14 ++++++++++++-- app/javascript/application.js | 2 ++ app/javascript/elements/set_origin_url.js | 7 +++++++ app/mailers/application_mailer.rb | 4 ++++ app/models/encrypted_config.rb | 1 + app/views/profile/index.html.erb | 9 +++++++++ app/views/setup/index.html.erb | 9 +++++++++ config/application.rb | 2 +- config/environments/development.rb | 4 ---- config/environments/production.rb | 4 ---- config/routes.rb | 1 + lib/docuseal.rb | 20 ++++++++++++++++++++ 14 files changed, 93 insertions(+), 11 deletions(-) create mode 100644 app/javascript/elements/set_origin_url.js create mode 100644 lib/docuseal.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 974ba177..aa0bd5d5 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -10,6 +10,10 @@ class ApplicationController < ActionController::Base :current_account, :svg_icon + def default_url_options + Docuseal.default_url_options + end + private def current_account diff --git a/app/controllers/profile_controller.rb b/app/controllers/profile_controller.rb index 0b90f5a7..033f12a8 100644 --- a/app/controllers/profile_controller.rb +++ b/app/controllers/profile_controller.rb @@ -1,6 +1,10 @@ # frozen_string_literal: true class ProfileController < ApplicationController + before_action :load_encrypted_config, only: %i[index update_app_url] + + def index; end + def update_contact if current_user.update(contact_params) redirect_to settings_profile_index_path, notice: 'Contact information successfully updated' @@ -18,8 +22,23 @@ class ProfileController < ApplicationController end end + def update_app_url + if @encrypted_config.update(app_url_params) + Docuseal.refresh_default_url_options! + + redirect_to settings_profile_index_path, notice: 'App URL successfully changed' + else + render :index, status: :unprocessable_entity + end + end + private + def load_encrypted_config + @encrypted_config = + EncryptedConfig.find_or_initialize_by(account: current_account, key: EncryptedConfig::APP_URL_KEY) + end + def contact_params params.require(:user).permit(:first_name, :last_name, :email, account_attributes: %i[name]) end @@ -27,4 +46,8 @@ class ProfileController < ApplicationController def password_params params.require(:user).permit(:password, :password_confirmation) end + + def app_url_params + params.require(:encrypted_config).permit(:value) + end end diff --git a/app/controllers/setup_controller.rb b/app/controllers/setup_controller.rb index 43817ef2..8fe0bbc2 100644 --- a/app/controllers/setup_controller.rb +++ b/app/controllers/setup_controller.rb @@ -10,6 +10,7 @@ class SetupController < ApplicationController def index @account = Account.new(account_params) @user = @account.users.new(user_params) + @encrypted_config = EncryptedConfig.new(account: @account, key: EncryptedConfig::APP_URL_KEY) end def create @@ -17,8 +18,11 @@ class SetupController < ApplicationController @user = @account.users.new(user_params) if @user.save - @account.encrypted_configs.create!(key: EncryptedConfig::ESIGN_CERTS_KEY, - value: GenerateCertificate.call) + encrypted_configs = [ + { key: EncryptedConfig::APP_URL_KEY, value: encrypted_config_params[:value] }, + { key: EncryptedConfig::ESIGN_CERTS_KEY, value: GenerateCertificate.call } + ] + @account.encrypted_configs.create!(encrypted_configs) sign_in(@user) @@ -42,6 +46,12 @@ class SetupController < ApplicationController params.require(:account).permit(:name) end + def encrypted_config_params + return {} unless params[:encrypted_config] + + params.require(:encrypted_config).permit(:value) + end + def redirect_to_root_if_signed redirect_to root_path, notice: 'You are already signed in' end diff --git a/app/javascript/application.js b/app/javascript/application.js index b9ee6f22..aca2d2c0 100644 --- a/app/javascript/application.js +++ b/app/javascript/application.js @@ -11,6 +11,7 @@ import ClipboardCopy from './elements/clipboard_copy' import TemplateBuilder from './template_builder/builder' import DynamicList from './elements/dynamic_list' import DownloadButton from './elements/download_button' +import SetOriginUrl from './elements/set_origin_url' document.addEventListener('turbo:before-cache', () => { window.flash?.remove() @@ -30,6 +31,7 @@ window.customElements.define('menu-active', MenuActive) window.customElements.define('clipboard-copy', ClipboardCopy) window.customElements.define('dynamic-list', DynamicList) window.customElements.define('download-button', DownloadButton) +window.customElements.define('set-origin-url', SetOriginUrl) window.customElements.define('template-builder', class extends HTMLElement { connectedCallback () { diff --git a/app/javascript/elements/set_origin_url.js b/app/javascript/elements/set_origin_url.js new file mode 100644 index 00000000..77b0758f --- /dev/null +++ b/app/javascript/elements/set_origin_url.js @@ -0,0 +1,7 @@ +export default class extends HTMLElement { + connectedCallback () { + if (this.dataset.inputId) { + document.getElementById(this.dataset.inputId).value = document.location.origin + } + } +} diff --git a/app/mailers/application_mailer.rb b/app/mailers/application_mailer.rb index 0962aa12..8631df97 100644 --- a/app/mailers/application_mailer.rb +++ b/app/mailers/application_mailer.rb @@ -5,4 +5,8 @@ class ApplicationMailer < ActionMailer::Base layout 'mailer' register_interceptor ActionMailerConfigsInterceptor + + def default_url_options + Docuseal.default_url_options + end end diff --git a/app/models/encrypted_config.rb b/app/models/encrypted_config.rb index 0f11dd10..e06e5f52 100644 --- a/app/models/encrypted_config.rb +++ b/app/models/encrypted_config.rb @@ -24,6 +24,7 @@ class EncryptedConfig < ApplicationRecord FILES_STORAGE_KEY = 'active_storage' EMAIL_SMTP_KEY = 'action_mailer_smtp' ESIGN_CERTS_KEY = 'esign_certs' + APP_URL_KEY = 'app_url' belongs_to :account diff --git a/app/views/profile/index.html.erb b/app/views/profile/index.html.erb index ec9244a7..b367a41e 100644 --- a/app/views/profile/index.html.erb +++ b/app/views/profile/index.html.erb @@ -44,6 +44,15 @@ <%= f.button button_title(title: 'Update', disabled_with: 'Updating'), class: 'base-button' %> <% end %> +

App URL

+ <%= form_for @encrypted_config, url: update_app_url_settings_profile_index_path, method: :patch, html: { autocomplete: 'off', class: 'space-y-4' } do |f| %> +
+ <%= f.text_field :value, required: true, class: 'base-input' %> +
+
+ <%= f.button button_title(title: 'Update', disabled_with: 'Updating'), class: 'base-button' %> +
+ <% end %>
diff --git a/app/views/setup/index.html.erb b/app/views/setup/index.html.erb index f49f6a74..f6341b66 100644 --- a/app/views/setup/index.html.erb +++ b/app/views/setup/index.html.erb @@ -30,6 +30,15 @@ <%= ff.password_field :password, required: true, placeholder: '************', class: 'base-input' %> <% end %> + <%= f.fields_for @encrypted_config do |ff| %> +
+ <% if @encrypted_config.value.blank? %> + + <% end %> + <%= ff.label :value, 'App URL', class: 'label' %> + <%= ff.text_field :value, required: true, class: 'base-input' %> +
+ <% end %>
<%= f.button button_title(title: 'Confirm', disabled_with: 'Processing'), class: 'base-button' %> diff --git a/config/application.rb b/config/application.rb index c33b39c7..cf26ca7c 100644 --- a/config/application.rb +++ b/config/application.rb @@ -11,7 +11,7 @@ require 'active_job/railtie' Bundler.require(*Rails.groups) -module Docuseal +module DocuSeal class Application < Rails::Application config.load_defaults 7.0 diff --git a/config/environments/development.rb b/config/environments/development.rb index e07ae1aa..5f7a43c4 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -75,10 +75,6 @@ Rails.application.configure do key_derivation_salt: 'dev key derivation salt' } - config.action_mailer.default_url_options = { host: 'localhost', port: ENV.fetch('PORT', 3000).to_i } - config.action_controller.default_url_options = config.action_mailer.default_url_options - routes.default_url_options = config.action_mailer.default_url_options - # Raises error for missing translations. # config.i18n.raise_on_missing_translations = true diff --git a/config/environments/production.rb b/config/environments/production.rb index 833c9047..bdda6d41 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -81,10 +81,6 @@ Rails.application.configure do key_derivation_salt: ENV.fetch('SECRET_KEY_BASE', nil) } - config.action_mailer.default_url_options = { host: ENV.fetch('HOST', nil), port: ENV.fetch('PORT', nil) } - config.action_controller.default_url_options = config.action_mailer.default_url_options - routes.default_url_options = config.action_mailer.default_url_options - # Do not dump schema after migrations. config.active_record.dump_schema_after_migration = false end diff --git a/config/routes.rb b/config/routes.rb index ba769ea4..5a7dbbb0 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -60,6 +60,7 @@ Rails.application.routes.draw do collection do patch :update_contact patch :update_password + patch :update_app_url end end end diff --git a/lib/docuseal.rb b/lib/docuseal.rb new file mode 100644 index 00000000..3ff4a043 --- /dev/null +++ b/lib/docuseal.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +module Docuseal + DEFAULT_APP_URL = 'http://localhost:3000' + + module_function + + def default_url_options + @default_url_options ||= begin + value = EncryptedConfig.find_by(key: EncryptedConfig::APP_URL_KEY)&.value + value ||= DEFAULT_APP_URL + url = Addressable::URI.parse(value) + { host: url.host, port: url.port, protocol: url.scheme } + end + end + + def refresh_default_url_options! + @default_url_options = nil + end +end