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

Add UserEntityRolesQuery

Klaus Zandersd050e4236e24
diff --git a/app/services/authorization/user_entity_roles_query.rb b/app/services/authorization/user_entity_roles_query.rb
new file mode 100644
index 00000000000..efe4052c605
--- /dev/null
+++ b/app/services/authorization/user_entity_roles_query.rb
@@ -0,0 +1,41 @@
+#-- 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.
+#++
+
+class Authorization::UserEntityRolesQuery < Authorization::UserRolesQuery
+ transformations.register :all, :user_restriction do |statement, user, _|
+ statement.where(users_table[:id].eq(user.id))
+ end
+
+ transformations.register users_members_join, :entity_restriction do |statement, _, entity|
+ statement
+ .and(members_table[:entity_type].eq(entity.class.to_s))
+ .and(members_table[:entity_id].eq(entity.id))
+
+ statement.and(members_table[:project_id].eq(entity.project.id)) if entity.respond_to?(:project)
+ end
+end
diff --git a/spec/services/authorization/user_entity_roles_query_spec.rb b/spec/services/authorization/user_entity_roles_query_spec.rb
new file mode 100644
index 00000000000..90ad9f5c4b4
--- /dev/null
+++ b/spec/services/authorization/user_entity_roles_query_spec.rb
@@ -0,0 +1,66 @@
+#-- 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.
+#++
+
+require 'spec_helper'
+
+RSpec.describe Authorization::UserEntityRolesQuery do
+ let(:user) { build(:user) }
+ let(:project) { build(:project, public: false) }
+ let(:work_package) { build(:work_package, project:) }
+ let(:work_package2) { build(:work_package, project:) }
+ let(:wp_role) { build(:role) }
+ let(:non_member) { build(:non_member) }
+ let(:member) { build(:member, project:, roles: [wp_role], principal: user, entity: work_package) }
+
+ describe '.query' do
+ before do
+ non_member.save!
+ user.save!
+ end
+
+ it 'is a relation' do
+ expect(described_class.query(user, work_package)).to be_a ActiveRecord::Relation
+ end
+
+ context 'with the user being a member of the work package' do
+ before do
+ member.save!
+ end
+
+ it 'returns the work package role' do
+ expect(described_class.query(user, work_package)).to match [wp_role]
+ end
+ end
+
+ context 'without the user being member in the project' do
+ it 'is empty' do
+ expect(described_class.query(user, project)).to be_empty
+ end
+ end
+ end
+end