2019-05-03 08:17:27 +10:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-12-22 21:47:04 +05:30
|
|
|
class ExportCsvController < ApplicationController
|
2017-08-31 12:06:56 +08:00
|
|
|
skip_before_action :preload_json, :check_xhr, only: [:show]
|
2014-08-09 15:58:57 +05:30
|
|
|
|
2014-12-07 09:45:22 +05:30
|
|
|
def export_entity
|
2024-02-22 13:47:15 -06:00
|
|
|
entity = export_params[:entity]
|
2025-01-24 08:13:25 +11:00
|
|
|
entity_id = params.dig(:args, :export_user_id)&.to_i if entity == "user_archive"
|
|
|
|
guardian.ensure_can_export_entity!(entity, entity_id)
|
2024-02-22 13:47:15 -06:00
|
|
|
raise Discourse::InvalidParameters.new(:entity) unless entity.is_a?(String) && entity.size < 100
|
2020-08-27 15:54:25 -07:00
|
|
|
|
2024-02-22 13:47:15 -06:00
|
|
|
(export_params[:args] || {}).each do |key, value|
|
|
|
|
unless value.is_a?(String) && value.size < 100
|
|
|
|
raise Discourse::InvalidParameters.new("args.#{key}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if entity == "user_archive"
|
2025-01-24 08:13:25 +11:00
|
|
|
requesting_user_id = current_user.id if entity_id
|
2025-01-24 09:37:05 +11:00
|
|
|
|
|
|
|
# Rate limit user archive exports to 1 per day
|
|
|
|
unless current_user.admin ||
|
|
|
|
UserExport.where(
|
|
|
|
user_id: entity_id || current_user.id,
|
|
|
|
created_at: (Time.zone.now.beginning_of_day..Time.zone.now.end_of_day),
|
|
|
|
).count == 0
|
|
|
|
render_json_error I18n.t("csv_export.rate_limit_error")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2025-01-24 08:13:25 +11:00
|
|
|
Jobs.enqueue(
|
|
|
|
:export_user_archive,
|
|
|
|
user_id: entity_id || current_user.id,
|
|
|
|
requesting_user_id:,
|
|
|
|
args: export_params[:args],
|
|
|
|
)
|
2020-08-27 15:54:25 -07:00
|
|
|
else
|
2023-01-09 12:20:10 +00:00
|
|
|
Jobs.enqueue(
|
|
|
|
:export_csv_file,
|
2024-02-22 13:47:15 -06:00
|
|
|
entity: entity,
|
2023-01-09 12:20:10 +00:00
|
|
|
user_id: current_user.id,
|
|
|
|
args: export_params[:args],
|
|
|
|
)
|
2020-08-27 15:54:25 -07:00
|
|
|
end
|
2024-02-22 13:47:15 -06:00
|
|
|
StaffActionLogger.new(current_user).log_entity_export(entity)
|
2014-11-21 20:55:04 +05:30
|
|
|
render json: success_json
|
2019-12-24 15:49:27 +05:30
|
|
|
rescue Discourse::InvalidAccess
|
2019-12-24 15:56:44 +05:30
|
|
|
render_json_error I18n.t("csv_export.rate_limit_error")
|
2014-11-21 20:55:04 +05:30
|
|
|
end
|
|
|
|
|
2025-01-24 08:13:25 +11:00
|
|
|
def latest_user_archive
|
|
|
|
user_id = params[:user_id].to_i
|
|
|
|
# If we can't export the entity, we shouldn't be able to see it either
|
|
|
|
guardian.ensure_can_export_entity!("user_archive", user_id)
|
|
|
|
|
|
|
|
render json:
|
|
|
|
UserExport
|
|
|
|
.where(user_id:)
|
|
|
|
.where("created_at > ?", UserExport::DESTROY_CREATED_BEFORE.ago)
|
|
|
|
.order(created_at: :desc)
|
|
|
|
.first
|
|
|
|
end
|
|
|
|
|
2015-09-15 16:45:01 -04:00
|
|
|
private
|
2019-07-22 14:59:56 +03:00
|
|
|
|
2018-06-07 13:28:18 +08:00
|
|
|
def export_params
|
2023-01-09 12:20:10 +00:00
|
|
|
@_export_params ||=
|
|
|
|
begin
|
|
|
|
params.require(:entity)
|
|
|
|
params.permit(:entity, args: Report::FILTERS).to_h
|
|
|
|
end
|
2018-06-07 13:28:18 +08:00
|
|
|
end
|
2014-08-09 15:58:57 +05:30
|
|
|
end
|