OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Extract member creation logic into separate module to re-use it for sharing a WP with a user that does not exist yet
Henriette Darge7651aeafd313
diff --git a/app/controllers/concerns/member_creation.rb b/app/controllers/concerns/member_creation.rbnew file mode 100644index 00000000000..f5f9e27232e--- /dev/null+++ b/app/controllers/concerns/member_creation.rb@@ -0,0 +1,115 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2023 the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+#++++module MemberCreation+ module_function++ def create_members+ overall_result = nil++ each_new_member_param do |member_params|+ service_call = Members::CreateService+ .new(user: current_user)+ .call(member_params)++ if overall_result+ overall_result.merge!(service_call)+ else+ overall_result = service_call+ end+ end++ overall_result+ end++ def each_new_member_param+ user_ids = user_ids_for_new_members(params[:member])++ group_ids = Group.where(id: user_ids).pluck(:id)++ user_ids.sort_by! { |id| group_ids.include?(id) ? 1 : -1 }++ user_ids.each do |id|+ yield permitted_params.member.merge(user_id: id, project: @project)+ end+ end++ def user_ids_for_new_members(member_params)+ invite_new_users possibly_separated_ids_for_entity(member_params, :user)+ end++ def invite_new_users(user_ids)+ user_ids.filter_map do |id|+ if id.to_i == 0 && id.present? # we've got an email - invite that user+ # Only users with the create_user permission can add users.+ if current_user.allowed_to_globally?(:create_user) && enterprise_allow_new_users?+ # The invitation can pretty much only fail due to the user already+ # having been invited. So look them up if it does.+ user = UserInvitation.invite_new_user(email: id) ||+ User.find_by_mail(id)++ user&.id+ end+ else+ id+ end+ end+ end++ def enterprise_allow_new_users?+ !OpenProject::Enterprise.user_limit_reached? || !OpenProject::Enterprise.fail_fast?+ end++ def each_comma_separated(array, &block)+ array.map do |e|+ if e.to_s.match? /\d(,\d)*/+ block.call(e)+ else+ e+ end+ end.flatten+ end++ def transform_array_of_comma_separated_ids(array)+ return array if array.blank?++ each_comma_separated(array) do |elem|+ elem.to_s.split(',')+ end+ end++ def possibly_separated_ids_for_entity(array, entity = :user)+ if !array[:"#{entity}_ids"].nil?+ transform_array_of_comma_separated_ids(array[:"#{entity}_ids"])+ elsif !array[:"#{entity}_id"].nil? && (id = array[:"#{entity}_id"]).present?+ [id]+ else+ []+ end+ end+enddiff --git a/app/controllers/members_controller.rb b/app/controllers/members_controller.rbindex 1f0b0571020..19b24b9fdd2 100644--- a/app/controllers/members_controller.rb+++ b/app/controllers/members_controller.rb@@ -27,6 +27,7 @@#++class MembersController < ApplicationController+ include MemberCreationmodel_object Memberbefore_action :find_model_object_and_projectbefore_action :authorize@@ -137,90 +138,6 @@ class MembersController < ApplicationController@members_query = Members::UserFilterComponent.query(filters)end- def create_members- overall_result = nil-- each_new_member_param do |member_params|- service_call = Members::CreateService- .new(user: current_user)- .call(member_params)-- if overall_result- overall_result.merge!(service_call)- else- overall_result = service_call- end- end-- overall_result- end-- def each_new_member_param- user_ids = user_ids_for_new_members(params[:member])-- group_ids = Group.where(id: user_ids).pluck(:id)-- user_ids.sort_by! { |id| group_ids.include?(id) ? 1 : -1 }-- user_ids.each do |id|- yield permitted_params.member.merge(user_id: id, project: @project)- end- end-- def user_ids_for_new_members(member_params)- invite_new_users possibly_separated_ids_for_entity(member_params, :user)- end-- def invite_new_users(user_ids)- user_ids.filter_map do |id|- if id.to_i == 0 && id.present? # we've got an email - invite that user- # Only users with the create_user permission can add users.- if current_user.allowed_to_globally?(:create_user) && enterprise_allow_new_users?- # The invitation can pretty much only fail due to the user already- # having been invited. So look them up if it does.- user = UserInvitation.invite_new_user(email: id) ||- User.find_by_mail(id)-- user&.id- end- else- id- end- end- end-- def enterprise_allow_new_users?- !OpenProject::Enterprise.user_limit_reached? || !OpenProject::Enterprise.fail_fast?- end-- def each_comma_separated(array, &block)- array.map do |e|- if e.to_s.match? /\d(,\d)*/- block.call(e)- else- e- end- end.flatten- end-- def transform_array_of_comma_separated_ids(array)- return array if array.blank?-- each_comma_separated(array) do |elem|- elem.to_s.split(',')- end- end-- def possibly_separated_ids_for_entity(array, entity = :user)- if !array[:"#{entity}_ids"].nil?- transform_array_of_comma_separated_ids(array[:"#{entity}_ids"])- elsif !array[:"#{entity}_id"].nil? && (id = array[:"#{entity}_id"]).present?- [id]- else- []- end- end-def members_added_notice(members)if members.size == 1I18n.t(:notice_member_added, name: members.first.name)diff --git a/app/controllers/work_packages/shares_controller.rb b/app/controllers/work_packages/shares_controller.rbindex 2866ab107fe..adfad96bbeb 100644--- a/app/controllers/work_packages/shares_controller.rb+++ b/app/controllers/work_packages/shares_controller.rb@@ -28,6 +28,7 @@class WorkPackages::SharesController < ApplicationControllerinclude OpTurbo::ComponentStream+ include MemberCreationbefore_action :find_work_package, only: %i[index create]before_action :find_share, only: %i[destroy update]@@ -39,10 +40,18 @@ class WorkPackages::SharesController < ApplicationControllerenddef create+ user_id = params[:member][:user_id]++ # In case, the user does not exist yet: create one+ if user_id.to_i == 0+ service_call = create_members+ user_id = service_call.result.user_id+ end+@share = WorkPackageMembers::CreateOrUpdateService.new(user: current_user).call(entity: @work_package,- user_id: params[:member][:user_id],+ user_id:,role_ids: find_role_ids(params[:member][:role_id])).result