A platform for community discussion. Free, open, simple.
DEV: Require admin for allow_admin screened IP mutations (#38545)
Isaac Janzen1444702c84ac
diff --git a/app/controllers/admin/screened_ip_addresses_controller.rb b/app/controllers/admin/screened_ip_addresses_controller.rbindex 66f83123d47..f1a65b82c19 100644--- a/app/controllers/admin/screened_ip_addresses_controller.rb+++ b/app/controllers/admin/screened_ip_addresses_controller.rb@@ -3,6 +3,7 @@class Admin::ScreenedIpAddressesController < Admin::StaffControllerbefore_action :can_see_ipbefore_action :fetch_screened_ip_address, only: %i[update destroy]+ before_action :ensure_admin_for_allow_admin, only: %i[create update destroy]def indexfilter = params[:filter]@@ -54,6 +55,15 @@ class Admin::ScreenedIpAddressesController < Admin::StaffControllerraise Discourse::InvalidAccess.new if !guardian.can_see_ip?end+ def ensure_admin_for_allow_admin+ return if current_user.admin?++ if params[:action_name].to_s == "allow_admin" ||+ @screened_ip_address&.action_type == ScreenedIpAddress.actions[:allow_admin]+ raise Discourse::InvalidAccess+ end+ end+def allowed_paramsparams.require(:ip_address)permitted = params.permit(:ip_address, :action_name)diff --git a/spec/requests/admin/screened_ip_addresses_controller_spec.rb b/spec/requests/admin/screened_ip_addresses_controller_spec.rbindex d758200243e..9155026d61a 100644--- a/spec/requests/admin/screened_ip_addresses_controller_spec.rb+++ b/spec/requests/admin/screened_ip_addresses_controller_spec.rb@@ -81,6 +81,148 @@ RSpec.describe Admin::ScreenedIpAddressesController doexpect(response.status).to eq(400)end++ it "allows creating an allow_admin screened ip address" do+ post "/admin/logs/screened_ip_addresses.json",+ params: {+ ip_address: "203.0.113.0/24",+ action_name: "allow_admin",+ }++ expect(response.status).to eq(200)+ expect(+ ScreenedIpAddress.where(action_type: ScreenedIpAddress.actions[:allow_admin]).count,+ ).to eq(1)+ end+ end++ context "when logged in as a moderator" do+ before { sign_in(moderator) }++ it "prevents creating an allow_admin screened ip address" do+ post "/admin/logs/screened_ip_addresses.json",+ params: {+ ip_address: "203.0.113.0/24",+ action_name: "allow_admin",+ }++ expect(response.status).to eq(403)+ expect(+ ScreenedIpAddress.where(action_type: ScreenedIpAddress.actions[:allow_admin]).count,+ ).to eq(0)+ end++ it "allows creating a block screened ip address" do+ post "/admin/logs/screened_ip_addresses.json",+ params: {+ ip_address: "203.0.113.0/24",+ action_name: "block",+ }++ expect(response.status).to eq(200)+ end+ end+ end++ describe "#update" do+ context "when logged in as a moderator" do+ before { sign_in(moderator) }++ it "prevents updating a screened ip address to allow_admin" do+ screened_ip = Fabricate(:screened_ip_address, ip_address: "100.0.0.1", action_name: "block")++ put "/admin/logs/screened_ip_addresses/#{screened_ip.id}.json",+ params: {+ ip_address: "100.0.0.1",+ action_name: "allow_admin",+ }++ expect(response.status).to eq(403)+ expect(screened_ip.reload.action_type).to eq(ScreenedIpAddress.actions[:block])+ end++ it "prevents changing an allow_admin screened ip address to block" do+ screened_ip =+ Fabricate(:screened_ip_address, ip_address: "100.0.0.8", action_name: "allow_admin")++ put "/admin/logs/screened_ip_addresses/#{screened_ip.id}.json",+ params: {+ ip_address: "100.0.0.8",+ action_name: "block",+ }++ expect(response.status).to eq(403)+ expect(screened_ip.reload.action_type).to eq(ScreenedIpAddress.actions[:allow_admin])+ end++ it "allows updating a block screened ip address" do+ screened_ip = Fabricate(:screened_ip_address, ip_address: "100.0.0.9", action_name: "block")++ put "/admin/logs/screened_ip_addresses/#{screened_ip.id}.json",+ params: {+ ip_address: "100.0.0.10",+ action_name: "block",+ }++ expect(response.status).to eq(200)+ expect(screened_ip.reload.ip_address.to_s).to eq("100.0.0.10")+ end+ end++ context "when logged in as an admin" do+ before { sign_in(admin) }++ it "allows updating a screened ip address to allow_admin" do+ screened_ip = Fabricate(:screened_ip_address, ip_address: "100.0.0.1", action_name: "block")++ put "/admin/logs/screened_ip_addresses/#{screened_ip.id}.json",+ params: {+ ip_address: "100.0.0.1",+ action_name: "allow_admin",+ }++ expect(response.status).to eq(200)+ expect(screened_ip.reload.action_type).to eq(ScreenedIpAddress.actions[:allow_admin])+ end+ end+ end++ describe "#destroy" do+ context "when logged in as a moderator" do+ before { sign_in(moderator) }++ it "prevents deleting an allow_admin screened ip address" do+ screened_ip =+ Fabricate(:screened_ip_address, ip_address: "100.0.0.5", action_name: "allow_admin")++ delete "/admin/logs/screened_ip_addresses/#{screened_ip.id}.json"++ expect(response.status).to eq(403)+ expect(ScreenedIpAddress.find_by(id: screened_ip.id)).to be_present+ end++ it "allows deleting a block screened ip address" do+ screened_ip = Fabricate(:screened_ip_address, ip_address: "100.0.0.6", action_name: "block")++ delete "/admin/logs/screened_ip_addresses/#{screened_ip.id}.json"++ expect(response.status).to eq(200)+ expect(ScreenedIpAddress.find_by(id: screened_ip.id)).to be_nil+ end+ end++ context "when logged in as an admin" do+ before { sign_in(admin) }++ it "allows deleting an allow_admin screened ip address" do+ screened_ip =+ Fabricate(:screened_ip_address, ip_address: "100.0.0.7", action_name: "allow_admin")++ delete "/admin/logs/screened_ip_addresses/#{screened_ip.id}.json"++ expect(response.status).to eq(200)+ expect(ScreenedIpAddress.find_by(id: screened_ip.id)).to be_nil+ endendendend