Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Only admins can generate invite codes #1611

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions app/controllers/invite_codes_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class InviteCodesController < ApplicationController
before_action :ensure_self_hosted
before_action :ensure_admin, only: :create

def index
@invite_codes = InviteCode.all
Expand All @@ -15,4 +16,8 @@ def create
def ensure_self_hosted
redirect_to root_path unless self_hosted?
end

def ensure_admin
redirect_to settings_hosting_path, alert: "You are not allowed to generate invite codes" unless Current.user.admin?
end
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think since we're disabling the input in the UI, we should probably just throw an exception here. A user should not be able to trigger this POST request given the UI, so if they try to side-step that, they should receive an error I think.

end
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
22 changes: 22 additions & 0 deletions test/controllers/invite_codes_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
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_no_difference("InviteCode.count") do
post invite_codes_url, params: {}
end
end
end
Loading