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 visible scope to principals

Oliver Günther37bef43ceade
diff --git a/app/models/principal.rb b/app/models/principal.rb
index 5f4e38f2878..829169ba763 100644
--- a/app/models/principal.rb
+++ b/app/models/principal.rb
@@ -63,7 +63,8 @@ class Principal < ApplicationRecord
:possible_assignee,
:possible_member,
:user,
- :ordered_by_name
+ :ordered_by_name,
+ :visible
scope :not_locked, -> {
not_builtin.where.not(status: statuses[:locked])
diff --git a/app/models/principals/scopes/visible.rb b/app/models/principals/scopes/visible.rb
new file mode 100644
index 00000000000..ce4788eb5f2
--- /dev/null
+++ b/app/models/principals/scopes/visible.rb
@@ -0,0 +1,49 @@
+#-- encoding: UTF-8
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2021 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 docs/COPYRIGHT.rdoc for more details.
+#++
+
+# Only return Principals that are visible to the current user.
+#
+# Either the user has the `manage_members` permission in any project,
+# or all principals in visible projects are returned.
+module Principals::Scopes
+ module Visible
+ extend ActiveSupport::Concern
+
+ class_methods do
+ def visible(user = ::User.current)
+ if user.allowed_to_globally?(:manage_members)
+ all
+ else
+ in_visible_project_or_me(user)
+ end
+ end
+ end
+ end
+end
diff --git a/lib/api/v3/principals/principals_api.rb b/lib/api/v3/principals/principals_api.rb
index 2bc003ba925..aa0045c8b3a 100644
--- a/lib/api/v3/principals/principals_api.rb
+++ b/lib/api/v3/principals/principals_api.rb
@@ -39,7 +39,7 @@ module API
if query.valid?
principals = query
.results
- .where(id: Principal.in_visible_project_or_me(current_user))
+ .where(id: Principal.visible(current_user))
.includes(:preference)
::API::V3::Users::PaginatedUserCollectionRepresenter.new(principals,
diff --git a/spec/models/principals/scopes/visible_spec.rb b/spec/models/principals/scopes/visible_spec.rb
new file mode 100644
index 00000000000..bd622939790
--- /dev/null
+++ b/spec/models/principals/scopes/visible_spec.rb
@@ -0,0 +1,68 @@
+#-- encoding: UTF-8
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2021 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 docs/COPYRIGHT.rdoc for more details.
+#++
+
+require 'spec_helper'
+
+describe Principals::Scopes::Visible, type: :model do
+ describe '.visible' do
+ shared_let(:project) { FactoryBot.create :project }
+ shared_let(:other_project) { FactoryBot.create :project }
+ shared_let(:role) { FactoryBot.create :role, permissions: %i[manage_members] }
+
+ shared_let(:other_project_user) { FactoryBot.create :user, member_in_project: other_project, member_through_role: role }
+ shared_let(:global_user) { FactoryBot.create :user }
+
+ subject { ::Principal.visible.to_a }
+
+ context 'when user has manage_members permission' do
+ current_user { FactoryBot.create :user, member_in_project: project, member_through_role: role }
+
+ it 'sees all users' do
+ expect(subject).to match_array [current_user, other_project_user, global_user]
+ end
+ end
+
+ context 'when user has no manage_members permission, but it is in other project' do
+ current_user { FactoryBot.create :user, member_in_project: other_project, member_with_permissions: %i[view_work_packages] }
+
+ it 'sees the other user in the same project' do
+ expect(subject).to match_array [current_user, other_project_user]
+ end
+ end
+
+ context 'when user has no permission' do
+ current_user { FactoryBot.create :user }
+
+ it 'sees only herself' do
+ expect(subject).to match_array [current_user]
+ end
+ end
+ end
+end