Remove CurrentUser.ip_addr.

Remove the `CurrentUser.ip_addr` global variable and replace it with
`request.remote_ip`. Before we had to track the current user's IP in a
global variable so that when we edited a post for example, we could pass
down the user's IP to the model and save it in the post_versions table.
Now that we now longer save IPs in version tables, we don't need a global
variable to get access to the current user's IP outside of controllers.
This commit is contained in:
evazion
2022-09-18 04:41:01 -05:00
parent d4da8499ce
commit 1d2bac7b95
41 changed files with 15 additions and 87 deletions

View File

@@ -39,7 +39,7 @@ class ApplicationController < ActionController::Base
before_action(only: action, if: if_proc) do
key = "#{controller_name}:#{action}"
rate_limiter = RateLimiter.build(action: key, rate: rate, burst: burst, user: CurrentUser.user, ip_addr: CurrentUser.ip_addr)
rate_limiter = RateLimiter.build(action: key, rate: rate, burst: burst, user: CurrentUser.user, ip_addr: request.remote_ip)
headers["X-Rate-Limit"] = rate_limiter.to_json
rate_limiter.limit!
end
@@ -101,7 +101,7 @@ class ApplicationController < ActionController::Base
rate: CurrentUser.user.api_regen_multiplier,
burst: 200,
user: CurrentUser.user,
ip_addr: CurrentUser.ip_addr,
ip_addr: request.remote_ip,
)
headers["X-Rate-Limit"] = rate_limiter.to_json
@@ -178,7 +178,6 @@ class ApplicationController < ActionController::Base
def reset_current_user
CurrentUser.user = nil
CurrentUser.ip_addr = nil
CurrentUser.safe_mode = false
end
@@ -215,7 +214,7 @@ class ApplicationController < ActionController::Base
end
def ip_ban_check
raise User::PrivilegeError if !request.get? && IpBan.hit!(:full, CurrentUser.ip_addr)
raise User::PrivilegeError if !request.get? && IpBan.hit!(:full, request.remote_ip)
end
def pundit_user

View File

@@ -43,7 +43,7 @@ class CommentsController < ApplicationController
end
def create
@comment = authorize Comment.new(creator: CurrentUser.user, creator_ip_addr: CurrentUser.ip_addr)
@comment = authorize Comment.new(creator: CurrentUser.user, creator_ip_addr: request.remote_ip)
@comment.update(permitted_attributes(@comment))
flash[:notice] = @comment.valid? ? "Comment posted" : @comment.errors.full_messages.join("; ")
respond_with(@comment) do |format|

View File

@@ -38,7 +38,7 @@ class DmailsController < ApplicationController
end
def create
@dmail = authorize(Dmail).create_split(from: CurrentUser.user, creator_ip_addr: CurrentUser.ip_addr, **permitted_attributes(Dmail))
@dmail = authorize(Dmail).create_split(from: CurrentUser.user, creator_ip_addr: request.remote_ip, **permitted_attributes(Dmail))
respond_with(@dmail)
end

View File

@@ -38,7 +38,7 @@ class ForumPostsController < ApplicationController
end
def create
@forum_post = authorize ForumPost.new(creator: CurrentUser.user, creator_ip_addr: CurrentUser.ip_addr, topic_id: params.dig(:forum_post, :topic_id))
@forum_post = authorize ForumPost.new(creator: CurrentUser.user, creator_ip_addr: request.remote_ip, topic_id: params.dig(:forum_post, :topic_id))
@forum_post.update(permitted_attributes(@forum_post))
page = @forum_post.topic.last_page if @forum_post.topic.last_page > 1

View File

@@ -58,7 +58,7 @@ class ForumTopicsController < ApplicationController
def create
@forum_topic = authorize ForumTopic.new(creator: CurrentUser.user, **permitted_attributes(ForumTopic))
@forum_topic.original_post.creator = CurrentUser.user
@forum_topic.original_post.creator_ip_addr = CurrentUser.ip_addr
@forum_topic.original_post.creator_ip_addr = request.remote_ip
@forum_topic.save
respond_with(@forum_topic)

View File

@@ -65,7 +65,7 @@ class UsersController < ApplicationController
user_verifier = UserVerifier.new(CurrentUser.user, request)
@user = authorize User.new(
last_ip_addr: CurrentUser.ip_addr,
last_ip_addr: request.remote_ip,
last_logged_in_at: Time.zone.now,
requires_verification: user_verifier.requires_verification?,
level: user_verifier.initial_level,

View File

@@ -11,7 +11,7 @@ class ApplicationJob < ActiveJob::Base
queue_with_priority 0
around_perform do |_job, block|
CurrentUser.scoped(User.system, "127.0.0.1") do
CurrentUser.scoped(User.system) do
ApplicationRecord.without_timeout do
Timeout.timeout(24.hours, JobTimeoutError) do
block.call

View File

@@ -184,7 +184,7 @@ class BulkUpdateRequestProcessor
# Process the bulk update request immediately.
def process!
CurrentUser.scoped(User.system, "127.0.0.1") do
CurrentUser.scoped(User.system) do
commands.map do |command, *args|
case command
when :create_alias

View File

@@ -66,7 +66,6 @@ class SessionLoader
# @see CurrentUser
def load
CurrentUser.user = User.anonymous
CurrentUser.ip_addr = request.remote_ip
if has_api_authentication?
load_session_for_api

View File

@@ -217,14 +217,13 @@ class ApplicationRecord < ActiveRecord::Base
end
current_user = CurrentUser.user
current_ip = CurrentUser.ip_addr
find_in_batches(batch_size: batch_size, error_on_ignore: true) do |batch|
Parallel.each(batch, in_processes: in_processes, in_threads: in_threads) do |record|
# XXX In threaded mode, the current user isn't inherited from the
# parent thread because the current user is a thread-local
# variable. Hence, we have to set it explicitly in the child thread.
CurrentUser.scoped(current_user, current_ip) do
CurrentUser.scoped(current_user) do
yield record
end
end

View File

@@ -24,7 +24,6 @@ FAVORITES = ENV.fetch("FAVORITES", POSTS * 5.0).to_i
DEFAULT_PASSWORD = ENV.fetch("DEFAULT_PASSWORD", "password")
CurrentUser.user = User.system
CurrentUser.ip_addr = "127.0.0.1"
def populate_users(n, password: DEFAULT_PASSWORD)
puts "*** Creating users ***"

View File

@@ -4,13 +4,11 @@ class CommentVotesControllerTest < ActionDispatch::IntegrationTest
context "A comment votes controller" do
setup do
CurrentUser.user = @user = create(:user, name: "cirno")
CurrentUser.ip_addr = "127.0.0.1"
@comment = create(:comment, creator: @user)
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end
context "index action" do

View File

@@ -8,12 +8,10 @@ class CommentsControllerTest < ActionDispatch::IntegrationTest
@post = create(:post, id: 100)
CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1"
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end
context "index action" do

View File

@@ -10,7 +10,6 @@ class DmailsControllerTest < ActionDispatch::IntegrationTest
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end
context "new action" do

View File

@@ -243,12 +243,10 @@ class PostsControllerTest < ActionDispatch::IntegrationTest
context "with a pool: search" do
setup do
CurrentUser.user = create(:user)
CurrentUser.ip_addr = "127.0.0.1"
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end
should "render for a pool: search" do
@@ -278,12 +276,10 @@ class PostsControllerTest < ActionDispatch::IntegrationTest
context "with a favgroup: search" do
setup do
CurrentUser.user = create(:user)
CurrentUser.ip_addr = "127.0.0.1"
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end
should "render for a favgroup: search" do

View File

@@ -28,18 +28,15 @@ class ApplicationRecordTest < ActiveSupport::TestCase
@user1 = create(:user)
@user2 = create(:user)
CurrentUser.scoped(@user1, "1.1.1.1") do
CurrentUser.scoped(@user1) do
Tag.parallel_each do |tag|
assert_equal(@user1, CurrentUser.user)
assert_equal("1.1.1.1", CurrentUser.ip_addr)
CurrentUser.scoped(@user2, "2.2.2.2") do
CurrentUser.scoped(@user2) do
assert_equal(@user2, CurrentUser.user)
assert_equal("2.2.2.2", CurrentUser.ip_addr)
end
assert_equal(@user1, CurrentUser.user)
assert_equal("1.1.1.1", CurrentUser.ip_addr)
end
end
end

View File

@@ -4,12 +4,10 @@ class ArtistCommentaryTest < ActiveSupport::TestCase
setup do
user = FactoryBot.create(:user)
CurrentUser.user = user
CurrentUser.ip_addr = "127.0.0.1"
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end
should "A post should not have more than one commentary" do

View File

@@ -17,12 +17,10 @@ class ArtistTest < ActiveSupport::TestCase
setup do
user = travel_to(1.month.ago) {FactoryBot.create(:user)}
CurrentUser.user = user
CurrentUser.ip_addr = "127.0.0.1"
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end
should "parse inactive urls" do

View File

@@ -8,12 +8,10 @@ class ArtistURLTest < ActiveSupport::TestCase
context "An artist url" do
setup do
CurrentUser.user = FactoryBot.create(:user)
CurrentUser.ip_addr = "127.0.0.1"
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end
should "allow urls to be marked as inactive" do

View File

@@ -6,13 +6,11 @@ class BanTest < ActiveSupport::TestCase
setup do
@banner = FactoryBot.create(:admin_user)
CurrentUser.user = @banner
CurrentUser.ip_addr = "127.0.0.1"
end
teardown do
@banner = nil
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end
should "set the is_banned flag on the user" do
@@ -54,7 +52,6 @@ class BanTest < ActiveSupport::TestCase
context "Searching for a ban" do
should "find a given ban" do
CurrentUser.user = FactoryBot.create(:admin_user)
CurrentUser.ip_addr = "127.0.0.1"
user = FactoryBot.create(:user)
ban = FactoryBot.create(:ban, user: user)
@@ -76,13 +73,11 @@ class BanTest < ActiveSupport::TestCase
setup do
@admin = FactoryBot.create(:admin_user)
CurrentUser.user = @admin
CurrentUser.ip_addr = "127.0.0.1"
@user = FactoryBot.create(:user)
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end
end
end

View File

@@ -12,12 +12,10 @@ class BulkUpdateRequestTest < ActiveSupport::TestCase
setup do
@admin = FactoryBot.create(:admin_user)
CurrentUser.user = @admin
CurrentUser.ip_addr = "127.0.0.1"
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end
should_eventually "parse tags with tag type prefixes inside the script" do

View File

@@ -5,12 +5,10 @@ class CommentTest < ActiveSupport::TestCase
setup do
user = FactoryBot.create(:user)
CurrentUser.user = user
CurrentUser.ip_addr = "127.0.0.1"
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end
context "that mentions a user" do

View File

@@ -3,7 +3,6 @@ require 'test_helper'
class CurrentUserTest < ActiveSupport::TestCase
setup do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end
context "The current user" do
@@ -11,14 +10,11 @@ class CurrentUserTest < ActiveSupport::TestCase
user = FactoryBot.create(:user)
assert_nil(CurrentUser.user)
assert_nil(CurrentUser.ip_addr)
CurrentUser.user = user
CurrentUser.ip_addr = "1.2.3.4"
assert_not_nil(CurrentUser.user)
assert_equal(user.id, CurrentUser.user.id)
assert_equal("1.2.3.4", CurrentUser.ip_addr)
end
end
@@ -27,7 +23,7 @@ class CurrentUserTest < ActiveSupport::TestCase
user1 = FactoryBot.create(:user)
user2 = FactoryBot.create(:user)
CurrentUser.user = user1
CurrentUser.scoped(user2, nil) do
CurrentUser.scoped(user2) do
assert_equal(user2.id, CurrentUser.user.id)
end
assert_equal(user1.id, CurrentUser.user.id)
@@ -38,7 +34,7 @@ class CurrentUserTest < ActiveSupport::TestCase
user2 = FactoryBot.create(:user)
CurrentUser.user = user1
assert_raises(RuntimeError) do
CurrentUser.scoped(user2, nil) do
CurrentUser.scoped(user2) do
assert_equal(user2.id, CurrentUser.user.id)
raise "ERROR"
end

View File

@@ -46,12 +46,10 @@ class DTextTest < ActiveSupport::TestCase
context "#format_text" do
setup do
CurrentUser.user = create(:user)
CurrentUser.ip_addr = "127.0.0.1"
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end
should "add tag types to wiki links" do

View File

@@ -5,13 +5,11 @@ class ForumPostTest < ActiveSupport::TestCase
setup do
@user = FactoryBot.create(:user)
CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1"
@topic = FactoryBot.create(:forum_topic)
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end
context "that mentions a user" do

View File

@@ -6,13 +6,11 @@ class ForumTopicTest < ActiveSupport::TestCase
travel_to Time.now
@user = FactoryBot.create(:user)
CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1"
@topic = create(:forum_topic, title: "xxx", creator: @user)
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end
context "#mark_as_read!" do

View File

@@ -6,13 +6,10 @@ class PoolTest < ActiveSupport::TestCase
@user = FactoryBot.create(:user)
CurrentUser.user = @user
end
CurrentUser.ip_addr = "127.0.0.1"
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end
context "Searching pools" do

View File

@@ -5,12 +5,10 @@ class PostDisapprovalTest < ActiveSupport::TestCase
setup do
@alice = FactoryBot.create(:moderator_user, name: "alice")
CurrentUser.user = @alice
CurrentUser.ip_addr = "127.0.0.1"
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end
context "a post disapproval" do

View File

@@ -15,12 +15,10 @@ class PostQueryBuilderTest < ActiveSupport::TestCase
setup do
CurrentUser.user = create(:user)
CurrentUser.ip_addr = "127.0.0.1"
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end
context "Searching:" do

View File

@@ -6,7 +6,6 @@ module PostSets
setup do
@user = FactoryBot.create(:user)
CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1"
@post_1 = FactoryBot.create(:post, :tag_string => "a")
@post_2 = FactoryBot.create(:post, :tag_string => "b")
@@ -15,7 +14,6 @@ module PostSets
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end
context "a set for page 2" do

View File

@@ -17,14 +17,12 @@ class PostTest < ActiveSupport::TestCase
@user = FactoryBot.create(:user)
end
CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1"
end
def teardown
super
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end
context "Deletion:" do

View File

@@ -7,12 +7,10 @@ class PostVersionTest < ActiveSupport::TestCase
@user = FactoryBot.create(:user)
end
CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1"
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end
context "that has multiple versions: " do

View File

@@ -4,7 +4,6 @@ class RelatedTagQueryTest < ActiveSupport::TestCase
setup do
user = FactoryBot.create(:user)
CurrentUser.user = user
CurrentUser.ip_addr = "127.0.0.1"
end
context "#other_wiki_pages" do

View File

@@ -5,7 +5,6 @@ class SavedSearchTest < ActiveSupport::TestCase
super
@user = FactoryBot.create(:user)
CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1"
@mock_redis = MockRedis.new
SavedSearch.stubs(:redis).returns(@mock_redis)
end
@@ -13,7 +12,6 @@ class SavedSearchTest < ActiveSupport::TestCase
def teardown
super
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end
context ".labels_for" do

View File

@@ -18,7 +18,6 @@ class SessionLoaderTest < ActiveSupport::TestCase
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
CurrentUser.safe_mode = nil
end

View File

@@ -227,7 +227,6 @@ module Sources
context "translating the tags" do
setup do
CurrentUser.user = FactoryBot.create(:user)
CurrentUser.ip_addr = "127.0.0.1"
tags = {
"comic" => "漫画",

View File

@@ -9,12 +9,10 @@ class TagAliasTest < ActiveSupport::TestCase
user = FactoryBot.create(:user)
CurrentUser.user = user
end
CurrentUser.ip_addr = "127.0.0.1"
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end
context "on validation" do

View File

@@ -5,12 +5,10 @@ class TagImplicationTest < ActiveSupport::TestCase
setup do
@admin = create(:admin_user)
CurrentUser.user = @admin
CurrentUser.ip_addr = "127.0.0.1"
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end
context "on validation" do

View File

@@ -4,12 +4,10 @@ class TagTest < ActiveSupport::TestCase
setup do
@builder = FactoryBot.create(:builder_user)
CurrentUser.user = @builder
CurrentUser.ip_addr = "127.0.0.1"
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end
context "A tag category fetcher" do

View File

@@ -18,12 +18,10 @@ class UserTest < ActiveSupport::TestCase
setup do
@user = FactoryBot.create(:user)
CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1"
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end
context "promoting a user" do

View File

@@ -1,13 +1,8 @@
require 'test_helper'
class WikiPageTest < ActiveSupport::TestCase
setup do
CurrentUser.ip_addr = "127.0.0.1"
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end
context "A wiki page" do