Aadd 'secondary_emails' field in users export

FIX: escape_comma wasn't working in CSV exports
FIX: group_names field wasn't properly serialized
This commit is contained in:
Régis Hanol 2019-02-27 10:12:20 +01:00
parent cfddfa6de2
commit 326d892f5e
2 changed files with 57 additions and 22 deletions

View File

@ -11,7 +11,7 @@ module Jobs
HEADER_ATTRS_FOR ||= HashWithIndifferentAccess.new( HEADER_ATTRS_FOR ||= HashWithIndifferentAccess.new(
user_archive: ['topic_title', 'category', 'sub_category', 'is_pm', 'post', 'like_count', 'reply_count', 'url', 'created_at'], user_archive: ['topic_title', 'category', 'sub_category', 'is_pm', 'post', 'like_count', 'reply_count', 'url', 'created_at'],
user_list: ['id', 'name', 'username', 'email', 'title', 'created_at', 'last_seen_at', 'last_posted_at', 'last_emailed_at', 'trust_level', 'approved', 'suspended_at', 'suspended_till', 'silenced_till', 'active', 'admin', 'moderator', 'ip_address', 'staged'], user_list: ['id', 'name', 'username', 'email', 'title', 'created_at', 'last_seen_at', 'last_posted_at', 'last_emailed_at', 'trust_level', 'approved', 'suspended_at', 'suspended_till', 'silenced_till', 'active', 'admin', 'moderator', 'ip_address', 'staged', 'secondary_emails'],
user_stats: ['topics_entered', 'posts_read_count', 'time_read', 'topic_count', 'post_count', 'likes_given', 'likes_received'], user_stats: ['topics_entered', 'posts_read_count', 'time_read', 'topic_count', 'post_count', 'likes_given', 'likes_received'],
user_profile: ['location', 'website', 'views'], user_profile: ['location', 'website', 'views'],
user_sso: ['external_id', 'external_email', 'external_username', 'external_name', 'external_avatar_url'], user_sso: ['external_id', 'external_email', 'external_username', 'external_name', 'external_avatar_url'],
@ -114,7 +114,7 @@ module Jobs
if SiteSetting.enable_sso if SiteSetting.enable_sso
# SSO enabled # SSO enabled
User.where(condition).includes(:user_profile, :user_stat, :single_sign_on_record, :groups).find_each do |user| User.where(condition).includes(:user_profile, :user_stat, :user_emails, :single_sign_on_record, :groups).find_each do |user|
user_info_array = get_base_user_array(user) user_info_array = get_base_user_array(user)
user_info_array = add_single_sign_on(user, user_info_array) user_info_array = add_single_sign_on(user, user_info_array)
user_info_array = add_custom_fields(user, user_info_array, user_field_ids) user_info_array = add_custom_fields(user, user_info_array, user_field_ids)
@ -123,7 +123,7 @@ module Jobs
end end
else else
# SSO disabled # SSO disabled
User.where(condition).includes(:user_profile, :user_stat, :groups).find_each do |user| User.where(condition).includes(:user_profile, :user_stat, :user_emails, :groups).find_each do |user|
user_info_array = get_base_user_array(user) user_info_array = get_base_user_array(user)
user_info_array = add_custom_fields(user, user_info_array, user_field_ids) user_info_array = add_custom_fields(user, user_info_array, user_field_ids)
user_info_array = add_group_names(user, user_info_array) user_info_array = add_group_names(user, user_info_array)
@ -212,16 +212,42 @@ module Jobs
private private
def escape_comma(string) def escape_comma(string)
if string && string =~ /,/ string&.include?(",") ? %Q|"#{string}"| : string
return "#{string}"
else
return string
end
end end
def get_base_user_array(user) def get_base_user_array(user)
user_array = [] [
user_array.push(user.id, escape_comma(user.name), user.username, user.email, escape_comma(user.title), user.created_at, user.last_seen_at, user.last_posted_at, user.last_emailed_at, user.trust_level, user.approved, user.suspended_at, user.suspended_till, user.silenced_till, user.active, user.admin, user.moderator, user.ip_address, user.staged, user.user_stat.topics_entered, user.user_stat.posts_read_count, user.user_stat.time_read, user.user_stat.topic_count, user.user_stat.post_count, user.user_stat.likes_given, user.user_stat.likes_received, escape_comma(user.user_profile.location), user.user_profile.website, user.user_profile.views) user.id,
escape_comma(user.name),
user.username,
user.email,
escape_comma(user.title),
user.created_at,
user.last_seen_at,
user.last_posted_at,
user.last_emailed_at,
user.trust_level,
user.approved,
user.suspended_at,
user.suspended_till,
user.silenced_till,
user.active,
user.admin,
user.moderator,
user.ip_address,
user.staged,
user.secondary_emails.join(";"),
user.user_stat.topics_entered,
user.user_stat.posts_read_count,
user.user_stat.time_read,
user.user_stat.topic_count,
user.user_stat.post_count,
user.user_stat.likes_given,
user.user_stat.likes_received,
escape_comma(user.user_profile.location),
user.user_profile.website,
user.user_profile.views,
]
end end
def add_single_sign_on(user, user_info_array) def add_single_sign_on(user, user_info_array)
@ -243,11 +269,8 @@ module Jobs
end end
def add_group_names(user, user_info_array) def add_group_names(user, user_info_array)
group_names = user.groups.each_with_object("") do |group, names| group_names = user.groups.map { |g| g.name }.join(";")
names << "#{group.name};" user_info_array << escape_comma(group_names) if group_names.present?
end
user_info_array << group_names[0..-2] unless group_names.blank?
group_names = nil
user_info_array user_info_array
end end

View File

@ -17,9 +17,10 @@ describe Jobs::ExportCsvFile do
"system_messages.csv_export_succeeded.subject_template", "system_messages.csv_export_succeeded.subject_template",
export_title: "User Archive" export_title: "User Archive"
)) ))
expect(user.topics_allowed.last.first_post.raw).to include("user-archive-john_doe-") expect(user.topics_allowed.last.first_post.raw).to include("user-archive-john_doe-")
ensure ensure
user.uploads.find_each { |upload| upload.destroy! } user.uploads.each(&:destroy!)
end end
end end
end end
@ -28,10 +29,10 @@ describe Jobs::ExportCsvFile do
%w{ %w{
id name username email title created_at last_seen_at last_posted_at id name username email title created_at last_seen_at last_posted_at
last_emailed_at trust_level approved suspended_at suspended_till blocked last_emailed_at trust_level approved suspended_at suspended_till blocked
active admin moderator ip_address staged topics_entered posts_read_count active admin moderator ip_address staged secondary_emails topics_entered
time_read topic_count post_count likes_given likes_received location posts_read_count time_read topic_count post_count likes_given
website views external_id external_email external_username external_name likes_received location website views external_id external_email
external_avatar_url external_username external_name external_avatar_url
} }
} }
@ -41,16 +42,27 @@ describe Jobs::ExportCsvFile do
Hash[*user_list_header.zip(row).flatten] Hash[*user_list_header.zip(row).flatten]
end end
it "experts secondary emails" do
user = Fabricate(:user)
Fabricate(:secondary_email, user: user, primary: false)
secondary_emails = user.secondary_emails.join(";")
user = to_hash(user_list_export.find { |u| u[0].to_i == user.id })
expect(user["secondary_emails"]).to eq(secondary_emails)
end
it 'exports sso data' do it 'exports sso data' do
SiteSetting.sso_url = "https://www.example.com/sso" SiteSetting.sso_url = "https://www.example.com/sso"
SiteSetting.enable_sso = true SiteSetting.enable_sso = true
user = Fabricate(:user) user = Fabricate(:user)
user.user_profile.update_column(:location, "La La Land") user.user_profile.update_column(:location, "La,La Land")
user.create_single_sign_on_record(external_id: "123", last_payload: "xxx", external_email: 'test@test.com') user.create_single_sign_on_record(external_id: "123", last_payload: "xxx", external_email: 'test@test.com')
user = to_hash(user_list_export.find { |u| u[0].to_i == user.id }) user = to_hash(user_list_export.find { |u| u[0].to_i == user.id })
expect(user["location"]).to eq("La La Land") expect(user["location"]).to eq('"La,La Land"')
expect(user["external_id"]).to eq("123") expect(user["external_id"]).to eq("123")
expect(user["external_email"]).to eq("test@test.com") expect(user["external_email"]).to eq("test@test.com")
end end