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

[#50908] Implement redirect to project folder with waiting under /projects/:project_id/project_storages/:id/open

Pavel Balashoue8cff94979bc
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb
index 1902b27c67d..12f6bdda52e 100644
--- a/app/helpers/application_helper.rb
+++ b/app/helpers/application_helper.rb
@@ -119,7 +119,14 @@ module ApplicationHelper
def render_flash_messages
messages = flash
.reject { |k, _| k.start_with? '_' }
- .map { |k, v| render_flash_message(k, v) }
+ .map do |k, v|
+ if k.to_sym == :modal
+ component = v[:type].constantize
+ component.new(**v[:parameters]).render_in(self)
+ else
+ render_flash_message(k, v)
+ end
+ end
safe_join messages, "\n"
end
diff --git a/frontend/src/stimulus/controllers/dynamic/storages/open-project-storage-modal.controller.js b/frontend/src/stimulus/controllers/dynamic/storages/open-project-storage-modal.controller.js
new file mode 100644
index 00000000000..108e6666a61
--- /dev/null
+++ b/frontend/src/stimulus/controllers/dynamic/storages/open-project-storage-modal.controller.js
@@ -0,0 +1,77 @@
+/*
+ * -- copyright
+ * OpenProject is an open source project management software.
+ * Copyright (C) 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.
+ * ++
+ */
+
+import { Controller } from '@hotwired/stimulus';
+import { renderStreamMessage } from '@hotwired/turbo';
+
+export default class OpenProjectStorageModalController extends Controller {
+ static values = {
+ projectStorageOpenUrl: String,
+ redirectUrl: String,
+ };
+
+ connect() {
+ this.element.open = true;
+ this.interval = 0;
+ this.load();
+ // the following is not enough, because the modal can be closed with Esc when close button in focus
+ const closeButton = this.element.getElementsByClassName("Overlay-closeButton")[0]
+ closeButton.addEventListener('click', () => { this.disconnect() });
+ }
+
+ disconnect() {
+ clearInterval(this.interval);
+ }
+
+ load() {
+ this.interval = setTimeout(
+ async () => {
+ let response = await fetch(
+ this.projectStorageOpenUrlValue,
+ {
+ headers: {
+ 'Accept': 'text/vnd.turbo-stream.html'
+ }
+ }
+ )
+ if (response.status === 200) {
+ let streamActionHTML = await response.text();
+ renderStreamMessage(streamActionHTML);
+ setTimeout(
+ () => { window.location.href = this.redirectUrlValue; },
+ 2000
+ );
+ } else {
+ this.load();
+ }
+ },
+ 3000)
+ }
+}
diff --git a/modules/storages/app/components/storages/open_project_storage_modal_component.html.erb b/modules/storages/app/components/storages/open_project_storage_modal_component.html.erb
new file mode 100644
index 00000000000..4230687862a
--- /dev/null
+++ b/modules/storages/app/components/storages/open_project_storage_modal_component.html.erb
@@ -0,0 +1,11 @@
+<%= render(
+ Primer::Alpha::Dialog.new(
+ id: self.class.dialog_id,
+ title: 'Modal',
+ visually_hide_title: true,
+ data: @data)
+) do |dialog| %>
+ <% dialog.with_body(id: self.class.dialog_body_id) do %>
+ <%= render(self.class::Body.new(@state)) %>
+ <% end %>
+<% end %>
diff --git a/modules/storages/app/components/storages/open_project_storage_modal_component.rb b/modules/storages/app/components/storages/open_project_storage_modal_component.rb
new file mode 100644
index 00000000000..df4584cd065
--- /dev/null
+++ b/modules/storages/app/components/storages/open_project_storage_modal_component.rb
@@ -0,0 +1,47 @@
+# 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::OpenProjectStorageModalComponent < ViewComponent::Base
+ def initialize(project_storage_open_url:, redirect_url:, state:, **options)
+ super
+ controller = 'storages--open-project-storage-modal'
+ @data = {
+ controller:,
+ 'application-target': 'dynamic',
+ "#{controller}-project-storage-open-url-value": project_storage_open_url,
+ "#{controller}-redirect-url-value": redirect_url
+ }
+ @state = state
+ end
+
+ def self.dialog_id
+ 'open-project-storage-modal-component'
+ end
+
+ def self.dialog_body_id
+ 'open-project-storage-modal-body-component'
+ end
+end
diff --git a/modules/storages/app/components/storages/open_project_storage_modal_component/body.html.erb b/modules/storages/app/components/storages/open_project_storage_modal_component/body.html.erb
new file mode 100644
index 00000000000..639a7ae832d
--- /dev/null
+++ b/modules/storages/app/components/storages/open_project_storage_modal_component/body.html.erb
@@ -0,0 +1,29 @@
+<% case @state %>
+<% when :waiting %>
+ <%= render(Primer::OpenProject::FlexLayout.new(align_items: :center)) do |flex| %>
+ <% flex.with_row do %>
+ <div class="op-loading-indicator">
+ <div></div><div></div>
+ </div>
+ <% end %>
+ <% flex.with_row do %>
+ <%= render(Primer::Beta::Text.new) { I18n.t('storages.open_project_storage_modal.waiting.a') } %>
+ <% end %>
+ <% flex.with_row do %>
+ <%= render(Primer::Beta::Text.new) { I18n.t('storages.open_project_storage_modal.waiting.b') } %>
+ <% end %>
+ <% end %>
+<% when :success %>
+ <%= render(Primer::OpenProject::FlexLayout.new(align_items: :center)) do |flex| %>
+ <% flex.with_row do %>
+ <%= render(Primer::Beta::Octicon.new(:"check-circle", size: :medium, color: :success)) %>
+ <% end %>
+ <% flex.with_row do %>
+ <%= render(Primer::Beta::Text.new) { I18n.t('storages.open_project_storage_modal.success.a') } %>
+ <% end %>
+ <% flex.with_row do %>
+ <%= render(Primer::Beta::Text.new) { I18n.t('storages.open_project_storage_modal.success.b') } %>
+ <% end %>
+ <% end %>
+<% end %>
+<!-- Timeout: Permissions were not set within the expected time. Please, contact the administrator. -->
diff --git a/modules/storages/app/components/storages/open_project_storage_modal_component/body.rb b/modules/storages/app/components/storages/open_project_storage_modal_component/body.rb
new file mode 100644
index 00000000000..78b4c8d00fc
--- /dev/null
+++ b/modules/storages/app/components/storages/open_project_storage_modal_component/body.rb
@@ -0,0 +1,31 @@
+# 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::OpenProjectStorageModalComponent::Body < ViewComponent::Base
+ def initialize(state)
+ @state = state
+ end
+end
diff --git a/modules/storages/app/controllers/storages/project_storages_controller.rb b/modules/storages/app/controllers/storages/project_storages_controller.rb
new file mode 100644
index 00000000000..f688efd3f5c
--- /dev/null
+++ b/modules/storages/app/controllers/storages/project_storages_controller.rb
@@ -0,0 +1,102 @@
+#-- 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::ProjectStoragesController < ApplicationController
+ using Storages::Peripherals::ServiceResultRefinements
+
+ menu_item :overview
+ model_object Storages::ProjectStorage
+
+ before_action :require_login
+ before_action :find_model_object, only: %i[open]
+ before_action :find_project_by_project_id
+
+ def open
+ url = @object.open(current_user).match(
+ on_success: ->(url) { url },
+ on_failure: ->(error) { raise_error(error) }
+ )
+
+ modal_params = {
+ project_storage_open_url: request.path,
+ redirect_url: url
+ }
+ storage = @object.storage
+ result = ::Storages::Peripherals::Registry
+ .resolve("queries.#{storage.short_provider_type}.file_info")
+ .call(storage:,
+ user: current_user,
+ file_id: @object.project_folder_id)
+
+ redirect_to url if [email protected]_folder_automatic?
+ result.match(
+ on_success: ->(_) do
+ respond_to do |format|
+ format.turbo_stream do
+ stream = OpTurbo::StreamComponent.new(
+ action: :update,
+ target: Storages::OpenProjectStorageModalComponent.dialog_body_id,
+ template: Storages::OpenProjectStorageModalComponent::Body.new(:success).render_in(view_context)
+ ).render_in(view_context)
+ render turbo_stream: stream
+ end
+ format.html { redirect_to url }
+ end
+ end,
+ on_failure: ->(result) do
+ case result.code
+ when :unauthorized
+ respond_to do |format|
+ format.turbo_stream { head :no_content }
+ format.html do
+ ensure_connection_url = oauth_clients_ensure_connection_url(
+ oauth_client_id: storage.oauth_client.client_id,
+ storage_id: storage.id,
+ destination_url: request.url
+ )
+ redirect_to ensure_connection_url
+ end
+ end
+ when :forbidden
+ respond_to do |format|
+ format.turbo_stream do
+ head :no_content
+ end
+ format.html do
+ flash[:modal] = {
+ type: 'Storages::OpenProjectStorageModalComponent',
+ parameters: modal_params.merge(state: :waiting)
+ }
+ redirect_to project_overview_path(project_id: @project.identifier)
+ end
+ end
+ end
+ end
+ )
+ end
+end
diff --git a/modules/storages/app/models/storages/project_storage.rb b/modules/storages/app/models/storages/project_storage.rb
index efb77649a16..c11062ab7a2 100644
--- a/modules/storages/app/models/storages/project_storage.rb
+++ b/modules/storages/app/models/storages/project_storage.rb
@@ -68,6 +68,18 @@ class Storages::ProjectStorage < ApplicationRecord
escaped_file_path.match?(%r|^/#{project_folder_path_escaped}|)
end
+ def open(user)
+ if project_folder_inactive? || (project_folder_automatic? && !user.allowed_in_project?(:read_files, project))
+ Storages::Peripherals::Registry
+ .resolve("queries.#{storage.short_provider_type}.open_storage")
+ .call(storage:, user:)
+ else
+ Storages::Peripherals::Registry
+ .resolve("queries.#{storage.short_provider_type}.open_file_link")
+ .call(storage:, user:, file_id: project_folder_id)
+ end
+ end
+
private
def escape_path(path)
diff --git a/modules/storages/config/locales/en.yml b/modules/storages/config/locales/en.yml
index ae5b7bfb5de..b986f4c6ed2 100644
--- a/modules/storages/config/locales/en.yml
+++ b/modules/storages/config/locales/en.yml
@@ -245,3 +245,10 @@ en:
automatically_managed_project_folder_missing: "To complete the setup, please configure automatically managed project folders for your storage."
notice_oauth_application_replaced: "The OpenProject OAuth application was successfully replaced."
notice_successful_storage_connection: "Storage connected successfully! Remember to activate the module and the specific storage in the project settings of each desired project to use it."
+ open_project_storage_modal:
+ waiting:
+ a: "We are setting up your permissions on the project folder."
+ b: "One moment please, this might take some time..."
+ success:
+ a: "Integration setup completed"
+ b: "You are being redirected"
diff --git a/modules/storages/config/routes.rb b/modules/storages/config/routes.rb
index f221bdef73f..5f3a0a0adcc 100644
--- a/modules/storages/config/routes.rb
+++ b/modules/storages/config/routes.rb
@@ -50,6 +50,10 @@ OpenProject::Application.routes.draw do
end
end
+ get 'projects/:project_id/project_storages/:id/open',
+ controller: 'storages/project_storages',
+ action: 'open',
+ as: 'open_project_storage'
scope 'projects/:project_id', as: 'project' do
namespace 'settings' do
resources :project_storages, controller: '/storages/admin/project_storages', except: %i[show] do
diff --git a/modules/storages/lib/api/v3/project_storages/project_storage_open_api.rb b/modules/storages/lib/api/v3/project_storages/project_storage_open_api.rb
index b48d22f1bd9..83cc6789d1d 100644
--- a/modules/storages/lib/api/v3/project_storages/project_storage_open_api.rb
+++ b/modules/storages/lib/api/v3/project_storages/project_storage_open_api.rb
@@ -33,24 +33,7 @@ class API::V3::ProjectStorages::ProjectStorageOpenAPI < API::OpenProjectAPI
resources :open do
get do
- query_result = if @project_storage.project_folder_inactive?
- Storages::Peripherals::Registry
- .resolve("queries.#{@project_storage.storage.short_provider_type}.open_storage")
- .call(
- storage: @project_storage.storage,
- user: current_user
- )
- else
- Storages::Peripherals::Registry
- .resolve("queries.#{@project_storage.storage.short_provider_type}.open_file_link")
- .call(
- storage: @project_storage.storage,
- user: current_user,
- file_id: @project_storage.project_folder_id
- )
- end
-
- query_result.match(
+ @project_storage.open(current_user).match(
on_success: ->(url) do
redirect url, body: "The requested resource can be viewed at #{url}"
status 303
diff --git a/modules/storages/lib/open_project/storages/engine.rb b/modules/storages/lib/open_project/storages/engine.rb
index 4d197986ef4..8dcba806c85 100644
--- a/modules/storages/lib/open_project/storages/engine.rb
+++ b/modules/storages/lib/open_project/storages/engine.rb
@@ -159,7 +159,12 @@ module OpenProject::Storages
User.current.allowed_in_project?(:view_file_links, project)
project.project_storages.each do |project_storage|
storage = project_storage.storage
- href = "/api/v3/project_storages/#{project_storage.id}/open"
+ href = Rails
+ .application
+ .routes
+ .url_helpers
+ .open_project_storage_path(project_id: project_storage.project.identifier,
+ id: project_storage.id)
icon = if storage.provider_type_nextcloud?
'nextcloud-circle'
else