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

[chore] introduce new interface for set permission command

Eric Schubert8306ca4eebbf
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/inputs/set_permissions.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/inputs/set_permissions.rb
new file mode 100644
index 00000000000..90ecbc971f2
--- /dev/null
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/inputs/set_permissions.rb
@@ -0,0 +1,69 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 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 Storages
+ module Peripherals
+ module StorageInteraction
+ module Inputs
+ # permissions - A map of user ids to a map of all file permissions.
+ # IMPORTANT: the user ids are considered to be the ids of the remote identities.
+ # Example:
+ # {
+ # "d6e00f6d-1ae7-43e6-b0af-15d99a56d4ce": {
+ # read_files: true,
+ # write_files: false,
+ # create_files: false,
+ # delete_files: false,
+ # share_files: false
+ # }
+ # }
+ SetPermissions = Data.define(:file_id, :permissions) do
+ def valid?
+ file_id.is_a?(String) && file_id.present? && valid_permissions?
+ end
+
+ private
+
+ def valid_permissions?
+ permissions.is_a?(Hash) &&
+ permissions.all? do |user_id, user_permissions|
+ user_id.is_a?(Symbol) &&
+ user_permissions.is_a?(Hash) &&
+ user_permissions.all? do |permission, value|
+ OpenProject::Storages::Engine.permissions.include?(permission) &&
+ (value.is_a?(TrueClass) || value.is_a?(FalseClass))
+ end
+ end
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/set_permissions_command.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/set_permissions_command.rb
index ba3ee91542e..d2a138e2f47 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/set_permissions_command.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/set_permissions_command.rb
@@ -32,6 +32,8 @@ module Storages
module Peripherals
module StorageInteraction
module Nextcloud
+ # TODO: refactor to be consistent with OneDrive::SetPermissionsCommand interface
+ # And streamline test cases.
class SetPermissionsCommand
include TaggedLogging
using ServiceResultRefinements
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/set_permissions_command.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/set_permissions_command.rb
index 0964c821fc5..a9eb37d870f 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/set_permissions_command.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/set_permissions_command.rb
@@ -45,25 +45,33 @@ module Storages
def update? = permission_ids.any? && user_ids.any?
end
- def self.call(storage:, auth_strategy:, path:, permissions:)
- new(storage).call(auth_strategy:, path:, permissions:)
+ def self.call(storage:, auth_strategy:, input_data:)
+ new(storage).call(auth_strategy:, input_data:)
end
def initialize(storage)
@storage = storage
end
- def call(auth_strategy:, path:, permissions:)
+ # rubocop:disable Metrics/AbcSize
+ def call(auth_strategy:, input_data:)
with_tagged_logger do
+ info "Validating input data"
+ validate_input_data(input_data).on_failure { return _1 }
+
Authentication[auth_strategy].call(storage: @storage) do |http|
- item_exists?(http, path).on_failure { return _1 }
+ item = input_data.file_id
+ item_exists?(http, item).on_failure { return _1 }
- current_permissions = get_current_permissions(http, path).on_failure { return _1 }.result
+ current_permissions = get_current_permissions(http, item).on_failure { return _1 }.result
info "Read and write permissions found: #{current_permissions}"
- permissions.each_pair do |role, user_ids|
+ role_to_user_map(input_data).each_pair do |role, user_ids|
apply_permission_changes(
- PermissionUpdateData.new(role:, user_ids:, permission_ids: current_permissions[role], drive_item_id: path),
+ PermissionUpdateData.new(role:,
+ user_ids:,
+ permission_ids: current_permissions[role],
+ drive_item_id: item),
http
)
end
@@ -73,8 +81,29 @@ module Storages
end
end
+ # rubocop:enable Metrics/AbcSize
+
private
+ def validate_input_data(input_data)
+ if input_data.is_a?(Inputs::SetPermissions) && input_data.valid?
+ ServiceResult.success
+ else
+ ServiceResult.failure(result: :error,
+ errors: StorageError.new(code: :error,
+ data: StorageErrorData.new(source: self.class),
+ log_message: "Invalid input data: #{input_data.inspect}"))
+ end
+ end
+
+ def role_to_user_map(input_data)
+ input_data.permissions
+ .each_with_object({ read: [], write: [] }) do |(user_id, permissions), map|
+ map[:read] << user_id if permissions[:read_files]
+ map[:write] << user_id if permissions[:write_files]
+ end
+ end
+
def item_exists?(http, item_id)
info "Checking if folder #{item_id} exists"
handle_response(http.get(item_path(item_id)))
diff --git a/modules/storages/spec/common/storages/peripherals/storage_interaction/one_drive/set_permissions_command_spec.rb b/modules/storages/spec/common/storages/peripherals/storage_interaction/one_drive/set_permissions_command_spec.rb
index 068c4620068..a648aa7828d 100644
--- a/modules/storages/spec/common/storages/peripherals/storage_interaction/one_drive/set_permissions_command_spec.rb
+++ b/modules/storages/spec/common/storages/peripherals/storage_interaction/one_drive/set_permissions_command_spec.rb
@@ -37,7 +37,9 @@ RSpec.describe Storages::Peripherals::StorageInteraction::OneDrive::SetPermissio
drive_id: "b!dmVLG22QlE2PSW0AqVB7UOhZ8n7tjkVGkgqLNnuw2ODRDvn3haLiQIhB5UYNdqMy")
end
- let(:auth_strategy) { Storages::Peripherals::StorageInteraction::AuthenticationStrategies::OAuthClientCredentials.new }
+ let(:auth_strategy) do
+ Storages::Peripherals::StorageInteraction::AuthenticationStrategies::OAuthClientCredentials.new(false)
+ end
let(:permissions_command) { described_class.new(storage) }
@@ -53,17 +55,9 @@ RSpec.describe Storages::Peripherals::StorageInteraction::OneDrive::SetPermissio
let(:path) { folder.id }
- it "is registered at commands.one_drive.set_permissions" do
- expect(Storages::Peripherals::Registry.resolve("one_drive.commands.set_permissions")).to eq(described_class)
- end
-
- it "responds to .call with storage, path and permissions keyword args" do
- expect(described_class).to respond_to(:call)
+ it_behaves_like "set_permissions_command: basic command setup"
- method = described_class.method(:call)
- expect(method.parameters).to contain_exactly(%i[keyreq auth_strategy], %i[keyreq storage],
- %i[keyreq path], %i[keyreq permissions])
- end
+ it_behaves_like "set_permissions_command: validating input data"
describe "#call" do
after do
@@ -74,8 +68,8 @@ RSpec.describe Storages::Peripherals::StorageInteraction::OneDrive::SetPermissio
context "when trying to access a non-existing driveItem" do
it "returns a failure", vcr: "one_drive/set_permissions_not_found_folder" do
- result = permissions_command.call(auth_strategy:, path: "THIS_IS_NOT_THE_FOLDER_YOURE_LOOKING_FOR",
- permissions: { write: [] })
+ input_data = input_data(file_id: "THIS_IS_NOT_THE_FOLDER_YOURE_LOOKING_FOR", permissions: {})
+ result = permissions_command.call(auth_strategy:, input_data:)
expect(result).to be_failure
expect(result.result).to eq(:not_found)
@@ -85,19 +79,27 @@ RSpec.describe Storages::Peripherals::StorageInteraction::OneDrive::SetPermissio
context "when a permission set already exists" do
it "replaces the write permission grant with the provided list",
vcr: "one_drive/set_permissions_replace_permissions_write" do
- permissions_command.call(auth_strategy:, path:, permissions: { write: ["84acc1d5-61be-470b-9d79-0d1f105c2c5f"] })
+ input_data = input_data(file_id: folder.id,
+ permissions: { "84acc1d5-61be-470b-9d79-0d1f105c2c5f": { write_files: true } })
+ permissions_command.call(auth_strategy:, input_data:)
expect(user_list("write")).to match_array("84acc1d5-61be-470b-9d79-0d1f105c2c5f")
- permissions_command.call(auth_strategy:, path:, permissions: { write: ["d6e00f6d-1ae7-43e6-b0af-15d99a56d4ce"] })
+ input_data = input_data(file_id: folder.id,
+ permissions: { "d6e00f6d-1ae7-43e6-b0af-15d99a56d4ce": { write_files: true } })
+ permissions_command.call(auth_strategy:, input_data:)
expect(user_list("write")).to match_array("d6e00f6d-1ae7-43e6-b0af-15d99a56d4ce")
end
it "replaces the read permission grant with the provided list",
vcr: "one_drive/set_permissions_replace_permissions_read" do
- permissions_command.call(auth_strategy:, path:, permissions: { read: ["84acc1d5-61be-470b-9d79-0d1f105c2c5f"] })
+ input_data = input_data(file_id: folder.id,
+ permissions: { "84acc1d5-61be-470b-9d79-0d1f105c2c5f": { read_files: true } })
+ permissions_command.call(auth_strategy:, input_data:)
expect(user_list("read")).to match_array("84acc1d5-61be-470b-9d79-0d1f105c2c5f")
- permissions_command.call(auth_strategy:, path:, permissions: { read: ["d6e00f6d-1ae7-43e6-b0af-15d99a56d4ce"] })
+ input_data = input_data(file_id: folder.id,
+ permissions: { "d6e00f6d-1ae7-43e6-b0af-15d99a56d4ce": { read_files: true } })
+ permissions_command.call(auth_strategy:, input_data:)
expect(user_list("read")).to match_array("d6e00f6d-1ae7-43e6-b0af-15d99a56d4ce")
end
end
@@ -107,7 +109,9 @@ RSpec.describe Storages::Peripherals::StorageInteraction::OneDrive::SetPermissio
current_roles = remote_permissions.map { |permission| permission[:roles].first }
expect(current_roles).not_to include("write")
- permissions_command.call(auth_strategy:, path:, permissions: { write: ["d6e00f6d-1ae7-43e6-b0af-15d99a56d4ce"] })
+ input_data = input_data(file_id: folder.id,
+ permissions: { "d6e00f6d-1ae7-43e6-b0af-15d99a56d4ce": { write_files: true } })
+ permissions_command.call(auth_strategy:, input_data:)
current_roles = remote_permissions.map { |permission| permission[:roles].first }
expect(current_roles).to include("write")
@@ -117,7 +121,9 @@ RSpec.describe Storages::Peripherals::StorageInteraction::OneDrive::SetPermissio
current_roles = remote_permissions.map { |permission| permission[:roles].first }
expect(current_roles).not_to include("read")
- permissions_command.call(auth_strategy:, path:, permissions: { read: ["d6e00f6d-1ae7-43e6-b0af-15d99a56d4ce"] })
+ input_data = input_data(file_id: folder.id,
+ permissions: { "d6e00f6d-1ae7-43e6-b0af-15d99a56d4ce": { read_files: true } })
+ permissions_command.call(auth_strategy:, input_data:)
current_roles = remote_permissions.map { |permission| permission[:roles].first }
expect(current_roles).to include("read")
@@ -126,22 +132,26 @@ RSpec.describe Storages::Peripherals::StorageInteraction::OneDrive::SetPermissio
context "when there are no user to set permissions" do
it "deletes the write permission", vcr: "one_drive/set_permissions_delete_permission_write" do
- permissions_command.call(auth_strategy:, path:, permissions: { write: ["d6e00f6d-1ae7-43e6-b0af-15d99a56d4ce"] })
+ input_data = input_data(file_id: folder.id,
+ permissions: { "d6e00f6d-1ae7-43e6-b0af-15d99a56d4ce": { write_files: true } })
+ permissions_command.call(auth_strategy:, input_data:)
current_roles = remote_permissions.map { |permission| permission[:roles].first }
expect(current_roles).to include("write")
- permissions_command.call(auth_strategy:, path:, permissions: { write: [] })
+ permissions_command.call(auth_strategy:, input_data: input_data(file_id: folder.id, permissions: {}))
current_roles = remote_permissions.map { |permission| permission[:roles].first }
expect(current_roles).not_to include("write")
end
it "deletes the read permission", vcr: "one_drive/set_permissions_delete_permission_read" do
- permissions_command.call(auth_strategy:, path:, permissions: { read: ["d6e00f6d-1ae7-43e6-b0af-15d99a56d4ce"] })
+ input_data = input_data(file_id: folder.id,
+ permissions: { "d6e00f6d-1ae7-43e6-b0af-15d99a56d4ce": { read_files: true } })
+ permissions_command.call(auth_strategy:, input_data:)
current_roles = remote_permissions.map { |permission| permission[:roles].first }
expect(current_roles).to include("read")
- permissions_command.call(auth_strategy:, path:, permissions: { read: [] })
+ permissions_command.call(auth_strategy:, input_data: input_data(file_id: folder.id, permissions: {}))
current_roles = remote_permissions.map { |permission| permission[:roles].first }
expect(current_roles).not_to include("read")
@@ -153,7 +163,9 @@ RSpec.describe Storages::Peripherals::StorageInteraction::OneDrive::SetPermissio
stub_request_with_timeout(:post, /invite$/)
allow(Rails.logger).to receive(:error)
- permissions_command.call(auth_strategy:, path:, permissions: { read: ["d6e00f6d-1ae7-43e6-b0af-15d99a56d4ce"] })
+ input_data = input_data(file_id: folder.id,
+ permissions: { "d6e00f6d-1ae7-43e6-b0af-15d99a56d4ce": { read_files: true } })
+ permissions_command.call(auth_strategy:, input_data:)
# rubocop:disable Layout/LineLength
expect(Rails.logger)
@@ -170,6 +182,10 @@ RSpec.describe Storages::Peripherals::StorageInteraction::OneDrive::SetPermissio
private
+ def input_data(file_id:, permissions:)
+ Storages::Peripherals::StorageInteraction::Inputs::SetPermissions.new(file_id:, permissions:)
+ end
+
def user_list(role)
remote_permissions
.select { |item| item[:roles].first == role }
diff --git a/modules/storages/spec/support/shared_examples_for_adapters/set_permissions_command_examples.rb b/modules/storages/spec/support/shared_examples_for_adapters/set_permissions_command_examples.rb
new file mode 100644
index 00000000000..b2fbfc6bf51
--- /dev/null
+++ b/modules/storages/spec/support/shared_examples_for_adapters/set_permissions_command_examples.rb
@@ -0,0 +1,94 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 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.
+#++
+
+RSpec.shared_examples_for "set_permissions_command: basic command setup" do
+ it "is registered as commands.set_permissions" do
+ expect(Storages::Peripherals::Registry
+ .resolve("#{storage.short_provider_type}.commands.set_permissions")).to eq(described_class)
+ end
+
+ it "responds to #call with correct parameters" do
+ expect(described_class).to respond_to(:call)
+
+ method = described_class.method(:call)
+ expect(method.parameters).to contain_exactly(%i[keyreq storage],
+ %i[keyreq auth_strategy],
+ %i[keyreq input_data])
+ end
+end
+
+RSpec.shared_examples_for "set_permissions_command: error" do
+ it "returns a failure" do
+ result = described_class.call(storage:, auth_strategy:, input_data:)
+
+ expect(result).to be_failure
+
+ error = result.errors
+ expect(error.code).to eq(:error)
+ expect(error.data.source).to eq(error_source)
+ end
+end
+
+RSpec.shared_examples_for "set_permissions_command: validating input data" do
+ let(:error_source) { described_class }
+ let(:input_data) { Storages::Peripherals::StorageInteraction::Inputs::SetPermissions.new(file_id:, permissions:) }
+ let(:file_id) { "1337" }
+ let(:permissions) { { user_id: { read_files: true } } }
+
+ context "if input data is not of the correct type" do
+ let(:input_data) { "you shall not pass" }
+
+ it_behaves_like "set_permissions_command: error"
+ end
+
+ context "if file id is nil" do
+ let(:file_id) { nil }
+
+ it_behaves_like "set_permissions_command: error"
+ end
+
+ context "if file id is empty" do
+ let(:file_id) { "" }
+
+ it_behaves_like "set_permissions_command: error"
+ end
+
+ context "if permissions is nil" do
+ let(:permissions) { nil }
+
+ it_behaves_like "set_permissions_command: error"
+ end
+
+ context "if permissions is not of the correct type" do
+ let(:permissions) { { user_id: { read_files: 1 } } }
+
+ it_behaves_like "set_permissions_command: error"
+ end
+end