The personal finance app for everyone (by everyone)
Ruby
Add onboarding state selector for self-hosted signup (#251)
Juan José Mata72e7d7736bed
diff --git a/.env.example b/.env.exampleindex 23b85ed721..0a0d75aec7 100644--- a/.env.example+++ b/.env.example@@ -10,6 +10,9 @@# Enables self hosting features (should be set to true unless you know what you're doing)SELF_HOSTED=true+# Controls onboarding flow (valid: open, closed, invite_only)+ONBOARDING_STATE=open+# Secret key used to encrypt credentials (https://api.rubyonrails.org/v7.1.3.2/classes/Rails/Application.html#method-i-secret_key_base)# Has to be a random string, generated eg. by running `openssl rand -hex 64`SECRET_KEY_BASE=secret-valuediff --git a/.env.local.example b/.env.local.exampleindex 830a73906a..7aacba4a29 100644--- a/.env.local.example+++ b/.env.local.example@@ -1,6 +1,9 @@# To enable / disable self-hosting features.SELF_HOSTED = true+# Controls onboarding flow (valid: open, closed, invite_only)+ONBOARDING_STATE = open+# Enable Twelve market data (careful, this will use your API credits)TWELVE_DATA_API_KEY =diff --git a/.env.test.example b/.env.test.exampleindex 4c6c62cdad..57d67d0646 100644--- a/.env.test.example+++ b/.env.test.example@@ -1,5 +1,8 @@SELF_HOSTED=false+# Controls onboarding flow (valid: open, closed, invite_only)+ONBOARDING_STATE=open+# OpenID Connect for testsOIDC_ISSUER=OIDC_CLIENT_ID=@@ -21,4 +24,4 @@ OIDC_REDIRECT_URI=http://localhost:3000/auth/openid_connect/callbackCOVERAGE=false# Set to true to run test suite serially-DISABLE_PARALLELIZATION=false\ No newline at end of file+DISABLE_PARALLELIZATION=falsediff --git a/app/controllers/concerns/invitable.rb b/app/controllers/concerns/invitable.rbindex 5e12d2df99..a295e859fa 100644--- a/app/controllers/concerns/invitable.rb+++ b/app/controllers/concerns/invitable.rb@@ -8,7 +8,11 @@ module Invitableprivatedef invite_code_required?return false if @invitation.present?- self_hosted? ? Setting.require_invite_for_signup : ENV["REQUIRE_INVITE_CODE"] == "true"+ if self_hosted?+ Setting.onboarding_state == "invite_only"+ else+ ENV["REQUIRE_INVITE_CODE"] == "true"+ endenddef self_hosted?diff --git a/app/controllers/registrations_controller.rb b/app/controllers/registrations_controller.rbindex b57b508d9c..83287e708c 100644--- a/app/controllers/registrations_controller.rb+++ b/app/controllers/registrations_controller.rb@@ -3,6 +3,7 @@ class RegistrationsController < ApplicationControllerlayout "auth"+ before_action :ensure_signup_open, if: :self_hosted?before_action :set_user, only: :createbefore_action :set_invitationbefore_action :claim_invite_code, only: :create, if: :invite_code_required?@@ -79,4 +80,10 @@ class RegistrationsController < ApplicationControllerrender :new, status: :unprocessable_entityendend++ def ensure_signup_open+ return unless Setting.onboarding_state == "closed"++ redirect_to new_session_path, alert: t("registrations.closed")+ endenddiff --git a/app/controllers/settings/hostings_controller.rb b/app/controllers/settings/hostings_controller.rbindex c86a6dac0c..048a1c4634 100644--- a/app/controllers/settings/hostings_controller.rb+++ b/app/controllers/settings/hostings_controller.rb@@ -15,8 +15,9 @@ class Settings::HostingsController < ApplicationControllerenddef update- if hosting_params.key?(:require_invite_for_signup)- Setting.require_invite_for_signup = hosting_params[:require_invite_for_signup]+ if hosting_params.key?(:onboarding_state)+ onboarding_state = hosting_params[:onboarding_state].to_s+ Setting.onboarding_state = onboarding_stateendif hosting_params.key?(:require_email_confirmation)@@ -68,7 +69,7 @@ class Settings::HostingsController < ApplicationControllerprivatedef hosting_params- params.require(:setting).permit(:require_invite_for_signup, :require_email_confirmation, :brand_fetch_client_id, :twelve_data_api_key, :openai_access_token, :openai_uri_base, :openai_model)+ params.require(:setting).permit(:onboarding_state, :require_email_confirmation, :brand_fetch_client_id, :twelve_data_api_key, :openai_access_token, :openai_uri_base, :openai_model)enddef ensure_admindiff --git a/app/models/setting.rb b/app/models/setting.rbindex fbbe65256d..afffd70f2a 100644--- a/app/models/setting.rb+++ b/app/models/setting.rb@@ -10,9 +10,40 @@ class Setting < RailsSettings::Basefield :openai_model, type: :string, default: ENV["OPENAI_MODEL"]field :brand_fetch_client_id, type: :string, default: ENV["BRAND_FETCH_CLIENT_ID"]+ ONBOARDING_STATES = %w[open closed invite_only].freeze+ DEFAULT_ONBOARDING_STATE = begin+ env_value = ENV["ONBOARDING_STATE"].to_s.presence || "open"+ ONBOARDING_STATES.include?(env_value) ? env_value : "open"+ end++ field :onboarding_state, type: :string, default: DEFAULT_ONBOARDING_STATEfield :require_invite_for_signup, type: :boolean, default: falsefield :require_email_confirmation, type: :boolean, default: ENV.fetch("REQUIRE_EMAIL_CONFIRMATION", "true") == "true"+ def self.validate_onboarding_state!(state)+ return if ONBOARDING_STATES.include?(state)++ raise ValidationError, I18n.t("settings.hostings.update.invalid_onboarding_state")+ end++ class << self+ alias_method :raw_onboarding_state, :onboarding_state+ alias_method :raw_onboarding_state=, :onboarding_state=++ def onboarding_state+ value = raw_onboarding_state+ return "invite_only" if value.blank? && require_invite_for_signup++ value.presence || DEFAULT_ONBOARDING_STATE+ end++ def onboarding_state=(state)+ validate_onboarding_state!(state)+ self.require_invite_for_signup = state == "invite_only"+ self.raw_onboarding_state = state+ end+ end+# Validates OpenAI configuration requires model when custom URI base is setdef self.validate_openai_config!(uri_base: nil, model: nil)# Use provided values or current settingsdiff --git a/app/views/settings/hostings/_invite_code_settings.html.erb b/app/views/settings/hostings/_invite_code_settings.html.erbindex 3a89f214d0..14e4439e39 100644--- a/app/views/settings/hostings/_invite_code_settings.html.erb+++ b/app/views/settings/hostings/_invite_code_settings.html.erb@@ -9,7 +9,19 @@url: settings_hosting_path,method: :patch,data: { controller: "auto-submit-form", auto_submit_form_trigger_event_value: "change" } do |form| %>- <%= form.toggle :require_invite_for_signup, { data: { auto_submit_form_target: "auto" } } %>+ <div class="form-field w-fit">+ <%= form.select :onboarding_state,+ options_for_select(+ [+ [ t(".states.open"), "open" ],+ [ t(".states.closed"), "closed" ],+ [ t(".states.invite_only"), "invite_only" ]+ ],+ Setting.onboarding_state+ ),+ { label: false },+ { data: { auto_submit_form_target: "auto" } } %>+ </div><% end %></div>@@ -27,7 +39,7 @@<% end %></div>- <% if Setting.require_invite_for_signup %>+ <% if Setting.onboarding_state == "invite_only" %><div class="flex items-center justify-between mb-4"><div><span class="text-primary text-base font-medium"><%= t(".generated_tokens") %></span>diff --git a/config/locales/views/registrations/en.yml b/config/locales/views/registrations/en.ymlindex 4c02397cf3..86b1fb1926 100644--- a/config/locales/views/registrations/en.yml+++ b/config/locales/views/registrations/en.yml@@ -8,6 +8,7 @@ en:user:create: Continueregistrations:+ closed: Signups are currently closed.create:failure: There was a problem signing up.invalid_invite_code: Invalid invite code, please try again.diff --git a/config/locales/views/registrations/nb.yml b/config/locales/views/registrations/nb.ymlindex 1c462c0756..a915358cf7 100644--- a/config/locales/views/registrations/nb.yml+++ b/config/locales/views/registrations/nb.yml@@ -8,6 +8,7 @@ nb:user:create: Fortsettregistrations:+ closed: Registrering er midlertidig stengt.create:failure: Det oppsto et problem med registreringen.invalid_invite_code: Ugyldig invitasjonskode, vennligst prøv igjen.@@ -22,4 +23,4 @@ nb:welcome_body: For å komme i gang må du registrere deg for en ny konto. Du vilda kunne konfigurere flere innstillinger i appen.welcome_title: Velkommen til Self Hosted %{product_name}!- password_placeholder: Angi passordet ditt\ No newline at end of file+ password_placeholder: Angi passordet dittdiff --git a/config/locales/views/registrations/tr.yml b/config/locales/views/registrations/tr.ymlindex e22b57e0cf..dbc067a0e9 100644--- a/config/locales/views/registrations/tr.yml+++ b/config/locales/views/registrations/tr.yml@@ -8,6 +8,7 @@ tr:user:create: Devam Etregistrations:+ closed: Kayıtlar şu anda kapalı.create:failure: Kayıt olurken bir sorun oluştu.invalid_invite_code: Geçersiz davet kodu, lütfen tekrar deneyin.@@ -21,4 +22,4 @@ tr:title: Hesabınızı oluşturunwelcome_body: Başlamak için yeni bir hesap oluşturmalısınız. Daha sonra uygulama içinde ek ayarları yapılandırabileceksiniz.welcome_title: Self Hosted Maybe'ye Hoş Geldiniz!- password_placeholder: Şifrenizi girin\ No newline at end of file+ password_placeholder: Şifrenizi girindiff --git a/config/locales/views/settings/hostings/en.yml b/config/locales/views/settings/hostings/en.ymlindex 5be096d853..e769dfdcfa 100644--- a/config/locales/views/settings/hostings/en.yml+++ b/config/locales/views/settings/hostings/en.yml@@ -3,17 +3,20 @@ en:settings:hostings:invite_code_settings:- description: Every new user that joins your instance of %{product} can only do- so via an invite code+ description: Control how new people sign up for your instance of %{product}.email_confirmation_description: When enabled, users must confirm their emailaddress when changing it.email_confirmation_title: Require email confirmationgenerate_tokens: Generate new codegenerated_tokens: Generated codes- title: Require invite code for signup+ title: Onboarding+ states:+ open: Open+ closed: Closed+ invite_only: Invite-onlyshow:general: External Services- invites: Invite Codes+ invites: Onboardingtitle: Self-Hostingdanger_zone: Danger Zoneclear_cache: Clear data cache@@ -47,6 +50,7 @@ en:update:failure: Invalid setting valuesuccess: Settings updated+ invalid_onboarding_state: Invalid onboarding stateclear_cache:cache_cleared: Data cache has been cleared. This may take a few moments to complete.not_authorized: You are not authorized to perform this actiondiff --git a/config/locales/views/settings/hostings/nb.yml b/config/locales/views/settings/hostings/nb.ymlindex 2b59cd0f8d..923b89d297 100644--- a/config/locales/views/settings/hostings/nb.yml+++ b/config/locales/views/settings/hostings/nb.yml@@ -3,16 +3,20 @@ nb:settings:hostings:invite_code_settings:- description: Hver ny bruker som blir med i din instans av %{product_name} kan bare bli med via en invitasjonskode+ description: Kontroller hvordan nye personer registrerer seg for din instans av %{product}.email_confirmation_description: Når aktivert, må brukere bekrefte e-postadressensin når de endrer den.email_confirmation_title: Krev e-postbekreftelsegenerate_tokens: Generer ny kodegenerated_tokens: Genererte koder- title: Krev invitasjonskode for registrering+ title: Onboarding+ states:+ open: Åpen+ closed: Stengt+ invite_only: Kun invitasjonshow:general: Generelle innstillinger- invites: Invitasjonskoder+ invites: Onboardingtitle: Selvhostingdanger_zone: Fareområdeclear_cache: Tøm cache@@ -23,6 +27,7 @@ nb:update:failure: Ugyldig innstillingsverdisuccess: Innstillinger oppdatert+ invalid_onboarding_state: Ugyldig onboarding-modusclear_cache:cache_cleared: Cachen er tømt. Dette kan ta noen øyeblikk å fullføre.- not_authorized: Du er ikke autorisert til å utføre denne handlingen\ No newline at end of file+ not_authorized: Du er ikke autorisert til å utføre denne handlingendiff --git a/config/locales/views/settings/hostings/tr.yml b/config/locales/views/settings/hostings/tr.ymlindex df03b00c11..551fdc784b 100644--- a/config/locales/views/settings/hostings/tr.yml+++ b/config/locales/views/settings/hostings/tr.yml@@ -3,15 +3,19 @@ tr:settings:hostings:invite_code_settings:- description: Maybe uygulamanıza katılan her yeni kullanıcı yalnızca bir davet kodu ile katılabilir+ description: Yeni kullanıcıların %{product} örneğinize nasıl kaydolacağını kontrol edin.email_confirmation_description: Etkinleştirildiğinde, kullanıcılar e-posta adreslerini değiştirirken e-posta onayı yapmak zorundadır.email_confirmation_title: E-posta onayı gerektirgenerate_tokens: Yeni kod oluşturgenerated_tokens: Oluşturulan kodlar- title: Kayıt için davet kodu gerektir+ title: Onboarding+ states:+ open: Açık+ closed: Kapalı+ invite_only: Davet ileshow:general: Genel Ayarlar- invites: Davet Kodları+ invites: Onboardingtitle: Kendi Sunucunda Barındırmadanger_zone: Tehlikeli Bölgeclear_cache: Veri önbelleğini temizle@@ -29,6 +33,7 @@ tr:update:failure: Geçersiz ayar değerisuccess: Ayarlar güncellendi+ invalid_onboarding_state: Geçersiz onboarding durumuclear_cache:cache_cleared: Veri önbelleği temizlendi. Bu işlemin tamamlanması birkaç dakika sürebilir.- not_authorized: Bu işlemi gerçekleştirmek için yetkiniz yok\ No newline at end of file+ not_authorized: Bu işlemi gerçekleştirmek için yetkiniz yokdiff --git a/test/controllers/settings/hostings_controller_test.rb b/test/controllers/settings/hostings_controller_test.rbindex ecc380ffe2..a3c4be0baf 100644--- a/test/controllers/settings/hostings_controller_test.rb+++ b/test/controllers/settings/hostings_controller_test.rb@@ -26,7 +26,7 @@ class Settings::HostingsControllerTest < ActionDispatch::IntegrationTestget settings_hosting_urlassert_response :forbidden- patch settings_hosting_url, params: { setting: { require_invite_for_signup: true } }+ patch settings_hosting_url, params: { setting: { onboarding_state: "invite_only" } }assert_response :forbiddenendend@@ -48,6 +48,20 @@ class Settings::HostingsControllerTest < ActionDispatch::IntegrationTestendend+ test "can update onboarding state when self hosting is enabled" do+ with_self_hosting do+ patch settings_hosting_url, params: { setting: { onboarding_state: "invite_only" } }++ assert_equal "invite_only", Setting.onboarding_state+ assert Setting.require_invite_for_signup++ patch settings_hosting_url, params: { setting: { onboarding_state: "closed" } }++ assert_equal "closed", Setting.onboarding_state+ refute Setting.require_invite_for_signup+ end+ end+test "can update openai access token when self hosting is enabled" dowith_self_hosting dopatch settings_hosting_url, params: { setting: { openai_access_token: "token" } }diff --git a/test/system/settings_test.rb b/test/system/settings_test.rbindex 0271f129d4..deec326719 100644--- a/test/system/settings_test.rb+++ b/test/system/settings_test.rb@@ -44,7 +44,10 @@ class SettingsTest < ApplicationSystemTestCaseclick_link "Self-Hosting"assert_current_path settings_hosting_pathassert_selector "h1", text: "Self-Hosting"- check "setting[require_invite_for_signup]", allow_label_click: true+ find("select#setting_onboarding_state").select("Invite-only")+ within("select#setting_onboarding_state") do+ assert_selector "option[selected]", text: "Invite-only"+ endclick_button "Generate new code"assert_selector 'span[data-clipboard-target="source"]', visible: true, count: 1 # invite code copy widgetcopy_button = find('button[data-action="clipboard#copy"]', match: :first) # Find the first copy button (adjust if needed)