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 action filter to capabilities query

ulferts8f01fa7dab3f
diff --git a/app/models/queries/capabilities.rb b/app/models/queries/capabilities.rb
index 6a8b1472a7f..2c491c2e470 100644
--- a/app/models/queries/capabilities.rb
+++ b/app/models/queries/capabilities.rb
@@ -35,6 +35,7 @@ module Queries::Capabilities
Queries::Register.filter query, filter_ns::IdFilter
Queries::Register.filter query, filter_ns::PrincipalIdFilter
Queries::Register.filter query, filter_ns::ContextFilter
+ Queries::Register.filter query, filter_ns::ActionFilter
order_ns = Queries::Capabilities::Orders
diff --git a/app/models/queries/capabilities/filters/action_filter.rb b/app/models/queries/capabilities/filters/action_filter.rb
new file mode 100644
index 00000000000..1c05815838d
--- /dev/null
+++ b/app/models/queries/capabilities/filters/action_filter.rb
@@ -0,0 +1,49 @@
+#-- 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.
+#++
+
+class Queries::Capabilities::Filters::ActionFilter < Queries::Capabilities::Filters::CapabilityFilter
+ include Queries::Filters::Shared::ParsedFilter
+
+ private
+
+ def split_values
+ values.map do |value|
+ if (matches = value.match(/\A([a-z]+\/[a-z]+)\z/))
+ {
+ action: matches[1]
+ }
+ end
+ end
+ end
+
+ def value_conditions
+ split_values.map do |value|
+ "action = '#{value[:action]}'"
+ end
+ end
+end
diff --git a/spec/models/queries/capabilities/filters/action_filter_spec.rb b/spec/models/queries/capabilities/filters/action_filter_spec.rb
new file mode 100644
index 00000000000..cc661e9ba2b
--- /dev/null
+++ b/spec/models/queries/capabilities/filters/action_filter_spec.rb
@@ -0,0 +1,82 @@
+#-- 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 Queries::Capabilities::Filters::ActionFilter, type: :model do
+ it_behaves_like 'basic query filter' do
+ let(:class_key) { :action }
+ let(:type) { :string }
+ let(:model) { Capability }
+ let(:attribute) { :action }
+ let(:values) { ['memberships/create'] }
+
+ describe '#available_operators' do
+ it 'supports = and !' do
+ expect(instance.available_operators)
+ .to eql [Queries::Operators::Equals, Queries::Operators::NotEquals]
+ end
+ end
+
+ describe '#valid?' do
+ context 'without values' do
+ let(:values) { [] }
+
+ it 'is invalid' do
+ expect(instance)
+ .to be_invalid
+ end
+ end
+
+ context 'with valid value' do
+ it 'is valid' do
+ expect(instance)
+ .to be_valid
+ end
+ end
+
+ context 'with multiple valid values' do
+ let(:values) { %w[memberships/create users/create] }
+
+ it 'is valid' do
+ expect(instance)
+ .to be_valid
+ end
+ end
+
+ context 'with malfomed values' do
+ let(:values) { ["foo/5"] }
+
+ it 'is invalid' do
+ expect(instance)
+ .to be_invalid
+ end
+ end
+ end
+ end
+end
diff --git a/spec/requests/api/v3/capability_resource_spec.rb b/spec/requests/api/v3/capability_resource_spec.rb
index ad13dd41138..d2370bd9b3b 100644
--- a/spec/requests/api/v3/capability_resource_spec.rb
+++ b/spec/requests/api/v3/capability_resource_spec.rb
@@ -338,6 +338,33 @@ describe 'API v3 capabilities resource', type: :request, content_type: :json do
.at_path('total')
end
end
+
+ context 'when filtering by action' do
+ let(:filters) do
+ [{ 'action' => {
+ 'operator' => '=',
+ 'values' => ["memberships/create"]
+ } },
+ { 'principalId' => {
+ 'operator' => '=',
+ 'values' => [other_user.id.to_s]
+ } }]
+ end
+
+ let(:setup) do
+ other_user_member
+ end
+
+ it 'contains only the filtered capabilities in the response' do
+ expect(subject.body)
+ .to be_json_eql('1')
+ .at_path('total')
+
+ expect(subject.body)
+ .to be_json_eql("memberships/create/p#{project.id}-#{other_user.id}".to_json)
+ .at_path('_embedded/elements/0/id')
+ end
+ end
end
describe 'GET /api/v3/capabilities/:id' do