Merge pull request #2992 from techAPJ/patch-4
FEATURE: export screened IPs list in a CSV file
This commit is contained in:
commit
727e87b78f
|
@ -22,5 +22,14 @@ Discourse.ExportCsv.reopenClass({
|
||||||
bootbox.alert(I18n.t("admin.export_csv.failed"));
|
bootbox.alert(I18n.t("admin.export_csv.failed"));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
Exports screened IPs list
|
||||||
|
|
||||||
|
@method export_screened_ips_list
|
||||||
|
**/
|
||||||
|
exportScreenedIpsList: function() {
|
||||||
|
return Discourse.ajax("/admin/export_csv/screened_ips.json");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -0,0 +1,15 @@
|
||||||
|
export default Discourse.Route.extend({
|
||||||
|
|
||||||
|
actions: {
|
||||||
|
exportScreenedIps: function() {
|
||||||
|
Discourse.ExportCsv.exportScreenedIpsList().then(function(result) {
|
||||||
|
if (result.success) {
|
||||||
|
bootbox.alert(I18n.t("admin.export_csv.success"));
|
||||||
|
} else {
|
||||||
|
bootbox.alert(I18n.t("admin.export_csv.failed"));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
|
@ -8,6 +8,9 @@
|
||||||
<li><a href="/logs" data-auto-route="true">{{i18n admin.logs.logster.title}}</a></li>
|
<li><a href="/logs" data-auto-route="true">{{i18n admin.logs.logster.title}}</a></li>
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
|
<div class="pull-right">
|
||||||
|
<button {{action "exportScreenedIps"}} class="btn" title="{{i18n admin.export_csv.screened_ips.title}}">{{fa-icon "download"}}{{i18n admin.export_csv.screened_ips.text}}</button>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="admin-container">
|
<div class="admin-container">
|
||||||
|
|
|
@ -8,6 +8,12 @@ class Admin::ExportCsvController < Admin::AdminController
|
||||||
render json: success_json
|
render json: success_json
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def export_screened_ips_list
|
||||||
|
# export csv file in a background thread
|
||||||
|
Jobs.enqueue(:export_csv_file, entity: 'screened_ips', user_id: current_user.id)
|
||||||
|
render json: success_json
|
||||||
|
end
|
||||||
|
|
||||||
# download
|
# download
|
||||||
def show
|
def show
|
||||||
filename = params.fetch(:id)
|
filename = params.fetch(:id)
|
||||||
|
|
|
@ -6,6 +6,7 @@ module Jobs
|
||||||
class ExportCsvFile < Jobs::Base
|
class ExportCsvFile < Jobs::Base
|
||||||
CSV_USER_ATTRS = ['id','name','username','email','title','created_at','trust_level','active','admin','moderator','ip_address']
|
CSV_USER_ATTRS = ['id','name','username','email','title','created_at','trust_level','active','admin','moderator','ip_address']
|
||||||
CSV_USER_STATS = ['topics_entered','posts_read_count','time_read','topic_count','post_count','likes_given','likes_received']
|
CSV_USER_STATS = ['topics_entered','posts_read_count','time_read','topic_count','post_count','likes_given','likes_received']
|
||||||
|
SCREENED_IP_ATTRS = ['ip_address','action_type','match_count','last_match_at','created_at']
|
||||||
|
|
||||||
sidekiq_options retry: false
|
sidekiq_options retry: false
|
||||||
attr_accessor :current_user
|
attr_accessor :current_user
|
||||||
|
@ -24,20 +25,26 @@ module Jobs
|
||||||
when 'user'
|
when 'user'
|
||||||
query = ::AdminUserIndexQuery.new
|
query = ::AdminUserIndexQuery.new
|
||||||
user_data = query.find_users_query.to_a
|
user_data = query.find_users_query.to_a
|
||||||
data = Array.new
|
data = []
|
||||||
|
|
||||||
user_data.each do |user|
|
user_data.each do |user|
|
||||||
user_array = Array.new
|
|
||||||
group_names = get_group_names(user).join(';')
|
group_names = get_group_names(user).join(';')
|
||||||
user_array = get_user_fields(user)
|
user_array = get_user_fields(user)
|
||||||
user_array.push(group_names) if group_names != ''
|
user_array.push(group_names) if group_names != ''
|
||||||
data.push(user_array)
|
data.push(user_array)
|
||||||
end
|
end
|
||||||
|
when 'screened_ips'
|
||||||
|
screened_ips_data = ScreenedIpAddress.order('id desc').to_a
|
||||||
|
data = []
|
||||||
|
screened_ips_data.each do |screened_ip|
|
||||||
|
screened_ip_array = get_screened_ip_fields(screened_ip)
|
||||||
|
data.push(screened_ip_array)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if data && data.length > 0
|
if data && data.length > 0
|
||||||
set_file_path
|
set_file_path
|
||||||
write_csv_file(data)
|
header = get_header(entity)
|
||||||
|
write_csv_file(data, header)
|
||||||
end
|
end
|
||||||
|
|
||||||
notify_user
|
notify_user
|
||||||
|
@ -46,7 +53,7 @@ module Jobs
|
||||||
private
|
private
|
||||||
|
|
||||||
def get_group_names(user)
|
def get_group_names(user)
|
||||||
group_names = Array.new
|
group_names = []
|
||||||
groups = user.groups
|
groups = user.groups
|
||||||
groups.each do |group|
|
groups.each do |group|
|
||||||
group_names.push(group.name)
|
group_names.push(group.name)
|
||||||
|
@ -55,7 +62,7 @@ module Jobs
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_user_fields(user)
|
def get_user_fields(user)
|
||||||
user_array = Array.new
|
user_array = []
|
||||||
|
|
||||||
CSV_USER_ATTRS.each do |attr|
|
CSV_USER_ATTRS.each do |attr|
|
||||||
user_array.push(user.attributes[attr])
|
user_array.push(user.attributes[attr])
|
||||||
|
@ -74,16 +81,31 @@ module Jobs
|
||||||
return user_array
|
return user_array
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_header
|
def get_screened_ip_fields(screened_ip)
|
||||||
header_array = CSV_USER_ATTRS + CSV_USER_STATS
|
screened_ip_array = []
|
||||||
|
|
||||||
user_custom_fields = UserField.all
|
SCREENED_IP_ATTRS.each do |attr|
|
||||||
if user_custom_fields.present?
|
screened_ip_array.push(screened_ip.attributes[attr])
|
||||||
user_custom_fields.each do |custom_field|
|
end
|
||||||
header_array.push("#{custom_field.name} (custom user field)")
|
|
||||||
end
|
return screened_ip_array
|
||||||
|
end
|
||||||
|
|
||||||
|
def get_header(entity)
|
||||||
|
|
||||||
|
case entity
|
||||||
|
when 'user'
|
||||||
|
header_array = CSV_USER_ATTRS + CSV_USER_STATS
|
||||||
|
user_custom_fields = UserField.all
|
||||||
|
if user_custom_fields.present?
|
||||||
|
user_custom_fields.each do |custom_field|
|
||||||
|
header_array.push("#{custom_field.name} (custom user field)")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
header_array.push("group_names")
|
||||||
|
when 'screened_ips'
|
||||||
|
header_array = SCREENED_IP_ATTRS
|
||||||
end
|
end
|
||||||
header_array.push("group_names")
|
|
||||||
|
|
||||||
return header_array
|
return header_array
|
||||||
end
|
end
|
||||||
|
@ -95,10 +117,10 @@ module Jobs
|
||||||
FileUtils.mkdir_p(dir) unless Dir.exists?(dir)
|
FileUtils.mkdir_p(dir) unless Dir.exists?(dir)
|
||||||
end
|
end
|
||||||
|
|
||||||
def write_csv_file(data)
|
def write_csv_file(data, header)
|
||||||
# write to CSV file
|
# write to CSV file
|
||||||
CSV.open(File.expand_path("#{ExportCsv.base_directory}/#{@file_name}", __FILE__), "w") do |csv|
|
CSV.open(File.expand_path("#{ExportCsv.base_directory}/#{@file_name}", __FILE__), "w") do |csv|
|
||||||
csv << get_header
|
csv << header
|
||||||
data.each do |value|
|
data.each do |value|
|
||||||
csv << value
|
csv << value
|
||||||
end
|
end
|
||||||
|
|
|
@ -1686,6 +1686,9 @@ en:
|
||||||
users:
|
users:
|
||||||
text: "Export Users"
|
text: "Export Users"
|
||||||
title: "Export user list in a CSV file."
|
title: "Export user list in a CSV file."
|
||||||
|
screened_ips:
|
||||||
|
text: "Export Screened IPs"
|
||||||
|
title: "Export screened IPs list in a CSV file."
|
||||||
success: "Export has been initiated, you will be notified shortly with progress."
|
success: "Export has been initiated, you will be notified shortly with progress."
|
||||||
failed: "Export failed. Please check the logs."
|
failed: "Export failed. Please check the logs."
|
||||||
|
|
||||||
|
|
|
@ -159,6 +159,7 @@ Discourse::Application.routes.draw do
|
||||||
resources :export_csv, constraints: AdminConstraint.new do
|
resources :export_csv, constraints: AdminConstraint.new do
|
||||||
collection do
|
collection do
|
||||||
get "users" => "export_csv#export_user_list"
|
get "users" => "export_csv#export_user_list"
|
||||||
|
get "screened_ips" => "export_csv#export_screened_ips_list"
|
||||||
end
|
end
|
||||||
member do
|
member do
|
||||||
get "" => "export_csv#show", constraints: { id: /[^\/]+/ }
|
get "" => "export_csv#show", constraints: { id: /[^\/]+/ }
|
||||||
|
|
Loading…
Reference in New Issue