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

Create filter for Roles that allow becoming an assignee

Aaron Contrerasf3228c486407
diff --git a/app/models/queries/roles.rb b/app/models/queries/roles.rb
index 25a084c0f83..47f3561a84c 100644
--- a/app/models/queries/roles.rb
+++ b/app/models/queries/roles.rb
@@ -28,6 +28,7 @@
module Queries::Roles
::Queries::Register.register(RoleQuery) do
+ filter Filters::AllowsBecomingAssigneeFilter
filter Filters::GrantableFilter
filter Filters::UnitFilter
end
diff --git a/app/models/queries/roles/filters/allows_becoming_assignee_filter.rb b/app/models/queries/roles/filters/allows_becoming_assignee_filter.rb
new file mode 100644
index 00000000000..35abe6d52d1
--- /dev/null
+++ b/app/models/queries/roles/filters/allows_becoming_assignee_filter.rb
@@ -0,0 +1,71 @@
+# frozen_string_literal: true
+
+#-- 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 Queries::Roles::Filters::AllowsBecomingAssigneeFilter <
+ Queries::Roles::Filters::RoleFilter
+ def type
+ :list
+ end
+
+ def where
+ permission_ids = if values.first == OpenProject::Database::DB_VALUE_TRUE
+ assignable_permissions
+ else
+ unassignable_permissions
+ end
+
+ if operator == '='
+ ["role_permissions.id IN (?)", permission_ids]
+ else
+ ["role_permissions.id NOT IN (?)", permission_ids]
+ end
+ end
+
+ def joins
+ :role_permissions
+ end
+
+ def allowed_values
+ [[I18n.t(:general_text_yes), OpenProject::Database::DB_VALUE_TRUE],
+ [I18n.t(:general_text_no), OpenProject::Database::DB_VALUE_FALSE]]
+ end
+
+ private
+
+ def assignable_permissions
+ RolePermission.where(permission: 'work_package_assigned')
+ .select('id')
+ end
+
+ def unassignable_permissions
+ RolePermission.where.not(permission: 'work_package_assigned')
+ .select('id')
+ end
+end
diff --git a/spec/models/queries/roles/filters/allows_becoming_assignee_filter_spec.rb b/spec/models/queries/roles/filters/allows_becoming_assignee_filter_spec.rb
new file mode 100644
index 00000000000..1a2b896add2
--- /dev/null
+++ b/spec/models/queries/roles/filters/allows_becoming_assignee_filter_spec.rb
@@ -0,0 +1,99 @@
+# frozen_string_literal: true
+
+#-- 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 Queries::Roles::Filters::AllowsBecomingAssigneeFilter do
+ it_behaves_like 'basic query filter' do
+ let(:class_key) { :allows_becoming_assignee }
+ let(:type) { :list }
+ let(:model) { Role }
+ end
+
+ it_behaves_like 'boolean query filter', scope: false do
+ let(:model) { Role }
+ let(:attribute) { :type }
+ let(:permission_name) { 'work_package_assigned' }
+ end
+
+ describe '#scope' do
+ shared_let(:assignable_role) do
+ create(:role,
+ name: 'assignable_role',
+ permissions: %i[work_package_assigned])
+ end
+ shared_let(:unassignable_role) do
+ create(:role,
+ name: 'unassignable_role',
+ permissions: %i[wrong])
+ end
+
+ let(:instance) do
+ described_class.create!.tap do |filter|
+ filter.values = values
+ filter.operator = operator
+ end
+ end
+
+ subject { instance.scope.map(&:name) }
+
+ context 'with a "=" operator' do
+ let(:operator) { '=' }
+
+ context 'with a "true" value' do
+ let(:values) { [OpenProject::Database::DB_VALUE_TRUE] }
+
+ it { is_expected.to contain_exactly(assignable_role.name) }
+ end
+
+ context 'with a "false" value' do
+ let(:values) { [OpenProject::Database::DB_VALUE_FALSE] }
+
+ it { is_expected.to contain_exactly(unassignable_role.name) }
+ end
+ end
+
+ context 'with a "!" operator' do
+ let(:operator) { '!' }
+
+ context 'with a "true" value' do
+ let(:values) { [OpenProject::Database::DB_VALUE_TRUE] }
+
+ it { is_expected.to contain_exactly(unassignable_role.name) }
+ end
+
+ context 'with a "false" value' do
+ let(:values) { [OpenProject::Database::DB_VALUE_FALSE] }
+
+ it { is_expected.to contain_exactly(assignable_role.name) }
+ end
+ end
+ end
+end