From d171d6db19d724578b0ed27ef70dc3b59c22fdee Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 26 Nov 2014 09:43:17 +1100 Subject: [PATCH] FEATURE: export sso data if sso is enabled --- app/jobs/regular/export_csv_file.rb | 100 +++++++++++++++++----------- spec/jobs/export_csv_file_spec.rb | 23 ++++++- 2 files changed, 82 insertions(+), 41 deletions(-) diff --git a/app/jobs/regular/export_csv_file.rb b/app/jobs/regular/export_csv_file.rb index f3cbadec057..926aee562d7 100644 --- a/app/jobs/regular/export_csv_file.rb +++ b/app/jobs/regular/export_csv_file.rb @@ -6,6 +6,7 @@ module Jobs class ExportCsvFile < Jobs::Base 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_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'] sidekiq_options retry: false @@ -19,27 +20,15 @@ module Jobs entity = args[:entity] @current_user = User.find_by(id: args[:user_id]) - raise Discourse::InvalidParameters.new(:entity) if entity.blank? - case entity - when 'user' - query = ::AdminUserIndexQuery.new - user_data = query.find_users_query.to_a - data = [] - user_data.each do |user| - group_names = get_group_names(user).join(';') - user_array = get_user_fields(user) - 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 + data = + if entity == 'user' + user_export + elsif entity == 'screened_ips' + screened_ips_export + else + raise Discourse::InvalidParameters.new(:entity) if entity.blank? + end if data && data.length > 0 set_file_path @@ -50,6 +39,47 @@ module Jobs notify_user 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 def get_group_names(user) @@ -72,13 +102,21 @@ module Jobs user_array.push(user.user_stat.attributes[stat]) 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? user.user_fields.each do |custom_field| user_array.push(custom_field[1]) end end - return user_array + user_array end def get_screened_ip_fields(screened_ip) @@ -88,27 +126,9 @@ module Jobs screened_ip_array.push(screened_ip.attributes[attr]) end - return screened_ip_array + 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 - - return header_array - end def set_file_path @file_name = "export_#{SecureRandom.hex(4)}.csv" diff --git a/spec/jobs/export_csv_file_spec.rb b/spec/jobs/export_csv_file_spec.rb index 269fb12b2a9..8c41b43bc10 100644 --- a/spec/jobs/export_csv_file_spec.rb +++ b/spec/jobs/export_csv_file_spec.rb @@ -3,11 +3,32 @@ require 'spec_helper' describe Jobs::ExportCsvFile do context '.execute' do - it 'raises an error when the entity is missing' do lambda { Jobs::ExportCsvFile.new.execute(user_id: "1") }.should raise_error(Discourse::InvalidParameters) 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