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

[#49396] Nudge admin to go through OAuth flow

Kabiru Mwenjab80db42dde50
diff --git a/app/services/oauth_clients/connection_manager.rb b/app/services/oauth_clients/connection_manager.rb
index 4842b08121a..ac0d6e70462 100644
--- a/app/services/oauth_clients/connection_manager.rb
+++ b/app/services/oauth_clients/connection_manager.rb
@@ -170,6 +170,12 @@ module OAuthClients
:error
end
+ %i[connected failed_authorization error].each do |authorization_result|
+ define_method(:"authorization_state_#{authorization_result}?") do
+ authorization_state == authorization_result
+ end
+ end
+
# @returns ServiceResult with result to be :error or any type of object with data
def request_with_token_refresh(oauth_client_token)
# `yield` needs to returns a ServiceResult:
diff --git a/frontend/src/stimulus/controllers/dynamic/storages/oauth-access-grant-nudge-modal.controller.ts b/frontend/src/stimulus/controllers/dynamic/storages/oauth-access-grant-nudge-modal.controller.ts
new file mode 100644
index 00000000000..60c06ada963
--- /dev/null
+++ b/frontend/src/stimulus/controllers/dynamic/storages/oauth-access-grant-nudge-modal.controller.ts
@@ -0,0 +1,38 @@
+/*
+ * -- 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 { ModalDialogElement } from '@openproject/primer-view-components/app/components/primer/alpha/modal_dialog';
+
+export default class OAuthAccessGrantNudgeModalController extends Controller<ModalDialogElement> {
+ connect() {
+ this.element.open = true;
+ }
+}
diff --git a/modules/storages/app/components/storages/admin/oauth_access_grant_nudge_modal_component.html.erb b/modules/storages/app/components/storages/admin/oauth_access_grant_nudge_modal_component.html.erb
new file mode 100644
index 00000000000..fed79dfc586
--- /dev/null
+++ b/modules/storages/app/components/storages/admin/oauth_access_grant_nudge_modal_component.html.erb
@@ -0,0 +1,38 @@
+<%=
+ render(
+ Primer::Alpha::Dialog.new(
+ id: dialog_id,
+ title:,
+ data: {
+ 'application-target': 'dynamic',
+ controller: 'storages--oauth-access-grant-nudge-modal'
+ }
+ )
+ ) do |dialog|
+ dialog.with_body(id: dialog_body_id) { body_text }
+
+ dialog.with_footer(show_divider: false) do
+ concat(
+ render(
+ Primer::Beta::Button.new(
+ scheme: :default,
+ size: :medium,
+ aria: { label: cancel_button_text },
+ data: { 'close-dialog-id': dialog_id }
+ )
+ ) { cancel_button_text }
+ )
+
+ concat(
+ render(
+ Primer::Beta::Button.new(
+ scheme: :primary,
+ tag: :a,
+ href: confirm_button_url,
+ aria: { label: confirm_button_text }
+ )
+ ) { confirm_button_text }
+ )
+ end
+ end
+%>
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
new file mode 100644
index 00000000000..451bc8e1aa6
--- /dev/null
+++ b/modules/storages/app/components/storages/admin/oauth_access_grant_nudge_modal_component.rb
@@ -0,0 +1,56 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 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
+ class OAuthAccessGrantNudgeModalComponent < ApplicationComponent # rubocop:disable OpenProject/AddPreviewForViewComponent
+ options title: I18n.t('storages.oauth_grant_nudge_modal.title'),
+ body_text: I18n.t('storages.oauth_grant_nudge_modal.body'),
+ dialog_id: 'storages--oauth-grant-nudge-modal-component',
+ dialog_body_id: 'storages--oauth-grant-nudge-modal-body-component',
+ cancel_button_text: I18n.t('storages.oauth_grant_nudge_modal.cancel_button_label'),
+ confirm_button_text: I18n.t('storages.oauth_grant_nudge_modal.confirm_button_label')
+
+ attr_reader :project_storage
+
+ def initialize(project_storage_id:, **options)
+ @project_storage = ::Storages::ProjectStorage.find(project_storage_id)
+ super(@project_storage, **options)
+ end
+
+ private
+
+ def confirm_button_url
+ url_helpers.oauth_access_grant_project_settings_project_storage_path(
+ project_id: project_storage.project.id,
+ id: project_storage
+ )
+ 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 285311a24b9..359b66244db 100644
--- a/modules/storages/app/controllers/storages/admin/project_storages_controller.rb
+++ b/modules/storages/app/controllers/storages/admin/project_storages_controller.rb
@@ -39,7 +39,7 @@ class Storages::Admin::ProjectStoragesController < Projects::SettingsController
# This defines @object as the model instance.
model_object Storages::ProjectStorage
- before_action :find_model_object, only: %i[edit update destroy destroy_info] # Fill @object with ProjectStorage
+ before_action :find_model_object, only: %i[oauth_access_grant edit update destroy destroy_info]
# No need to before_action :find_project_by_project_id as SettingsController already checks
# No need to check for before_action :authorize, as the SettingsController already checks this.
@@ -79,23 +79,40 @@ class Storages::Admin::ProjectStoragesController < Projects::SettingsController
# Create a new ProjectStorage object.
# Called by: The new page above with form-data from that form.
def create
- # Check params and overwrite creator_id and project_id in untrusted data from the Internet
- # @project was calculated by before_action :find_optional_project.
service_result = ::Storages::ProjectStorages::CreateService
.new(user: current_user)
.call(permitted_storage_settings_params)
+ @project_storage = service_result.result
- # Create success/error messages to the user
if service_result.success?
flash[:notice] = I18n.t(:notice_successful_create)
- redirect_to project_settings_project_storages_path
+ redirect_to_project_storages_path_with_oauth_access_grant_confirmation
else
- @project_storage = service_result.result
@available_storages = available_storages
render '/storages/project_settings/new'
end
end
+ def oauth_access_grant # rubocop:disable Metrics/AbcSize
+ @project_storage = @object
+ connection_manager = OAuthClients::ConnectionManager.new(
+ user: current_user,
+ configuration: @project_storage.storage.oauth_configuration
+ )
+
+ if connection_manager.authorization_state_connected?
+ redirect_to(project_settings_project_storages_path)
+ else
+ nonce = SecureRandom.uuid
+ cookies["oauth_state_#{nonce}"] = {
+ value: { href: project_settings_project_storages_url(project_id: @project_storage.project_id),
+ projectStorageId: @project_storage.id }.to_json,
+ expires: 1.hour
+ }
+ redirect_to(connection_manager.get_authorization_uri(state: nonce))
+ end
+ end
+
# Edit page is very similar to new page, except that we don't need to set
# default attribute values because the object already exists
# Called by: Global app/config/routes.rb to serve Web page
@@ -171,4 +188,32 @@ class Storages::Admin::ProjectStoragesController < Projects::SettingsController
.not_enabled_for_project(@project)
.select(&:configured?)
end
+
+ def redirect_to_project_storages_path_with_oauth_access_grant_confirmation
+ if storage_oauth_access_granted?
+ redirect_to project_storages_path
+ else
+ redirect_to_project_storages_path_with_nudge_modal
+ end
+ end
+
+ def storage_oauth_access_granted?
+ OAuthClientToken
+ .find_by(user: current_user, oauth_client: @project_storage.storage.oauth_client)
+ .present?
+ end
+
+ def redirect_to_project_storages_path_with_nudge_modal
+ redirect_to(
+ project_settings_project_storages_path,
+ flash: {
+ modal: {
+ type: 'Storages::Admin::OAuthAccessGrantNudgeModalComponent',
+ parameters: {
+ project_storage_id: Storages::ProjectStorage.last.id
+ }
+ }
+ }
+ )
+ end
end
diff --git a/modules/storages/config/locales/en.yml b/modules/storages/config/locales/en.yml
index 9da518b1e4a..7269fee7586 100644
--- a/modules/storages/config/locales/en.yml
+++ b/modules/storages/config/locales/en.yml
@@ -175,6 +175,11 @@ en:
members_no_results: No members to display.
no_results: No storages set up yet.
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.
+ oauth_grant_nudge_modal:
+ body: You have successfully added a storage to this project. Would you like to login in the storage and authenticate your user to start using the storage?
+ cancel_button_label: I will do it later
+ confirm_button_label: 'Yes'
+ title: Storage added
open_project_storage_modal:
success:
a: Integration setup completed
diff --git a/modules/storages/config/routes.rb b/modules/storages/config/routes.rb
index 7f32a79a42f..1f8d59949fc 100644
--- a/modules/storages/config/routes.rb
+++ b/modules/storages/config/routes.rb
@@ -59,10 +59,12 @@ OpenProject::Application.routes.draw do
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
member do
+ get :oauth_access_grant
# Destroy uses a get request to prompt the user before the actual DELETE request
get :destroy_info, as: 'confirm_destroy'
end
diff --git a/modules/storages/lib/open_project/storages/engine.rb b/modules/storages/lib/open_project/storages/engine.rb
index 232be726201..8aaa33643f5 100644
--- a/modules/storages/lib/open_project/storages/engine.rb
+++ b/modules/storages/lib/open_project/storages/engine.rb
@@ -124,7 +124,7 @@ module OpenProject::Storages
contract_actions: { file_links: %i[manage] }
permission :manage_storages_in_project,
{ 'storages/admin/project_storages': %i[index members new
- edit update create
+ edit update create oauth_access_grant
destroy destroy_info set_permissions],
'storages/project_settings/project_storage_members': %i[index] },
permissible_on: :project,