FEATURE: export sso data if sso is enabled

This commit is contained in:
Sam 2014-11-26 09:43:17 +11:00
parent 4c9f55d1e1
commit d171d6db19
2 changed files with 82 additions and 41 deletions

View file

@ -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']
CSV_USER_SSO_ATTRS = ['external_id','external_email', 'external_username', 'external_name', 'external_avatar_url']
SCREENED_IP_ATTRS = ['ip_address','action_type','match_count','last_match_at','created_at'] SCREENED_IP_ATTRS = ['ip_address','action_type','match_count','last_match_at','created_at']
sidekiq_options retry: false sidekiq_options retry: false
@ -19,27 +20,15 @@ module Jobs
entity = args[:entity] entity = args[:entity]
@current_user = User.find_by(id: args[:user_id]) @current_user = User.find_by(id: args[:user_id])
raise Discourse::InvalidParameters.new(:entity) if entity.blank?
case entity data =
when 'user' if entity == 'user'
query = ::AdminUserIndexQuery.new user_export
user_data = query.find_users_query.to_a elsif entity == 'screened_ips'
data = [] screened_ips_export
user_data.each do |user| else
group_names = get_group_names(user).join(';') raise Discourse::InvalidParameters.new(:entity) if entity.blank?
user_array = get_user_fields(user) end
user_array.push(group_names) if group_names != ''
data.push(user_array)
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
if data && data.length > 0 if data && data.length > 0
set_file_path set_file_path
@ -50,6 +39,47 @@ module Jobs
notify_user notify_user
end end
def user_export
query = ::AdminUserIndexQuery.new
user_data = query.find_users_query.to_a
user_data.map do |user|
group_names = get_group_names(user).join(';')
user_array = get_user_fields(user)
user_array.push(group_names) if group_names != ''
user_array
end
end
def screened_ips_export
screened_ips_data = ScreenedIpAddress.order('id desc').to_a
screened_ips_data.map do |screened_ip|
get_screened_ip_fields(screened_ip)
end
end
def get_header(entity)
case entity
when 'user'
header_array = CSV_USER_ATTRS + CSV_USER_STATS
if SiteSetting.enable_sso
header_array.concat(CSV_USER_SSO_ATTRS)
end
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
header_array
end
private private
def get_group_names(user) def get_group_names(user)
@ -72,13 +102,21 @@ module Jobs
user_array.push(user.user_stat.attributes[stat]) user_array.push(user.user_stat.attributes[stat])
end end
if SiteSetting.enable_sso
sso = user.single_sign_on_record
CSV_USER_SSO_ATTRS.each do |stat|
field = sso.attributes[stat] if sso
user_array.push(field)
end
end
if user.user_fields.present? if user.user_fields.present?
user.user_fields.each do |custom_field| user.user_fields.each do |custom_field|
user_array.push(custom_field[1]) user_array.push(custom_field[1])
end end
end end
return user_array user_array
end end
def get_screened_ip_fields(screened_ip) def get_screened_ip_fields(screened_ip)
@ -88,27 +126,9 @@ module Jobs
screened_ip_array.push(screened_ip.attributes[attr]) screened_ip_array.push(screened_ip.attributes[attr])
end end
return screened_ip_array screened_ip_array
end 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
return header_array
end
def set_file_path def set_file_path
@file_name = "export_#{SecureRandom.hex(4)}.csv" @file_name = "export_#{SecureRandom.hex(4)}.csv"

View file

@ -3,11 +3,32 @@ require 'spec_helper'
describe Jobs::ExportCsvFile do describe Jobs::ExportCsvFile do
context '.execute' do context '.execute' do
it 'raises an error when the entity is missing' do it 'raises an error when the entity is missing' do
lambda { Jobs::ExportCsvFile.new.execute(user_id: "1") }.should raise_error(Discourse::InvalidParameters) lambda { Jobs::ExportCsvFile.new.execute(user_id: "1") }.should raise_error(Discourse::InvalidParameters)
end end
end
let :user_header do
Jobs::ExportCsvFile.new.get_header('user')
end
let :user_export do
Jobs::ExportCsvFile.new.user_export
end
def to_hash(row)
Hash[*user_header.zip(row).flatten]
end
it 'exports sso data' do
SiteSetting.enable_sso = true
user = Fabricate(:user)
user.create_single_sign_on_record(external_id: "123", last_payload: "xxx", external_email: 'test@test.com')
user = to_hash(user_export.find{|u| u[0] == user.id})
user["external_id"].should == "123"
user["external_email"].should == "test@test.com"
end end
end end