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

[#56922] Trigger a Login Dialog when the user has no oauth access grant

Kabiru Mwenja1b4eba69eb14
diff --git a/modules/storages/app/components/storages/admin/oauth_access_grant_nudge_modal_component.rb b/modules/storages/app/components/storages/admin/oauth_access_grant_nudge_modal_component.rb
index 38b3627b175..3a867075577 100644
--- a/modules/storages/app/components/storages/admin/oauth_access_grant_nudge_modal_component.rb
+++ b/modules/storages/app/components/storages/admin/oauth_access_grant_nudge_modal_component.rb
@@ -29,7 +29,7 @@
#++
#
module Storages::Admin
- class OAuthAccessGrantNudgeModalComponent < ApplicationComponent # rubocop:disable OpenProject/AddPreviewForViewComponent
+ class OAuthAccessGrantNudgeModalComponent < ApplicationComponent
options dialog_id: "storages--oauth-grant-nudge-modal-component",
dialog_body_id: "storages--oauth-grant-nudge-modal-body-component",
authorized: false
diff --git a/modules/storages/app/components/storages/admin/storages/oauth_access_grant_nudge_modal_component.html.erb b/modules/storages/app/components/storages/admin/storages/oauth_access_grant_nudge_modal_component.html.erb
new file mode 100644
index 00000000000..266dbbcb06c
--- /dev/null
+++ b/modules/storages/app/components/storages/admin/storages/oauth_access_grant_nudge_modal_component.html.erb
@@ -0,0 +1,94 @@
+<%=
+ component_wrapper do
+ render(
+ Primer::Alpha::Dialog.new(
+ id: dialog_id,
+ title:,
+ data: {
+ 'application-target': 'dynamic',
+ controller: 'storages--oauth-access-grant-nudge-modal',
+ 'storages--oauth-access-grant-nudge-modal-close-button-label-value': I18n.t('button_close'),
+ 'storages--oauth-access-grant-nudge-modal-loading-screen-reader-message-value': waiting_title,
+ },
+ test_selector: 'oauth-access-grant-nudge-modal'
+ )
+ ) do |dialog|
+ dialog.with_header(
+ show_divider: false,
+ role: :alert,
+ aria: { live: :assertive },
+ data: {
+ 'storages--oauth-access-grant-nudge-modal-target': 'header'
+ },
+ visually_hide_title: authorized
+ )
+
+ dialog.with_body(
+ id: dialog_body_id,
+ test_selector: 'oauth-access-grant-nudge-modal-body',
+ aria: { hidden: authorized }
+ ) do
+ concat(
+ render(
+ Primer::Beta::Text.new(
+ display: :none,
+ data: {
+ 'storages--oauth-access-grant-nudge-modal-target': 'loadingIndicator'
+ }
+ )
+ ) { render(Storages::OpenProjectStorageModalComponent::Body.new(:waiting, waiting_title:)) }
+ )
+ concat(
+ render(
+ Primer::Beta::Text.new(
+ data: {
+ 'storages--oauth-access-grant-nudge-modal-target': 'requestAccessBody'
+ }
+ )
+ ) { body_text }
+ )
+ end
+
+ dialog.with_footer(show_divider: false) do
+ concat(
+ render(
+ Primer::Beta::Button.new(
+ scheme: :default,
+ size: :medium,
+ data: {
+ 'close-dialog-id': dialog_id,
+ 'storages--oauth-access-grant-nudge-modal-target': 'closeButton'
+ }
+ )
+ ) { cancel_button_text }
+ )
+
+ unless authorized
+ concat(
+ primer_form_with(
+ model: @project_storage,
+ url: confirm_button_url,
+ method: :get,
+ data: {
+ 'storages--oauth-access-grant-nudge-modal-target': 'requestAccessForm'
+ }
+ ) do |_form|
+ render(
+ Primer::Beta::Button.new(
+ scheme: :primary,
+ size: :medium,
+ type: :submit,
+ aria: { label: confirm_button_aria_label },
+ data: {
+ 'storages--oauth-access-grant-nudge-modal-target': 'requestAccessButton',
+ action: 'click->storages--oauth-access-grant-nudge-modal#requestAccess'
+ }
+ )
+ ) { confirm_button_text }
+ end
+ )
+ end
+ end
+ end
+ end
+%>
diff --git a/modules/storages/app/components/storages/admin/storages/oauth_access_grant_nudge_modal_component.rb b/modules/storages/app/components/storages/admin/storages/oauth_access_grant_nudge_modal_component.rb
new file mode 100644
index 00000000000..fb15a096e3d
--- /dev/null
+++ b/modules/storages/app/components/storages/admin/storages/oauth_access_grant_nudge_modal_component.rb
@@ -0,0 +1,101 @@
+# 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::Admin::Storages
+ class OAuthAccessGrantNudgeModalComponent < ApplicationComponent
+ include OpTurbo::Streamable
+
+ options dialog_id: "storages--oauth-grant-nudge-modal-component",
+ dialog_body_id: "storages--oauth-grant-nudge-modal-body-component",
+ authorized: false
+
+ attr_reader :storage
+
+ def initialize(storage:, **)
+ @storage = find_storage(storage)
+ super(@storage, **)
+ end
+
+ def render?
+ @storage.present?
+ end
+
+ private
+
+ def confirm_button_text
+ I18n.t("storages.oauth_grant_nudge_modal.confirm_button_label")
+ end
+
+ def title
+ if authorized
+ I18n.t("storages.oauth_grant_nudge_modal.access_granted_screen_reader", storage: storage.name)
+ else
+ I18n.t("storages.oauth_grant_nudge_modal.title")
+ end
+ end
+
+ def waiting_title
+ I18n.t("storages.oauth_grant_nudge_modal.requesting_access_to", storage: storage.name)
+ end
+
+ def cancel_button_text
+ if authorized
+ I18n.t("button_close")
+ else
+ I18n.t("storages.oauth_grant_nudge_modal.cancel_button_label")
+ end
+ end
+
+ def body_text
+ if authorized
+ success_title = I18n.t("storages.oauth_grant_nudge_modal.access_granted")
+ success_subtitle = I18n.t("storages.oauth_grant_nudge_modal.storage_ready", storage: storage.name)
+ concat(render(::Storages::OpenProjectStorageModalComponent::Body.new(:success, success_subtitle:, success_title:)))
+ else
+ I18n.t("storages.oauth_grant_nudge_modal.body", storage: storage.name)
+ end
+ end
+
+ def confirm_button_aria_label
+ I18n.t("storages.oauth_grant_nudge_modal.confirm_button_aria_label", storage: storage.name)
+ end
+
+ def confirm_button_url
+ url_helpers.oauth_access_grant_admin_settings_storage_project_storages_path(storage)
+ end
+
+ def find_storage(storage_record_or_id)
+ return if storage_record_or_id.blank?
+ return storage_record_or_id if storage_record_or_id.is_a?(::Storages::Storage)
+
+ ::Storages::Storage.find_by(id: storage_record_or_id)
+ end
+ end
+end
diff --git a/modules/storages/app/controllers/storages/admin/storages/project_storages_controller.rb b/modules/storages/app/controllers/storages/admin/storages/project_storages_controller.rb
index 2bc95d57712..fac0e2de8ca 100644
--- a/modules/storages/app/controllers/storages/admin/storages/project_storages_controller.rb
+++ b/modules/storages/app/controllers/storages/admin/storages/project_storages_controller.rb
@@ -52,11 +52,28 @@ class Storages::Admin::Storages::ProjectStoragesController < ApplicationControll
def index; end
def new
- respond_with_dialog Storages::Admin::Storages::ProjectsStorageModalComponent.new(
- project_storage: @project_storage, last_project_folders: {}
+ respond_with_dialog(
+ if storage_oauth_access_granted?
+ ::Storages::Admin::Storages::ProjectsStorageModalComponent.new(
+ project_storage: @project_storage, last_project_folders: {}
+ )
+ else
+ ::Storages::Admin::Storages::OAuthAccessGrantNudgeModalComponent.new(storage: @storage)
+ end
)
end
+ def oauth_access_grant
+ nonce = SecureRandom.uuid
+ cookies["oauth_state_#{nonce}"] = {
+ value: { href: admin_settings_storage_project_storages_url(@storage),
+ storageId: @storage.id }.to_json,
+ expires: 1.hour
+ }
+ session[:oauth_callback_flash_modal] = oauth_access_grant_nudge_modal(authorized: true)
+ redirect_to(storage.oauth_configuration.authorization_uri(state: nonce), allow_other_host: true)
+ end
+
def create # rubocop:disable Metrics/AbcSize
create_service = ::Storages::ProjectStorages::BulkCreateService
.new(user: current_user, projects: @projects, storage: @storage,
@@ -188,6 +205,10 @@ class Storages::Admin::Storages::ProjectStoragesController < ApplicationControll
.result
end
+ def storage_oauth_access_granted?
+ OAuthClientToken.exists?(user: current_user, oauth_client: @storage.oauth_client)
+ end
+
def include_sub_projects?
ActiveRecord::Type::Boolean.new.cast(params.to_unsafe_h[:storages_project_storage][:include_sub_projects])
end
@@ -204,4 +225,14 @@ class Storages::Admin::Storages::ProjectStoragesController < ApplicationControll
respond_with_turbo_streams
false
end
+
+ def oauth_access_grant_nudge_modal(authorized: false)
+ {
+ type: "Storages::Admin::Storages::OAuthAccessGrantNudgeModalComponent",
+ parameters: {
+ storage: @storage.id,
+ authorized:
+ }
+ }
+ end
end
diff --git a/modules/storages/config/routes.rb b/modules/storages/config/routes.rb
index b26381b7272..bbc6a37a5ef 100644
--- a/modules/storages/config/routes.rb
+++ b/modules/storages/config/routes.rb
@@ -49,6 +49,7 @@ Rails.application.routes.draw do
controller: "/storages/admin/storages/project_storages",
only: %i[index new create edit update destroy] do
get :destroy_confirmation_dialog, on: :member
+ get :oauth_access_grant, on: :collection
end
end