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

[#47619] Create a storage command to set project folder permissions

Pavel Balashov5dffa16cd1e1
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
new file mode 100644
index 00000000000..e2065c25e0b
--- /dev/null
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/set_permissions_command.rb
@@ -0,0 +1,155 @@
+#-- 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.
+#++
+
+module Storages::Peripherals::StorageInteraction::Nextcloud
+ class SetPermissionsCommand < Storages::Peripherals::StorageInteraction::StorageCommand
+ include API::V3::Utilities::PathHelper
+ include Errors
+ using Storages::Peripherals::ServiceResultRefinements
+
+ PERMISSION_MAP = {
+ read_files: 1,
+ write_files: 2,
+ create_files: 4,
+ delete_files: 8,
+ share_files: 16
+ }.freeze
+
+ def initialize(base_uri:, username:, password:)
+ super()
+
+ @uri = base_uri
+ @base_path = api_v3_paths.join_uri_path(@uri.path, "remote.php/dav/files", escape_whitespace(username))
+ @username = username
+ @password = password
+ end
+
+ def execute(folder:, permissions:)
+ http = Net::HTTP.new(@uri.host, @uri.port)
+ http.use_ssl = @uri.scheme == 'https'
+
+ response = http.proppatch(
+ "#{@base_path}/#{requested_folder(folder)}",
+ converted_permissions(username: @username, permissions:),
+ {
+ 'Authorization' => "Basic #{Base64::encode64("#{@username}:#{@password}")}"
+ }
+ )
+
+ response.is_a?(Net::HTTPSuccess) ? ServiceResult.success : error(response)
+ end
+
+ private
+
+ def escape_whitespace(value)
+ value.gsub(' ', '%20')
+ end
+
+ def requested_folder(folder)
+ return '' if folder.nil?
+
+ escape_whitespace(folder)
+ end
+
+ def error(response)
+ case response
+ when Net::HTTPNotFound
+ error_result(:not_found)
+ when Net::HTTPUnauthorized
+ error_result(:not_authorized)
+ else
+ error_result(:error)
+ end
+ end
+
+ def error_result(code, log_message = nil, data = nil)
+ ServiceResult.failure(
+ result: code, # This is needed to work with the ConnectionManager token refresh mechanism.
+ errors: Storages::StorageError.new(code:, log_message:, data:)
+ )
+ end
+
+ def converted_permissions(username:, permissions:)
+ Nokogiri::XML::Builder.new do |xml|
+ xml['d'].propertyupdate(
+ 'xmlns:d' => 'DAV:',
+ 'xmlns:nc' => 'http://nextcloud.org/ns'
+ ) do
+ xml['d'].set do
+ xml['d'].prop do
+ xml['nc'].send('acl-list') do
+ control_user_permissions(username, xml)
+ control_group_permissions(xml)
+ user_permissions(xml, permissions)
+ end
+ end
+ end
+ end
+ end.to_xml
+ end
+
+ def control_user_permissions(username, xml)
+ xml['nc'].acl do
+ xml['nc'].send('acl-mapping-type', 'user')
+ xml['nc'].send('acl-mapping-id', username)
+ xml['nc'].send('acl-mask', '31')
+ xml['nc'].send('acl-permissions', '31')
+ end
+ end
+
+ def control_group_permissions(xml)
+ xml['nc'].acl do
+ xml['nc'].send('acl-mapping-type', 'group')
+ xml['nc'].send('acl-mapping-id', 'OpenProject')
+ xml['nc'].send('acl-mask', '31')
+ xml['nc'].send('acl-permissions', '0')
+ end
+ end
+
+ def user_permissions(xml, permissions)
+ permissions.each do |permission|
+ username = permission[:origin_user_id]
+ assignable_permission = nextcloud_permission(permission[:permissions])
+
+ xml['nc'].acl do
+ xml['nc'].send('acl-mapping-type', 'user')
+ xml['nc'].send('acl-mapping-id', username)
+ xml['nc'].send('acl-mask', '31')
+ xml['nc'].send('acl-permissions', assignable_permission)
+ end
+ end
+ end
+
+ def nextcloud_permission(permission)
+ permission.reduce(0) do |acc, (k, v)|
+ acc = acc + PERMISSION_MAP[k] if v
+ acc
+ end
+ end
+ end
+end
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/storage_command.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/storage_command.rb
new file mode 100644
index 00000000000..2c47fb7616a
--- /dev/null
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/storage_command.rb
@@ -0,0 +1,35 @@
+#-- 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.
+#++
+
+module Storages::Peripherals::StorageInteraction
+ class StorageCommand
+ def execute(data)
+ raise NotImplementedError, "#{self.class} has not implemented method '#{__method__}'"
+ end
+ end
+end
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/storage_commands.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/storage_commands.rb
new file mode 100644
index 00000000000..ac4e9733471
--- /dev/null
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/storage_commands.rb
@@ -0,0 +1,55 @@
+#-- 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.
+#++
+
+module Storages::Peripherals::StorageInteraction
+ class StorageCommands
+ using ::Storages::Peripherals::ServiceResultRefinements
+
+ def initialize(uri:, provider_type:, username:, password:)
+ @uri = uri
+ @provider_type = provider_type
+ @username = username
+ @password = password
+ end
+
+ def set_permissions_command
+ case @provider_type
+ when ::Storages::Storage::PROVIDER_TYPE_NEXTCLOUD
+ ServiceResult.success(
+ result: ::Storages::Peripherals::StorageInteraction::Nextcloud::SetPermissionsCommand.new(
+ base_uri: @uri,
+ username: @username,
+ password: @password
+ )
+ )
+ else
+ raise ArgumentError
+ end
+ end
+ end
+end
diff --git a/modules/storages/app/common/storages/peripherals/storage_requests.rb b/modules/storages/app/common/storages/peripherals/storage_requests.rb
index b737f7c3924..5fe52c0ec73 100644
--- a/modules/storages/app/common/storages/peripherals/storage_requests.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_requests.rb
@@ -57,6 +57,12 @@ module Storages::Peripherals
.map { |query| query.method(:query).to_proc }
end
+ def set_permissions_command
+ storage_commands
+ .set_permissions_command
+ .map { |command| command.method(:execute).to_proc }
+ end
+
private
def storage_queries(user)
@@ -68,5 +74,15 @@ module Storages::Peripherals
oauth_client: @oauth_client
)
end
+
+ def storage_commands
+ ::Storages::Peripherals::StorageInteraction::StorageCommands
+ .new(
+ uri: URI(@storage.host).normalize,
+ provider_type: @storage.provider_type,
+ username: @storage.username,
+ password: @storage.password
+ )
+ end
end
end
diff --git a/modules/storages/spec/common/peripherals/storage_requests_spec.rb b/modules/storages/spec/common/peripherals/storage_requests_spec.rb
index 9bc9f8095e8..632f7a83aa8 100644
--- a/modules/storages/spec/common/peripherals/storage_requests_spec.rb
+++ b/modules/storages/spec/common/peripherals/storage_requests_spec.rb
@@ -37,9 +37,11 @@ describe Storages::Peripherals::StorageRequests, webmock: true do
let(:origin_user_id) { 'admin' }
let(:storage) do
- storage = instance_double(Storages::Storage)
+ storage = instance_double(Storages::NextcloudStorage) # Stub as Nextcloud, because for now we only support Nextcloud
allow(storage).to receive(:oauth_client).and_return(instance_double(OAuthClient))
allow(storage).to receive(:provider_type).and_return(Storages::Storage::PROVIDER_TYPE_NEXTCLOUD)
+ allow(storage).to receive(:username).and_return('OpenProject')
+ allow(storage).to receive(:password).and_return('OpenProjectSecurePassword')
allow(storage).to receive(:host).and_return(url)
storage
end
@@ -60,6 +62,198 @@ describe Storages::Peripherals::StorageRequests, webmock: true do
subject { described_class.new(storage:) }
+ describe '#set_permissions_command' do
+ let(:permissions) do
+ [
+ {
+ origin_user_id: 'Obi-Wan',
+ permissions: {
+ read_files: true,
+ write_files: true,
+ create_files: true,
+ share_files: true,
+ delete_files: true
+ }
+ },
+ {
+ origin_user_id: 'Qui-Gon',
+ permissions: {
+ read_files: true,
+ write_files: true,
+ create_files: true,
+ share_files: true,
+ delete_files: true
+ }
+ }
+ ]
+ end
+
+ let(:expected_request_body) do
+ <<~XML
+ <?xml version="1.0"?>
+ <d:propertyupdate xmlns:d="DAV:" xmlns:nc="http://nextcloud.org/ns">
+ <d:set>
+ <d:prop>
+ <nc:acl-list>
+ <nc:acl>
+ <nc:acl-mapping-type>user</nc:acl-mapping-type>
+ <nc:acl-mapping-id>OpenProject</nc:acl-mapping-id>
+ <nc:acl-mask>31</nc:acl-mask>
+ <nc:acl-permissions>31</nc:acl-permissions>
+ </nc:acl>
+ <nc:acl>
+ <nc:acl-mapping-type>group</nc:acl-mapping-type>
+ <nc:acl-mapping-id>OpenProject</nc:acl-mapping-id>
+ <nc:acl-mask>31</nc:acl-mask>
+ <nc:acl-permissions>0</nc:acl-permissions>
+ </nc:acl>
+ <nc:acl>
+ <nc:acl-mapping-type>user</nc:acl-mapping-type>
+ <nc:acl-mapping-id>Obi-Wan</nc:acl-mapping-id>
+ <nc:acl-mask>31</nc:acl-mask>
+ <nc:acl-permissions>31</nc:acl-permissions>
+ </nc:acl>
+ <nc:acl>
+ <nc:acl-mapping-type>user</nc:acl-mapping-type>
+ <nc:acl-mapping-id>Qui-Gon</nc:acl-mapping-id>
+ <nc:acl-mask>31</nc:acl-mask>
+ <nc:acl-permissions>31</nc:acl-permissions>
+ </nc:acl>
+ </nc:acl-list>
+ </d:prop>
+ </d:set>
+ </d:propertyupdate>
+ XML
+ end
+
+ context 'with Nextcloud storage type selected' do
+ before do
+ stub_request(:proppatch, "#{url}/remote.php/dav/files/OpenProject/JediProject")
+ .with(
+ body: expected_request_body,
+ headers: {
+ 'Authorization' => 'Basic T3BlblByb2plY3Q6T3BlblByb2plY3RTZWN1cmVQYXNzd29yZA=='
+ }
+ )
+ .to_return(expected_response)
+ end
+
+ context 'when permissions can be set' do
+ let(:expected_response_body) do
+ <<~XML
+ <?xml version="1.0"?>
+ <d:multistatus
+ xmlns:d="DAV:"
+ xmlns:s="http://sabredav.org/ns"
+ xmlns:oc="http://owncloud.org/ns"
+ xmlns:nc="http://nextcloud.org/ns">
+ <d:response>
+ <d:href>/remote.php/dav/files/OpenProject/OpenProject/Project%231</d:href>
+ <d:propstat>
+ <d:prop>
+ <nc:acl-list/>
+ </d:prop>
+ <d:status>HTTP/1.1 200 OK</d:status>
+ </d:propstat>
+ </d:response>
+ </d:multistatus>
+ XML
+ end
+ let(:expected_response) do
+ {
+ status: 207,
+ body: expected_response_body,
+ headers: {}
+ }
+ end
+
+ it 'returns success when permissions can be set' do
+ subject
+ .set_permissions_command
+ .match(
+ on_success: ->(command) do
+ result = command.call(folder: 'JediProject', permissions:)
+ expect(result).to be_success
+ end,
+ on_failure: ->(error) do
+ raise "Set permissions command could not be executed: #{error}"
+ end
+ )
+ end
+ end
+
+ context 'when the password is wrong' do
+ let(:expected_response_body) do
+ <<~XML
+ <?xml version="1.0" encoding="utf-8"?>
+ <d:error
+ xmlns:d="DAV:"
+ xmlns:s="http://sabredav.org/ns">
+ <s:exception>Sabre\DAV\Exception\NotAuthenticated</s:exception>
+ <s:message>No public access to this resource., No 'Authorization: Basic' header found. Either the client didn't send one, or the server is misconfigured, No 'Authorization: Bearer' header found. Either the client didn't send one, or the server is mis-configured, No 'Authorization: Basic' header found. Either the client didn't send one, or the server is misconfigured</s:message>
+ </d:error>
+ XML
+ end
+ let(:expected_response) do
+ {
+ status: 401,
+ body: expected_response_body,
+ headers: {}
+ }
+ end
+
+ it 'returns failure' do
+ subject
+ .set_permissions_command
+ .match(
+ on_success: ->(command) do
+ result = command.call(folder: 'JediProject', permissions:)
+ expect(result).to be_failure
+ end,
+ on_failure: ->(error) do
+ raise "Set permissions command could not be executed: #{error}"
+ end
+ )
+ end
+ end
+
+ context 'when the NC control user cannot read(see) the project folder' do
+ let(:expected_response_body) do
+ <<~XML
+ <?xml version="1.0" encoding="utf-8"?>
+ <d:error
+ xmlns:d="DAV:"
+ xmlns:s="http://sabredav.org/ns">
+ <s:exception>Sabre\DAV\Exception\NotFound</s:exception>
+ <s:message>File with name /OpenProject/JediProject could not be located</s:message>
+ </d:error>
+ XML
+ end
+ let(:expected_response) do
+ {
+ status: 404,
+ body: expected_response_body,
+ headers: {}
+ }
+ end
+
+ it 'returns failure' do
+ subject
+ .set_permissions_command
+ .match(
+ on_success: ->(command) do
+ result = command.call(folder: 'JediProject', permissions:)
+ expect(result).to be_failure
+ end,
+ on_failure: ->(error) do
+ raise "Set permissions command could not be executed: #{error}"
+ end
+ )
+ end
+ end
+ end
+ end
+
describe '#download_link_query' do
let(:file_link) do
Struct.new(:file_link) do