Skip to content

Commit 7b7e51a

Browse files
committed
Manage server registrations
Allows admins to set server registration manually and at runtime to either * Open (default) * Closed * Invite Only The last one is new. If enabled, admins can manage invitation codes and registration is only possible when entering a valid code.
1 parent 725be4c commit 7b7e51a

33 files changed

+415
-18
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
</p>
2828
<% if admin_signed_in? %>
2929
<%= nav_link_to t(".users"), fasp_base.admin_users_path %>
30+
<% if registration_invite_only? %>
31+
<%= nav_link_to t(".invitation_codes"), fasp_base.admin_invitation_codes_path %>
32+
<% end %>
33+
<%= nav_link_to t(".settings"), fasp_base.admin_settings_path %>
3034
<% end %>
3135
</div>
3236
<div class="flex gap-8">

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
<% if signed_in? %>
3535
<%= nav_link_to t('.sign_out'), fasp_base.session_path, data: {turbo_method: :delete} %>
3636
<% else %>
37-
<% if FaspBase.registration_enabled? %>
37+
<% if registration_enabled? %>
3838
<%= nav_link_to t(".sign_up"), fasp_base.new_registration_path %>
3939
<% end %>
4040
<%= nav_link_to t(".sign_in"), fasp_base.new_session_path %>

debug_fasp/config/initializers/fasp_base.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
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
1312
end
1413

1514
Rails.application.config.to_prepare do
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# This migration comes from fasp_base (originally 20250508115122)
2+
class CreateFaspBaseInvitationCodes < ActiveRecord::Migration[8.0]
3+
def change
4+
create_table :fasp_base_invitation_codes do |t|
5+
t.string :code, null: false, index: { unique: true }
6+
7+
t.timestamps
8+
end
9+
end
10+
end
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# This migration comes from fasp_base (originally 20250508125745)
2+
class CreateFaspBaseSettings < ActiveRecord::Migration[8.0]
3+
def up
4+
create_table :fasp_base_settings do |t|
5+
t.string :name, null: false, index: { unique: true }
6+
t.string :value
7+
8+
t.timestamps
9+
end
10+
FaspBase::Setting.create!(name: "registration", value: "open")
11+
end
12+
13+
def down
14+
drop_table :fasp_base_settings
15+
end
16+
end

debug_fasp/db/schema.rb

Lines changed: 16 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,35 @@
11
module FaspBase
22
class Admin::InvitationCodesController < Admin::BaseController
3+
def index
4+
@invitation_codes = FaspBase::InvitationCode.order(created_at: :desc)
5+
6+
respond_to do |format|
7+
format.html
8+
format.json { render json: @invitation_codes }
9+
end
10+
end
11+
12+
def create
13+
@invitation_code = FaspBase::InvitationCode.create!
14+
15+
respond_to do |format|
16+
format.html do
17+
redirect_to fasp_base.admin_invitation_codes_path, notice: t(".success")
18+
end
19+
format.json { render json: @invitation_code, status: :created }
20+
end
21+
end
22+
23+
def destroy
24+
invitation_code = FaspBase::InvitationCode.find(params[:id])
25+
invitation_code.destroy
26+
27+
respond_to do |format|
28+
format.html do
29+
redirect_to fasp_base.admin_invitation_codes_path, notice: t(".success")
30+
end
31+
format.json { head :no_content }
32+
end
33+
end
334
end
435
end

fasp_base/app/controllers/fasp_base/admin/sessions_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def create
1919
def destroy
2020
reset_session
2121

22-
redirect_to fasp_base.admin_new_session_path,
22+
redirect_to fasp_base.new_admin_session_path,
2323
notice: t(".success")
2424
end
2525
end
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,36 @@
11
module FaspBase
22
class Admin::SettingsController < Admin::BaseController
3+
def index
4+
@registration = find_or_create_registration
5+
6+
respond_to do |format|
7+
format.html
8+
format.json { render json: [ @registration ] }
9+
end
10+
end
11+
12+
def update
13+
setting = Setting.find(params[:id])
14+
setting.update!(setting_params)
15+
16+
respond_to do |format|
17+
format.html do
18+
redirect_to fasp_base.admin_settings_path, notice: t(".success")
19+
end
20+
format.json { head :no_content }
21+
end
22+
end
23+
24+
private
25+
26+
def find_or_create_registration
27+
Setting
28+
.create_with(value: "open")
29+
.find_or_create_by(name: "registration")
30+
end
31+
32+
def setting_params
33+
params.expect(setting: [ :value ])
34+
end
335
end
436
end

fasp_base/app/controllers/fasp_base/registrations_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ def create
3131

3232
def registration_params
3333
params.require(:registration)
34-
.permit(:email, :base_url, :password, :password_confirmation)
34+
.permit(:email, :base_url, :password, :password_confirmation, :invitation_code)
3535
end
3636

3737
def registration_allowed
38-
head :not_found unless FaspBase.registration_enabled?
38+
head :not_found if FaspBase::Setting.get("registration").closed?
3939
end
4040
end
4141
end

0 commit comments

Comments
 (0)