Skip to content

Commit f55c822

Browse files
committed
Allow to disable registration
As a default registration is open, which will not suit (most) public installations. This is a first step towards better handling of server registration, allowing for registration to be disabled altogether. Future work will be to allow for a more nuanced approach, maybe using invite codes or similar methods.
1 parent 75344e9 commit f55c822

File tree

7 files changed

+39
-2
lines changed

7 files changed

+39
-2
lines changed

debug_fasp/app/views/layouts/application.html.erb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@
3434
<% if signed_in? %>
3535
<%= nav_link_to t('.sign_out'), fasp_base.session_path, data: {turbo_method: :delete} %>
3636
<% else %>
37-
<%= nav_link_to t(".sign_up"), fasp_base.new_registration_path %>
37+
<% if FaspBase.registration_enabled? %>
38+
<%= nav_link_to t(".sign_up"), fasp_base.new_registration_path %>
39+
<% end %>
3840
<%= nav_link_to t(".sign_in"), fasp_base.new_session_path %>
3941
<% end %>
4042
</div>

debug_fasp/config/initializers/fasp_base.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
f.privacy_policy_language = ENV["PRIVACY_POLICY_LANGUAGE"]
1010
f.contact_email = ENV["CONTACT_EMAIL"]
1111
f.fediverse_account = ENV["FEDIVERSE_ACCOUNT"]
12+
f.registration_enabled = ENV["DISABLE_SERVER_REGISTRATION"].present? ? false : true
1213
end
1314

1415
Rails.application.config.to_prepare do

fasp_base/app/controllers/fasp_base/registrations_controller.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ module FaspBase
22
class RegistrationsController < ApplicationController
33
skip_authentication
44

5+
before_action :registration_allowed
6+
57
def new
68
@registration = Registration.new
79
end
@@ -31,5 +33,9 @@ def registration_params
3133
params.require(:registration)
3234
.permit(:email, :base_url, :password, :password_confirmation)
3335
end
36+
37+
def registration_allowed
38+
head :not_found unless FaspBase.registration_enabled?
39+
end
3440
end
3541
end

fasp_base/lib/fasp_base.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class Error < ::StandardError ; end
1313
mattr_accessor :capabilities, default: []
1414
mattr_accessor :contact_email
1515
mattr_accessor :fediverse_account
16+
mattr_accessor :registration_enabled, default: true
1617

1718
def self.base_url
1819
protocol = Rails.env.production? ? "https" : "http"
@@ -24,4 +25,6 @@ def self.supports?(capability, version:)
2425
supported_capability[:id] == capability && supported_capability[:version].start_with?(version.to_s)
2526
end
2627
end
28+
29+
def self.registration_enabled? = registration_enabled
2730
end

fasp_base/lib/generators/fasp_base/install/templates/initializers/fasp_base.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
# f.privacy_policy_language = ENV["PRIVACY_POLICY_LANGUAGE"]
1414
# f.contact_email = ENV["CONTACT_EMAIL"]
1515
# f.fediverse_account = ENV["FEDIVERSE_ACCOUNT"]
16+
17+
# Disable open registration
18+
# f.registration_enabled = false
1619
end
1720

1821
# If you plan to patch classes from the `fasp_base` engine

fasp_base/lib/generators/fasp_base/install/templates/layouts/application.html.erb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@
3333
<% if signed_in? %>
3434
<%= nav_link_to t('.sign_out'), fasp_base.session_path, data: {turbo_method: :delete} %>
3535
<% else %>
36-
<%= nav_link_to t(".sign_up"), fasp_base.new_registration_path %>
36+
<% if FaspBase.registration_enabled? %>
37+
<%= nav_link_to t(".sign_up"), fasp_base.new_registration_path %>
38+
<% end %>
3739
<%= nav_link_to t(".sign_in"), fasp_base.new_session_path %>
3840
<% end %>
3941
</div>

fasp_base/test/integration/registration_test.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,26 @@ class RegistrationTest < ActionDispatch::IntegrationTest
77
stub_fasp_registration
88
end
99

10+
test "disabled registration" do
11+
FaspBase.registration_enabled = false
12+
get fasp_base.new_registration_path
13+
14+
assert_response :not_found
15+
16+
post fasp_base.registration_path, params: {
17+
registration: {
18+
email: "user@example.com",
19+
password: "super_secret",
20+
password_confirmation: "super_secret",
21+
base_url: "https://fedi.example.com/fasp"
22+
}
23+
}
24+
25+
assert_response :not_found
26+
27+
FaspBase.registration_enabled = true
28+
end
29+
1030
test "requesting the registration form" do
1131
get fasp_base.new_registration_path
1232

0 commit comments

Comments
 (0)