Skip to content

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

introduce scope for fetching users having an entity membership

ulferts0f819f2e5a66
diff --git a/app/components/work_packages/share/modal_component.html.erb b/app/components/work_packages/share/modal_component.html.erb
index c08628a401f..fd0e73bb6ef 100644
--- a/app/components/work_packages/share/modal_component.html.erb
+++ b/app/components/work_packages/share/modal_component.html.erb
@@ -33,7 +33,7 @@
invited_user_list.with_row do
flex_layout(align_items: :center) do |user_row_container|
user_row_container.with_column(flex: :auto) do
- render(Users::AvatarComponent.new(user: User.find(user), size: :medium))
+ render(Users::AvatarComponent.new(user: user, size: :medium))
end
user_row_container.with_column(color: :subtle, style: "width: 150px;") do
diff --git a/app/components/work_packages/share/modal_component.rb b/app/components/work_packages/share/modal_component.rb
index 9d0f8328c57..236bb759f30 100644
--- a/app/components/work_packages/share/modal_component.rb
+++ b/app/components/work_packages/share/modal_component.rb
@@ -37,7 +37,7 @@ module WorkPackages
super
@work_package = work_package
- @shared_users = Member.of_work_package(@work_package).pluck(:user_id)
+ @shared_users = User.having_entity_membership(@work_package)
end
end
end
diff --git a/app/models/user.rb b/app/models/user.rb
index b304eb6daa4..535d60bb98d 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -89,7 +89,8 @@ class User < Principal
:notified_globally,
:watcher_recipients,
:with_time_zone,
- :having_reminder_mail_to_send
+ :having_reminder_mail_to_send,
+ :having_entity_membership
def self.create_blocked_scope(scope, blocked)
scope.where(blocked_condition(blocked))
diff --git a/app/models/users/scopes/having_entity_membership.rb b/app/models/users/scopes/having_entity_membership.rb
new file mode 100644
index 00000000000..9fe94b5c5e0
--- /dev/null
+++ b/app/models/users/scopes/having_entity_membership.rb
@@ -0,0 +1,40 @@
+# -- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2010-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 Users::Scopes
+ module HavingEntityMembership
+ extend ActiveSupport::Concern
+
+ class_methods do
+ # Returns users having an entity membership for the given entity.
+ def having_entity_membership(work_package)
+ where(id: Member.of_work_package(work_package).select(:user_id))
+ end
+ end
+ end
+end
diff --git a/spec/models/users/scopes/having_entity_membership_spec.rb b/spec/models/users/scopes/having_entity_membership_spec.rb
new file mode 100644
index 00000000000..fb5cef57a53
--- /dev/null
+++ b/spec/models/users/scopes/having_entity_membership_spec.rb
@@ -0,0 +1,75 @@
+# -- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2010-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.
+# ++
+
+require 'spec_helper'
+
+RSpec.describe Users::Scopes::HavingEntityMembership do
+ describe '.having_entity_membership' do
+ subject { User.having_entity_membership(work_package) }
+
+ context 'with some sharing' do
+ let(:project_role) { create(:role) }
+ let(:view_work_package_role) { create(:view_work_package_role) }
+ let(:comment_work_package_role) { create(:comment_work_package_role) }
+ let(:edit_work_package_role) { create(:edit_work_package_role) }
+ let(:project) do
+ create(:project,
+ members: { non_shared_project_user => [project_role],
+ shared_project_user => [project_role] })
+ end
+ let(:work_package) do
+ create(:work_package, project:) do |wp|
+ create(:member, entity: wp, user: view_user, roles: [view_work_package_role])
+ create(:member, entity: wp, user: comment_user, roles: [comment_work_package_role])
+ create(:member, entity: wp, user: edit_user, roles: [edit_work_package_role])
+ create(:member, entity: wp, user: shared_project_user, roles: [edit_work_package_role])
+ end
+ end
+
+ let!(:view_user) { create(:user) }
+ let!(:comment_user) { create(:user) }
+ let!(:edit_user) { create(:user) }
+ let!(:non_shared_project_user) { create(:user) }
+ let!(:shared_project_user) { create(:user) }
+
+ it 'returns all those users having an entity membership' do
+ expect(subject).to contain_exactly(view_user, comment_user, edit_user, shared_project_user)
+ end
+ end
+
+ context 'without any sharing' do
+ let(:work_package) { create(:work_package) }
+
+ let!(:user) { create(:user) }
+
+ it 'is empty' do
+ expect(subject).to be_empty
+ end
+ end
+ end
+end