DEV: Add `user` modifier to prevent updating ip_address (#28280)

This commit is contained in:
Isaac Janzen 2024-08-08 13:06:08 -05:00 committed by GitHub
parent 7c5e3eacda
commit aeaae9babc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 22 additions and 0 deletions

View File

@ -1038,6 +1038,10 @@ class User < ActiveRecord::Base
end
def self.update_ip_address!(user_id, new_ip:, old_ip:)
can_update_ip_address =
DiscoursePluginRegistry.apply_modifier(:user_can_update_ip_address, user_id: user_id)
return if !can_update_ip_address
unless old_ip == new_ip || new_ip.blank?
DB.exec(<<~SQL, user_id: user_id, ip_address: new_ip)
UPDATE users

View File

@ -3165,6 +3165,11 @@ RSpec.describe User do
end
describe "#update_ip_address!" do
let!(:plugin) { Plugin::Instance.new }
let!(:modifier) { :user_can_update_ip_address }
let!(:deny_block) { Proc.new { false } }
let!(:allow_block) { Proc.new { true } }
it "updates ip_address correctly" do
expect do user.update_ip_address!("127.0.0.1") end.to change {
user.reload.ip_address.to_s
@ -3173,6 +3178,19 @@ RSpec.describe User do
expect do user.update_ip_address!("127.0.0.1") end.to_not change { user.reload.ip_address }
end
it "allows plugins to control updating ip_address" do
DiscoursePluginRegistry.register_modifier(plugin, modifier, &deny_block)
expect do user.update_ip_address!("127.0.0.1") end.to_not change { user.reload.ip_address }
DiscoursePluginRegistry.register_modifier(plugin, modifier, &allow_block)
expect do user.update_ip_address!("127.0.0.1") end.to change {
user.reload.ip_address.to_s
}.to("127.0.0.1")
ensure
DiscoursePluginRegistry.unregister_modifier(plugin, modifier, &deny_block)
DiscoursePluginRegistry.unregister_modifier(plugin, modifier, &allow_block)
end
describe "keeping old ip address" do
before { SiteSetting.keep_old_ip_address_count = 2 }