Skip to content

Commit

Permalink
fix: Only admins can generate invite codes (#1611)
Browse files Browse the repository at this point in the history
* fix: Only admins can generate invite codes

* fix: raise error if user is not an admin when creating invite codesss
  • Loading branch information
tonyvince authored Jan 24, 2025
1 parent 0476f25 commit 61321f6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions app/controllers/invite_codes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ def index
end

def create
raise StandardError, "You are not allowed to generate invite codes" unless Current.user.admin?
InviteCode.generate!
redirect_back_or_to invite_codes_path, notice: "Code generated"
end
Expand Down
2 changes: 1 addition & 1 deletion app/views/settings/hostings/_invite_code_settings.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<%= styled_form_with model: Setting.new, url: settings_hosting_path, method: :patch, data: { controller: "auto-submit-form", "auto-submit-form-trigger-event-value" => "blur" } do |form| %>
<div class="relative inline-block select-none">
<%= form.check_box :require_invite_for_signup, class: "sr-only peer", "data-auto-submit-form-target": "auto", "data-autosubmit-trigger-event": "input" %>
<%= form.check_box :require_invite_for_signup, class: "sr-only peer", "data-auto-submit-form-target": "auto", "data-autosubmit-trigger-event": "input", disabled: !Current.user.admin? %>
<%= form.label :require_invite_for_signup, "&nbsp;".html_safe, class: "maybe-switch" %>
</div>
<% end %>
Expand Down
20 changes: 20 additions & 0 deletions test/controllers/invite_codes_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require "test_helper"

class InviteCodesControllerTest < ActionDispatch::IntegrationTest
setup do
Rails.application.config.app_mode.stubs(:self_hosted?).returns(true)
end
test "admin can generate invite codes" do
sign_in users(:family_admin)

assert_difference("InviteCode.count") do
post invite_codes_url, params: {}
end
end

test "non-admin cannot generate invite codes" do
sign_in users(:family_member)

assert_raises(StandardError) { post invite_codes_url, params: {} }
end
end

0 comments on commit 61321f6

Please sign in to comment.