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

[#47912] Create a way to trigger the set permissions command

Pavel Balashovd437ce3f32a3
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb
index 269cd465826..eb315c38768 100644
--- a/app/controllers/application_controller.rb
+++ b/app/controllers/application_controller.rb
@@ -302,10 +302,10 @@ class ApplicationController < ActionController::Base
render_404
end
- def find_model_object
+ def find_model_object(object_id = :id)
model = self.class._model_object
if model
- @object = model.find(params[:id])
+ @object = model.find(params[object_id])
instance_variable_set('@' + controller_name.singularize, @object) if @object
end
rescue ActiveRecord::RecordNotFound
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 982080a2748..620a9c6a650 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
@@ -40,13 +40,15 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
share_files: 16
}.freeze
- def initialize(base_uri:, username:, password:)
+ def initialize(storage)
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
+ @uri = URI(storage.host).normalize
+ @base_path = api_v3_paths.join_uri_path(@uri.path, "remote.php/dav/files", escape_whitespace(storage.username))
+ @groupfolder = storage.groupfolder
+ @group = storage.group
+ @username = storage.username
+ @password = storage.password
end
def execute(folder:, permissions:)
@@ -54,8 +56,8 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
http.use_ssl = @uri.scheme == 'https'
response = http.proppatch(
- "#{@base_path}/#{requested_folder(folder)}",
- converted_permissions(username: @username, permissions:),
+ "#{@base_path}/#{@groupfolder}/#{requested_folder(folder)}",
+ converted_permissions(permissions:),
{
'Authorization' => "Basic #{Base64::encode64("#{@username}:#{@password}")}"
}
@@ -94,7 +96,7 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
)
end
- def converted_permissions(username:, permissions:)
+ def converted_permissions(permissions:)
Nokogiri::XML::Builder.new do |xml|
xml['d'].propertyupdate(
'xmlns:d' => 'DAV:',
@@ -103,7 +105,7 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
xml['d'].set do
xml['d'].prop do
xml['nc'].send('acl-list') do
- control_user_permissions(username, xml)
+ control_user_permissions(xml)
control_group_permissions(xml)
user_permissions(xml, permissions)
end
@@ -113,10 +115,10 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
end.to_xml
end
- def control_user_permissions(username, xml)
+ def control_user_permissions(xml)
xml['nc'].acl do
xml['nc'].send('acl-mapping-type', 'user')
- xml['nc'].send('acl-mapping-id', username)
+ xml['nc'].send('acl-mapping-id', @username)
xml['nc'].send('acl-mask', '31')
xml['nc'].send('acl-permissions', '31')
end
@@ -125,7 +127,7 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
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-mapping-id', @group)
xml['nc'].send('acl-mask', '31')
xml['nc'].send('acl-permissions', '0')
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
index ac4e9733471..2f8b3e879e0 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/storage_commands.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/storage_commands.rb
@@ -30,26 +30,13 @@ 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
+ def initialize(storage)
+ @storage = storage
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
+ command_class = "::Storages::Peripherals::StorageInteraction::#{@storage.short_provider_type.capitalize}::SetPermissionsCommand".constantize
+ ServiceResult.success(result: command_class.new(@storage))
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 5fe52c0ec73..9f2aa423b0b 100644
--- a/modules/storages/app/common/storages/peripherals/storage_requests.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_requests.rb
@@ -76,13 +76,7 @@ module Storages::Peripherals
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
- )
+ ::Storages::Peripherals::StorageInteraction::StorageCommands.new(@storage)
end
end
end
diff --git a/modules/storages/app/components/storages/projects_storages/row_component.rb b/modules/storages/app/components/storages/projects_storages/row_component.rb
index 2fd55538803..b184786ea28 100644
--- a/modules/storages/app/components/storages/projects_storages/row_component.rb
+++ b/modules/storages/app/components/storages/projects_storages/row_component.rb
@@ -55,6 +55,7 @@ module Storages::ProjectsStorages
def button_links
[delete_link].tap do |links|
links.unshift edit_link if OpenProject::FeatureDecisions.storage_project_folders_active?
+ links.unshift set_permissions_link if OpenProject::FeatureDecisions.managed_project_folders_active?
end
end
@@ -65,6 +66,15 @@ module Storages::ProjectsStorages
title: I18n.t(:button_edit)
end
+ def set_permissions_link
+ link_to('',
+ project_settings_projects_storage_set_permissions_path(projects_storage_id: project_storage.id,
+ project_id: project_storage.project.id),
+ class: 'icon icon-settings3',
+ data: { confirm: 'Force push project folder permissions?' },
+ title: 'Set permissions')
+ end
+
def delete_link
link_to '',
project_settings_projects_storage_path(project_id: project_storage.project, id: project_storage),
diff --git a/modules/storages/app/controllers/storages/admin/projects_storages_controller.rb b/modules/storages/app/controllers/storages/admin/projects_storages_controller.rb
index d5417a68d50..b046d2b528b 100644
--- a/modules/storages/app/controllers/storages/admin/projects_storages_controller.rb
+++ b/modules/storages/app/controllers/storages/admin/projects_storages_controller.rb
@@ -34,6 +34,7 @@
# Called by: Calls to the controller methods are initiated by user Web GUI
# actions and mapped to this controller by storages/config/routes.rb.
class Storages::Admin::ProjectsStoragesController < Projects::SettingsController
+ using Storages::Peripherals::ServiceResultRefinements
# This is the resource handled in this controller.
# So the controller knows that the ID in params (URl) refer to instances of this model.
# This defines @object as the model instance.
@@ -142,6 +143,37 @@ class Storages::Admin::ProjectsStoragesController < Projects::SettingsController
redirect_to project_settings_projects_storages_path
end
+ def set_permissions
+ if OpenProject::FeatureDecisions.managed_project_folders_active?
+ find_model_object(:projects_storage_id)
+ storage = @projects_storage.storage
+ project = @projects_storage.project
+ command = Storages::Peripherals::StorageRequests.new(storage:).set_permissions_command
+ folder = @projects_storage.project_folder_id
+ project_users = project.users
+ oauth_client = OAuthClient.where(integration_id: @projects_storage.storage_id, integration_type: 'Storages::Storage').first
+ nextcloud_users = OAuthClientToken.where(oauth_client:, user: project_users)
+ permissions = nextcloud_users.map do |token|
+ user = token.user
+ {
+ origin_user_id: token.origin_user_id,
+ permissions: {
+ read_files: user.allowed_to?(:read_files, @project),
+ write_files: user.allowed_to?(:write_files, @project),
+ create_files: user.allowed_to?(:create_files, @project),
+ share_files: user.allowed_to?(:share_files, @project),
+ delete_files: user.allowed_to?(:delete_files, @project)
+ }
+ }
+ end
+
+ command.result.call(folder:, permissions:).match(
+ on_success: ->(_) { flash[:notice] = 'Permissions were successfuly updated on the NextCloud side' },
+ on_failure: ->(error) { flash[:error] = "Error: #{error}" }
+ )
+ end
+ redirect_back(fallback_location: project_settings_projects_storages_path(project_id: project.id))
+ end
private
# Define the list of permitted parameters for creating/updating a ProjectStorage.
diff --git a/modules/storages/app/models/storages/nextcloud_storage.rb b/modules/storages/app/models/storages/nextcloud_storage.rb
index c63d4b81936..de829bc670f 100644
--- a/modules/storages/app/models/storages/nextcloud_storage.rb
+++ b/modules/storages/app/models/storages/nextcloud_storage.rb
@@ -1,8 +1,52 @@
+#-- 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 Storages::NextcloudStorage < Storages::Storage
- store :provider_fields, accessors: %i[username password has_managed_project_folders]
+ store :provider_fields, accessors: %i[username
+ password
+ group
+ groupfolder
+ has_managed_project_folders]
alias_method :has_managed_project_folders?, :has_managed_project_folders
+ def group
+ super || "OpenProject"
+ end
+
+ def groupfolder
+ super || "OpenProject"
+ end
+
+ def username
+ super || "OpenProject"
+ end
+
def has_managed_project_folders=(value)
super(!!value)
end
diff --git a/modules/storages/config/routes.rb b/modules/storages/config/routes.rb
index 0dc103dde65..a84d21f674e 100644
--- a/modules/storages/config/routes.rb
+++ b/modules/storages/config/routes.rb
@@ -40,8 +40,9 @@ OpenProject::Application.routes.draw do
scope 'projects/:project_id', as: 'project' do
namespace 'settings' do
- resources :projects_storages, controller: '/storages/admin/projects_storages',
- except: %i[show]
+ resources :projects_storages, controller: '/storages/admin/projects_storages', except: %i[show] do
+ get 'set_permissions', to: '/storages/admin/projects_storages#set_permissions'
+ end
end
end
end
diff --git a/modules/storages/lib/open_project/storages/engine.rb b/modules/storages/lib/open_project/storages/engine.rb
index 7511efac26c..43dec67fa59 100644
--- a/modules/storages/lib/open_project/storages/engine.rb
+++ b/modules/storages/lib/open_project/storages/engine.rb
@@ -68,7 +68,7 @@ module OpenProject::Storages
dependencies: %i[view_file_links],
contract_actions: { file_links: %i[manage] }
permission :manage_storages_in_project,
- { 'storages/admin/projects_storages': %i[index new edit update create destroy] },
+ { 'storages/admin/projects_storages': %i[index new edit update create destroy set_permissions] },
dependencies: %i[]
if OpenProject::FeatureDecisions.managed_project_folders_active?
diff --git a/modules/storages/spec/common/peripherals/storage_requests_spec.rb b/modules/storages/spec/common/peripherals/storage_requests_spec.rb
index c92ccd832e0..f613e2fac82 100644
--- a/modules/storages/spec/common/peripherals/storage_requests_spec.rb
+++ b/modules/storages/spec/common/peripherals/storage_requests_spec.rb
@@ -37,13 +37,7 @@ describe Storages::Peripherals::StorageRequests, webmock: true do
let(:origin_user_id) { 'admin' }
let(:storage) do
- 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
+ build(:storage, host: url, password: 'OpenProjectSecurePassword')
end
let(:token) do
@@ -129,7 +123,7 @@ describe Storages::Peripherals::StorageRequests, webmock: true do
context 'with Nextcloud storage type selected' do
context 'with outbound request' do
before do
- stub_request(:proppatch, "#{url}/remote.php/dav/files/OpenProject/JediProject")
+ stub_request(:proppatch, "#{url}/remote.php/dav/files/OpenProject/OpenProject/JediProject")
.with(
body: expected_request_body,
headers: {
diff --git a/modules/storages/spec/factories/storage_factory.rb b/modules/storages/spec/factories/storage_factory.rb
index d17bdafe27e..64bce66ac8c 100644
--- a/modules/storages/spec/factories/storage_factory.rb
+++ b/modules/storages/spec/factories/storage_factory.rb
@@ -27,7 +27,7 @@
#++
FactoryBot.define do
- factory :storage, class: '::Storages::Storage' do
+ factory :storage, class: '::Storages::NextcloudStorage' do
provider_type { Storages::Storage::PROVIDER_TYPE_NEXTCLOUD }
sequence(:name) { |n| "Storage #{n}" }
sequence(:host) { |n| "https://host#{n}.example.com" }