refactor: use MULTI_TENANT env variable, model concern instead of controller (#accepting_signups?)

This commit is contained in:
Adam Haris
2025-12-10 15:14:22 +01:00
parent 64ca9e551e
commit eb8655dc8e
11 changed files with 23 additions and 26 deletions
+1 -1
View File
@@ -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.
+1 -1
View File
@@ -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
-12
View File
@@ -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
+1 -1
View File
@@ -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
+2 -2
View File
@@ -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 hasnt been set up yet. Please request an invitation from one of your coworkers."
redirect_to new_session_url unless Account.accepting_signups?
end
end
+1 -1
View File
@@ -1,5 +1,5 @@
class Account < ApplicationRecord
include Entropic, Seedeable
include Entropic, Seedeable, MultiTenant
has_one :join_code
has_many :users, dependent: :destroy
+9
View File
@@ -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
+1 -1
View File
@@ -4,7 +4,7 @@
<header>
<h1 class="txt-x-large font-weight-black margin-none"><%= @page_title %></h1>
<p class="margin-none-block-start txt-medium">
<% 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:
+3 -3
View File
@@ -10,10 +10,10 @@
</label>
</div>
<% if single_tenant? %>
<p>Enter your email and well get you signed in.</p>
<% else %>
<% if Account.accepting_signups? %>
<p><strong>New here?</strong> <%= link_to "Sign up", new_signup_path %> to create an account. <strong>Already have an account?</strong> Enter your email and well get you signed in.</p>
<% else %>
<p>Enter your email and well get you signed in.</p>
<% end %>
<button type="submit" id="log_in" class="btn btn--link center txt-medium">
+1 -1
View File
@@ -30,7 +30,7 @@ env:
clear:
MAILER_FROM_ADDRESS: support@example.com # The email "from" address that Fizzy sends email from
SMTP_ADDRESS: mail.example.com # The SMTP server you'll use to send email
SINGLE_TENANT: false # Set to true to enable single-tenant mode
MULTI_TENANT: false # Set to false to enable single-tenant mode
SOLID_QUEUE_IN_PUMA: true # Run background jobs in the app container
+3 -3
View File
@@ -65,8 +65,8 @@ class SignupsControllerTest < ActionDispatch::IntegrationTest
end
test "redirects to session#new when single_tenant and user exists" do
previous_single_tenant = ENV["SINGLE_TENANT"]
ENV["SINGLE_TENANT"] = "true"
previous_multi_tenant = ENV["MULTI_TENANT"]
ENV["MULTI_TENANT"] = "false"
users(:david)
@@ -76,6 +76,6 @@ class SignupsControllerTest < ActionDispatch::IntegrationTest
assert_redirected_to new_session_url
end
ensure
ENV["SINGLE_TENANT"] = previous_single_tenant
ENV["MULTI_TENANT"] = previous_multi_tenant
end
end