Extract BCF aggregations

pull/7348/head
Wieland Lindenthal 6 years ago
parent 65f2de76b4
commit 65cbe29ffe
  1. 7
      modules/bcf/app/controllers/bcf/issues_controller.rb
  2. 4
      modules/bcf/app/views/bcf/issues/configure_invalid_people.html.erb
  3. 2
      modules/bcf/app/views/bcf/issues/configure_non_members.html.erb
  4. 2
      modules/bcf/app/views/bcf/issues/configure_unknown_mails.html.erb
  5. 60
      modules/bcf/lib/open_project/bcf/bcf_xml/aggregations.rb
  6. 51
      modules/bcf/lib/open_project/bcf/bcf_xml/importer.rb

@ -137,7 +137,8 @@ module ::Bcf
end
def render_config_invalid_people?
@importer.invalid_people.any? && !params.dig(:import_options, :invalid_people_action)
@importer.aggregations.invalid_people.any? && !params.dig(:import_options, :invalid_people_action)
end
end
def render_config_unknown_mails
@ -146,7 +147,7 @@ module ::Bcf
end
def render_config_unknown_mails?
@importer.unknown_mails.any? && !params.dig(:import_options, :unknown_mails_action)
@importer.aggregations.unknown_mails.any? && !params.dig(:import_options, :unknown_mails_action)
end
def render_config_non_members
@ -155,7 +156,7 @@ module ::Bcf
end
def render_config_non_members?
@importer.non_members.any? && !params.dig(:import_options, :non_members_action)
@importer.aggregations.non_members.any? && !params.dig(:import_options, :non_members_action)
end
def build_importer

@ -3,10 +3,10 @@
<%= form_tag({ action: :configure_import }, multipart: true, method: :post) do %>
<%= render 'import_options_hidden_fields' %>
<% if @importer.invalid_people.any? %>
<% if @importer.aggregations.invalid_people.any? %>
<h3>Invalid e-mail addresses found</h3>
<ul>
<% @importer.invalid_people.each do |name| %>
<% @importer.aggregations.invalid_people.each do |name| %>
<li> <%= name %></li>
<% end %>
</ul>

@ -6,7 +6,7 @@
<h3>Non project members found</h3>
<ul>
<% @importer.non_members.each do |user| %>
<% @importer.aggregations.non_members.each do |user| %>
<li><%= user.mail %> (<%= user.name %>)</li>
<% end %>
</ul>

@ -5,7 +5,7 @@
<%= render 'import_options_hidden_fields' %>
<h3>Unknown e-mail addresses found</h3>
<ul>
<% @importer.unknown_mails.each do |mail| %>
<% @importer.aggregations.unknown_mails.each do |mail| %>
<li> <%= mail %></li>
<% end %>
</ul>

@ -0,0 +1,60 @@
module OpenProject::Bcf::BcfXml
class Aggregations
attr_reader :listings, :project, :instance_cache
def initialize(listing, project)
@listings = listing
@project = project
@instance_cache = {}
end
def all_people
@instance_cache[:all_people] ||= listings.map { |entry| entry[:people] }.flatten.uniq
end
def all_mails
@instance_cache[:all_mails] ||= listings.map { |entry| entry[:mail_addresses] }.flatten.uniq
end
def known_users
@instance_cache[:known_users] ||= User.where(mail: all_mails).includes(:memberships)
end
def unknown_mails
@instance_cache[:unknown_mails] ||= all_mails.map(&:downcase) - known_users.map(&:mail).map(&:downcase)
end
def members
@instance_cache[:members] ||= known_users.select { |user| user.projects.map(&:id).include? @project.id }
end
def non_members
@instance_cache[:non_members] ||= known_users - members
end
def invalid_people
@instance_cache[:invalid_people] ||= all_people - all_mails
end
def all_statuses
@instance_cache[:all_statuses] ||= listings.map { |entry| entry[:status] }.flatten.uniq
end
def unknown_statuses
@instance_cache[:unknown_statuses] ||= all_statuses - Status.all.map(&:name)
end
def all_types
@instance_cache[:all_types] ||= listings.map { |entry| entry[:type] }.flatten.uniq
end
def unknown_types
@instance_cache[:unknown_types] ||= all_types - Type.all.map(&:name)
end
def clear_instance_cache
@instance_cache = {}
end
end
end

@ -1,16 +1,15 @@
require 'activerecord-import'
require_relative 'issue_reader'
require_relative 'aggregations'
module OpenProject::Bcf::BcfXml
class Importer
attr_reader :file, :project, :current_user, :instance_cache
attr_reader :file, :project, :current_user
def initialize(file, project, current_user:)
@file = file
@project = project
@current_user = current_user
@instance_cache = {}
end
##
@ -25,32 +24,8 @@ module OpenProject::Bcf::BcfXml
end
end
def all_people
@instance_cache[:all_people] ||= extractor_list.map { |entry| entry[:people] }.flatten.uniq
end
def all_mails
@instance_cache[:all_mails] ||= extractor_list.map { |entry| entry[:mail_addresses] }.flatten.uniq
end
def known_users
@instance_cache[:known_users] ||= User.where(mail: all_mails).includes(:memberships)
end
def unknown_mails
@instance_cache[:unknown_mails] ||= all_mails.map(&:downcase) - known_users.map(&:mail).map(&:downcase)
end
def members
@instance_cache[:members] ||= known_users.select { |user| user.projects.map(&:id).include? @project.id }
end
def non_members
@instance_cache[:non_members] ||= known_users - members
end
def invalid_people
@instance_cache[:invalid_people] ||= all_people - all_mails
def aggregations
@aggregations ||= Aggregations.new(extractor_list, @project)
end
def import!(options = {})
@ -74,10 +49,8 @@ module OpenProject::Bcf::BcfXml
private
##
# Invite all unknown email addresses and add them
def treat_invalid_people(options)
if invalid_people.any?
if aggregations.invalid_people.any?
unless options[:invalid_people_action] == 'anonymize'
raise StandardError.new 'Invalid people found in import. Use valid e-mail addresses.'
end
@ -87,11 +60,11 @@ module OpenProject::Bcf::BcfXml
##
# Invite all unknown email addresses and add them
def treat_unknown_mails(options)
if treat_unknown_mails?(options)
if aggregations.treat_unknown_mails?(options)
raise StandardError.new 'For inviting new users you need admin privileges.' unless User.current.admin?
raise StandardError.new 'Enterprise Edition user limit reached.' unless enterprise_allow_new_users?
unknown_mails.each do |mail|
aggregations.unknown_mails.each do |mail|
add_unknown_mail(mail, options)
end
end
@ -100,7 +73,7 @@ module OpenProject::Bcf::BcfXml
##
# Add all non members to project
def treat_non_members(options)
clear_instance_cache
aggregations.clear_instance_cache
if treat_non_members?(options)
unless User.current.allowed_to?(:manage_members, project)
@ -133,13 +106,13 @@ module OpenProject::Bcf::BcfXml
end
def treat_unknown_mails?(options)
unknown_mails.any? &&
aggregations.unknown_mails.any? &&
options[:unknown_mails_action] == 'invite' &&
options[:unknown_mails_invite_role_ids].any?
end
def treat_non_members?(options)
non_members.any? &&
aggregations.non_members.any? &&
options[:non_members_action] == 'add' &&
options[:non_members_add_role_ids].any?
end
@ -175,9 +148,5 @@ module OpenProject::Bcf::BcfXml
def enterprise_allow_new_users?
!OpenProject::Enterprise.user_limit_reached? || !OpenProject::Enterprise.fail_fast?
end
def clear_instance_cache
@instance_cache = {}
end
end
end

Loading…
Cancel
Save