From 7b0ae702e7811c8b41eb22129efcfaad52db9a55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Mon, 24 Nov 2014 19:48:54 +0100 Subject: [PATCH] FEATURE: log a new staff action when rolling up banned IP addresses --- .../admin/screened_ip_addresses_controller.rb | 9 ++++++--- app/models/user_history.rb | 6 ++++-- app/services/staff_action_logger.rb | 16 ++++++++++++---- .../screened_ip_addresses_controller_spec.rb | 5 +++-- 4 files changed, 25 insertions(+), 11 deletions(-) diff --git a/app/controllers/admin/screened_ip_addresses_controller.rb b/app/controllers/admin/screened_ip_addresses_controller.rb index e9c25aa40..ea77a8b98 100644 --- a/app/controllers/admin/screened_ip_addresses_controller.rb +++ b/app/controllers/admin/screened_ip_addresses_controller.rb @@ -45,11 +45,14 @@ class Admin::ScreenedIpAddressesController < Admin::AdminController action_type: ScreenedIpAddress.actions[:block], min_count: SiteSetting.min_ban_entries_for_roll_up).values.flatten + # 2 - log the call + StaffActionLogger.new(current_user).log_roll_up(subnets) unless subnets.blank? + subnets.each do |subnet| - # 2 - create subnet if not already exists + # 3 - create subnet if not already exists ScreenedIpAddress.new(ip_address: subnet).save unless ScreenedIpAddress.where(ip_address: subnet).first - # 3 - update stats + # 4 - update stats sql = <<-SQL UPDATE screened_ip_addresses SET match_count = sum_match_count, @@ -70,7 +73,7 @@ class Admin::ScreenedIpAddressesController < Admin::AdminController ScreenedIpAddress.exec_sql(sql, action_type: ScreenedIpAddress.actions[:block], ip_address: subnet) - # 4 - remove old matches + # 5 - remove old matches ScreenedIpAddress.where(action_type: ScreenedIpAddress.actions[:block]) .where("family(ip_address) = 4") .where("masklen(ip_address) = 32") diff --git a/app/models/user_history.rb b/app/models/user_history.rb index 954a66ac0..90aae0c0b 100644 --- a/app/models/user_history.rb +++ b/app/models/user_history.rb @@ -33,7 +33,8 @@ class UserHistory < ActiveRecord::Base :check_email, :delete_post, :delete_topic, - :impersonate) + :impersonate, + :roll_up) end # Staff actions is a subset of all actions, used to audit actions taken by staff users. @@ -50,7 +51,8 @@ class UserHistory < ActiveRecord::Base :check_email, :delete_post, :delete_topic, - :impersonate] + :impersonate, + :roll_up] end def self.staff_action_ids diff --git a/app/services/staff_action_logger.rb b/app/services/staff_action_logger.rb index dfd6e281c..7e0ca21da 100644 --- a/app/services/staff_action_logger.rb +++ b/app/services/staff_action_logger.rb @@ -1,5 +1,6 @@ # Responsible for logging the actions of admins and moderators. class StaffActionLogger + def initialize(admin) @admin = admin raise Discourse::InvalidParameters.new('admin is nil') unless @admin && @admin.is_a?(User) @@ -164,10 +165,17 @@ class StaffActionLogger })) end - private - - def params(opts) - { acting_user_id: @admin.id, context: opts[:context] } + def log_roll_up(subnets) + UserHistory.create(params(opts).merge({ + action: UserHistory.action[:roll_up], + details: subnets.join(", ") + })) end + private + + def params(opts) + { acting_user_id: @admin.id, context: opts[:context] } + end + end diff --git a/spec/controllers/admin/screened_ip_addresses_controller_spec.rb b/spec/controllers/admin/screened_ip_addresses_controller_spec.rb index ae56646f4..ddd1d1212 100644 --- a/spec/controllers/admin/screened_ip_addresses_controller_spec.rb +++ b/spec/controllers/admin/screened_ip_addresses_controller_spec.rb @@ -21,8 +21,6 @@ describe Admin::ScreenedIpAddressesController do describe 'roll_up' do it "works" do - SiteSetting.expects(:min_ban_entries_for_roll_up).returns(3) - Fabricate(:screened_ip_address, ip_address: "1.2.3.4", match_count: 1) Fabricate(:screened_ip_address, ip_address: "1.2.3.5", match_count: 1) Fabricate(:screened_ip_address, ip_address: "1.2.3.6", match_count: 1) @@ -30,6 +28,9 @@ describe Admin::ScreenedIpAddressesController do Fabricate(:screened_ip_address, ip_address: "42.42.42.4", match_count: 1) Fabricate(:screened_ip_address, ip_address: "42.42.42.5", match_count: 1) + StaffActionLogger.any_instance.expects(:log_roll_up) + SiteSetting.expects(:min_ban_entries_for_roll_up).returns(3) + xhr :post, :roll_up response.should be_success