From eb8655dc8e22ce90314a350b5000f25185b6d78f Mon Sep 17 00:00:00 2001 From: Adam Haris Date: Wed, 10 Dec 2025 15:14:22 +0100 Subject: [PATCH] refactor: use MULTI_TENANT env variable, model concern instead of controller (#accepting_signups?) --- README.md | 2 +- app/controllers/application_controller.rb | 2 +- app/controllers/concerns/set_tenant.rb | 12 ------------ app/controllers/sessions_controller.rb | 2 +- app/controllers/signups_controller.rb | 4 ++-- app/models/account.rb | 2 +- app/models/concerns/multi_tenant.rb | 9 +++++++++ app/views/sessions/magic_links/show.html.erb | 2 +- app/views/sessions/new.html.erb | 6 +++--- config/deploy.yml | 2 +- test/controllers/signups_controller_test.rb | 6 +++--- 11 files changed, 23 insertions(+), 26 deletions(-) delete mode 100644 app/controllers/concerns/set_tenant.rb create mode 100644 app/models/concerns/multi_tenant.rb diff --git a/README.md b/README.md index 00b8e76ae..b1739b5e8 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ We've added comments to that file to highlight what each setting needs to be, bu - `proxy/ssl` and `proxy/host`: Kamal can set up SSL certificates for you automatically. To enable that, set the hostname again as `host`. If you don't want SSL for some reason, you can set `ssl: false` to turn it off. - `env/clear/MAILER_FROM_ADDRESS`: This is the email address that Fizzy will send emails from. It should usually be an address from the same domain where you're running Fizzy. - `env/clear/SMTP_ADDRESS`: The address of an SMTP server that you can send email through. You can use a 3rd-party service for this, like Sendgrid or Postmark, in which case their documentation will tell you what to use for this. -- `env/clear/SINGLE_TENANT`: Set to `true` to enable single-tenant mode (disable multi-account signups). +- `env/clear/MULTI_TENANT`: Set to `false` to enable single-tenant mode (disable multi-account signups). Fizzy also requires a few environment variables to be set up, some of which contain secrets. diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 70bbffeff..87c3ab8dd 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -2,7 +2,7 @@ class ApplicationController < ActionController::Base include Authentication include Authorization include BlockSearchEngineIndexing - include CurrentRequest, CurrentTimezone, SetPlatform, SetTenant + include CurrentRequest, CurrentTimezone, SetPlatform include RequestForgeryProtection include TurboFlash, ViewTransitions include RoutingHeaders diff --git a/app/controllers/concerns/set_tenant.rb b/app/controllers/concerns/set_tenant.rb deleted file mode 100644 index e4616e7bb..000000000 --- a/app/controllers/concerns/set_tenant.rb +++ /dev/null @@ -1,12 +0,0 @@ -module SetTenant - extend ActiveSupport::Concern - - included do - helper_method :single_tenant? - end - - private - def single_tenant? - ENV.fetch("SINGLE_TENANT", "false") == "true" - end -end diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index cc8f73d69..e32cf3829 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -14,7 +14,7 @@ class SessionsController < ApplicationController else signup = Signup.new(email_address: email_address) if signup.valid?(:identity_creation) - identity = signup.create_identity unless single_tenant? + identity = signup.create_identity if Account.accepting_signups? redirect_to_session_magic_link identity else head :unprocessable_entity diff --git a/app/controllers/signups_controller.rb b/app/controllers/signups_controller.rb index ec39063ce..b9e12d759 100644 --- a/app/controllers/signups_controller.rb +++ b/app/controllers/signups_controller.rb @@ -3,7 +3,7 @@ class SignupsController < ApplicationController allow_unauthenticated_access rate_limit to: 10, within: 3.minutes, only: :create, with: -> { redirect_to new_signup_path, alert: "Try again later." } before_action :redirect_authenticated_user - before_action :prevent_signup_when_users_exist, if: -> { single_tenant? && User.any? } + before_action :prevent_signup_when_users_exist layout "public" @@ -30,6 +30,6 @@ class SignupsController < ApplicationController end def prevent_signup_when_users_exist - redirect_to new_session_url, alert: "Your account hasn’t been set up yet. Please request an invitation from one of your coworkers." + redirect_to new_session_url unless Account.accepting_signups? end end diff --git a/app/models/account.rb b/app/models/account.rb index 0bb4d4111..bedfdc630 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -1,5 +1,5 @@ class Account < ApplicationRecord - include Entropic, Seedeable + include Entropic, Seedeable, MultiTenant has_one :join_code has_many :users, dependent: :destroy diff --git a/app/models/concerns/multi_tenant.rb b/app/models/concerns/multi_tenant.rb new file mode 100644 index 000000000..07d98025d --- /dev/null +++ b/app/models/concerns/multi_tenant.rb @@ -0,0 +1,9 @@ +module MultiTenant + extend ActiveSupport::Concern + + class_methods do + def accepting_signups? + ENV.fetch("MULTI_TENANT", "true") == "true" || Account.none? + end + end +end diff --git a/app/views/sessions/magic_links/show.html.erb b/app/views/sessions/magic_links/show.html.erb index 9a84172e3..e4c439803 100644 --- a/app/views/sessions/magic_links/show.html.erb +++ b/app/views/sessions/magic_links/show.html.erb @@ -4,7 +4,7 @@

<%= @page_title %>

- <% if single_tenant? %> + <% unless Account.accepting_signups? %> If your email belongs to an existing account. <% end %> Then enter the verification code included in the email below: diff --git a/app/views/sessions/new.html.erb b/app/views/sessions/new.html.erb index 643c9b66c..4e82c0b70 100644 --- a/app/views/sessions/new.html.erb +++ b/app/views/sessions/new.html.erb @@ -10,10 +10,10 @@ - <% if single_tenant? %> -

Enter your email and we’ll get you signed in.

- <% else %> + <% if Account.accepting_signups? %>

New here? <%= link_to "Sign up", new_signup_path %> to create an account. Already have an account? Enter your email and we’ll get you signed in.

+ <% else %> +

Enter your email and we’ll get you signed in.

<% end %>