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[Op#56922]: extract oauth access grantable concern

Kabiru Mwenja717d0c1277a0
diff --git a/modules/storages/app/controllers/concerns/storages/oauth_access_grantable.rb b/modules/storages/app/controllers/concerns/storages/oauth_access_grantable.rb
new file mode 100644
index 00000000000..820b0efaed8
--- /dev/null
+++ b/modules/storages/app/controllers/concerns/storages/oauth_access_grantable.rb
@@ -0,0 +1,62 @@
+#-- 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 OAuthAccessGrantable
+ extend ActiveSupport::Concern
+
+ def open_redirect_to_storage_authorization_with(callback_url:, storage:)
+ nonce = SecureRandom.uuid
+ cookies["oauth_state_#{nonce}"] = {
+ value: { href: callback_url,
+ storageId: storage.id }.to_json,
+ expires: 1.hour
+ }
+ session[:oauth_callback_flash_modal] = storage_oauth_access_granted_modal(storage:)
+ redirect_to(storage.oauth_configuration.authorization_uri(state: nonce), allow_other_host: true)
+ end
+
+ def storage_oauth_access_granted?(storage:)
+ OAuthClientToken.exists?(user: User.current, oauth_client: storage.oauth_client)
+ end
+
+ def project_storage_oauth_access_grant_nudge_modal(project_storage:)
+ {
+ type: ::Storages::ProjectStorages::OAuthAccessGrantNudgeModalComponent.name,
+ parameters: { project_storage: project_storage.id }
+ }
+ end
+
+ def storage_oauth_access_granted_modal(storage:)
+ {
+ type: ::Storages::Admin::Storages::OAuthAccessGrantedModalComponent.name,
+ parameters: { storage: storage.id }
+ }
+ end
+ end
+end
diff --git a/modules/storages/app/controllers/storages/admin/project_storages_controller.rb b/modules/storages/app/controllers/storages/admin/project_storages_controller.rb
index 931d3fdbd11..bf62970463b 100644
--- a/modules/storages/app/controllers/storages/admin/project_storages_controller.rb
+++ b/modules/storages/app/controllers/storages/admin/project_storages_controller.rb
@@ -27,6 +27,8 @@
#++
class Storages::Admin::ProjectStoragesController < Projects::SettingsController
+ include Storages::OAuthAccessGrantable
+
model_object Storages::ProjectStorage
before_action :find_model_object, only: %i[oauth_access_grant edit update destroy destroy_info]
@@ -70,7 +72,7 @@ class Storages::Admin::ProjectStoragesController < Projects::SettingsController
end
end
- def oauth_access_grant # rubocop:disable Metrics/AbcSize
+ def oauth_access_grant
@project_storage = @object
storage = @project_storage.storage
auth_state = ::Storages::Peripherals::StorageInteraction::Authentication
@@ -79,14 +81,10 @@ class Storages::Admin::ProjectStoragesController < Projects::SettingsController
if auth_state == :connected
redirect_to(external_file_storages_project_settings_project_storages_path)
else
- nonce = SecureRandom.uuid
- cookies["oauth_state_#{nonce}"] = {
- value: { href: external_file_storages_project_settings_project_storages_url(project_id: @project_storage.project_id),
- storageId: @project_storage.storage_id }.to_json,
- expires: 1.hour
- }
- session[:oauth_callback_flash_modal] = storage_oauth_access_granted_modal(storage:)
- redirect_to(storage.oauth_configuration.authorization_uri(state: nonce), allow_other_host: true)
+ open_redirect_to_storage_authorization_with(
+ callback_url: external_file_storages_project_settings_project_storages_url(project_id: @project_storage.project_id),
+ storage: @project_storage.storage
+ )
end
end
@@ -156,36 +154,17 @@ class Storages::Admin::ProjectStoragesController < Projects::SettingsController
end
def redirect_to_project_storages_path_with_oauth_access_grant_confirmation
- if storage_oauth_access_granted?
+ if storage_oauth_access_granted?(storage: @project_storage.storage)
redirect_to external_file_storages_project_settings_project_storages_path
else
redirect_to_project_storages_path_with_nudge_modal
end
end
- def storage_oauth_access_granted?
- OAuthClientToken
- .exists?(user: current_user, oauth_client: @project_storage.storage.oauth_client)
- end
-
def redirect_to_project_storages_path_with_nudge_modal
redirect_to(
external_file_storages_project_settings_project_storages_path,
- flash: { modal: oauth_access_grant_nudge_modal(project_storage: @project_storage) }
+ flash: { modal: project_storage_oauth_access_grant_nudge_modal(project_storage: @project_storage) }
)
end
-
- def oauth_access_grant_nudge_modal(project_storage:)
- {
- type: Storages::ProjectStorages::OAuthAccessGrantNudgeModalComponent.name,
- parameters: { project_storage: project_storage.id }
- }
- end
-
- def storage_oauth_access_granted_modal(storage:)
- {
- type: Storages::Admin::Storages::OAuthAccessGrantedModalComponent.name,
- parameters: { storage: storage.id }
- }
- 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 afc0e37ebf1..bf7b4a7fceb 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
@@ -33,6 +33,7 @@ class Storages::Admin::Storages::ProjectStoragesController < ApplicationControll
include OpTurbo::DialogStreamHelper
include FlashMessagesOutputSafetyHelper
include ApplicationComponentStreams
+ include Storages::OAuthAccessGrantable
layout "admin"
@@ -53,7 +54,7 @@ class Storages::Admin::Storages::ProjectStoragesController < ApplicationControll
def new
respond_with_dialog(
- if storage_oauth_access_granted?
+ if storage_oauth_access_granted?(storage: @storage)
::Storages::Admin::Storages::ProjectsStorageModalComponent.new(
project_storage: @project_storage, last_project_folders: {}
)
@@ -64,14 +65,10 @@ class Storages::Admin::Storages::ProjectStoragesController < ApplicationControll
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_granted_modal_params
- redirect_to(@storage.oauth_configuration.authorization_uri(state: nonce), allow_other_host: true)
+ open_redirect_to_storage_authorization_with(
+ callback_url: admin_settings_storage_project_storages_url(@storage),
+ storage: @storage
+ )
end
def create # rubocop:disable Metrics/AbcSize
@@ -205,10 +202,6 @@ 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
@@ -225,11 +218,4 @@ class Storages::Admin::Storages::ProjectStoragesController < ApplicationControll
respond_with_turbo_streams
false
end
-
- def oauth_access_granted_modal_params
- {
- type: Storages::Admin::Storages::OAuthAccessGrantedModalComponent.name,
- parameters: { storage: @storage.id }
- }
- end
end