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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
chore[Op#56496]: Introduce flash modal contention via controller concern
Kabiru Mwenja4dbd60a36771
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rbindex 276ef7d8cd0..3ed423a1317 100644--- a/app/controllers/application_controller.rb+++ b/app/controllers/application_controller.rb@@ -52,6 +52,7 @@ class ApplicationController < ActionController::Baseinclude AdditionalUrlHelpersinclude OpenProjectErrorHelperinclude Security::DefaultUrlOptions+ include OpModal::Flashablelayout "base"diff --git a/app/controllers/concerns/op_modal/flashable.rb b/app/controllers/concerns/op_modal/flashable.rbnew file mode 100644index 00000000000..0b6d8a853ed--- /dev/null+++ b/app/controllers/concerns/op_modal/flashable.rb@@ -0,0 +1,49 @@+#-- 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 OpModal+ module Flashable+ extend ActiveSupport::Concern++ included do+ add_flash_types :op_modal+ end++ def flash_op_modal(component:, parameters: {})+ flash[:op_modal] = { component:, parameters: }+ end++ def store_callback_op_modal_flash(component:, parameters: {})+ session[:callback_op_modal] = { component:, parameters: }+ end++ def retrieve_callback_op_modal_flash+ session.delete(:callback_op_modal) if session[:callback_op_modal].present?+ end+ end+enddiff --git a/app/controllers/oauth_clients_controller.rb b/app/controllers/oauth_clients_controller.rbindex 4329c8c0ebe..365fefee171 100644--- a/app/controllers/oauth_clients_controller.rb+++ b/app/controllers/oauth_clients_controller.rb@@ -56,10 +56,9 @@ class OAuthClientsController < ApplicationControllerservice_result = @connection_manager.code_to_token(@code)if service_result.success?- flash[:modal] = session.delete(:oauth_callback_flash_modal) if session[:oauth_callback_flash_modal].present?# Redirect the user to the page that initially wanted to access the OAuth2 resource.# "state" is a nonce that identifies a cookie which holds that page's URL.- redirect_to @redirect_uri+ redirect_to @redirect_uri, op_modal: retrieve_callback_op_modal_flashelse# We got a list of errors from ::OAuthClients::ConnectionManagerset_oauth_errors(service_result)diff --git a/app/helpers/flash_messages_helper.rb b/app/helpers/flash_messages_helper.rbindex 0e8b668c935..421508d044d 100644--- a/app/helpers/flash_messages_helper.rb+++ b/app/helpers/flash_messages_helper.rb@@ -40,9 +40,8 @@ module FlashMessagesHelper.reject { |k, _| k.to_s == "op_primer_flash" }.reject { |k, _| k.start_with? "_" }.map do |k, v|- if k.to_sym == :modal- component = v[:type].constantize- component.new(**v.fetch(:parameters, {})).render_in(self)+ if k.to_sym == :op_modal+ render_op_modal_flash_component(component: v[:component], parameters: v.fetch(:parameters, {}))elserender_flash_message(k, v)end@@ -51,6 +50,12 @@ module FlashMessagesHelpersafe_join messages, "\n"end+ def render_op_modal_flash_component(component:, parameters: {})+ component.constantize.new(**parameters).render_in(self)+ rescue NameError => e+ Rails.logger.error { "Could not render flash component #{component}: #{e.message}" }+ end+def render_flash_message(type, message, html_options = {}) # rubocop:disable Metrics/AbcSizeif type.to_s == "notice"type = "success"diff --git a/lookbook/docs/patterns/09-flash-modal.md.erb b/lookbook/docs/patterns/09-flash-modal.md.erbindex 1829e02d808..83f6c2f4185 100644--- a/lookbook/docs/patterns/09-flash-modal.md.erb+++ b/lookbook/docs/patterns/09-flash-modal.md.erb@@ -12,34 +12,30 @@ To flash a modal, set a `flash[:op_modal]` hash in your controller action. The hEx.```ruby-flash[:op_modal] = {- type: ::Storages::ProjectStorages::OAuthAccessGrantedModalComponent.name,- parameters: { storage: storage.id }-}+flash_op_modal component: ::Storages::ProjectStorages::OAuthAccessGrantedModalComponent.name,+ parameters: { storage: storage.id }```Internally, the `FlashMessagesHelper#render_flash_messages` will select flash messages with the key `:op_modal` and render the modal component with the given parameters.### Flash a modal on callback from external service-When you want to display a modal after a callback request from an external service, set the `:op_modal` properties directly in the `session` in the controller action that performs the open redirect and then extract them in the controller action that handles the callback.+When you want to display a modal after a callback request from an external service, store the op modal component directly in the `session` in the controller action that performs by calling `OpModal::Flashable#store_callback_op_modal_flash` bofore the open redirect and then extract them in the controller action that handles the callback via `OpTurbo::Flashable#retrieve_callback_op_modal_flash`.Ex.```ruby# Controller action that performs the open redirectdef redirect_to_external_service- session[:op_modal] = {- type: ::Storages::ProjectStorages::OAuthAccessGrantedModalComponent.name,- parameters: { storage: storage.id }- }+ store_callback_op_modal_flash component: ::Storages::ProjectStorages::OAuthAccessGrantedModalComponent.name,+ parameters: { storage: storage.id }redirect_to external_service_urlend# Controller action that handles the callbackdef callback_from_external_service- flash[:op_modal] = session.delete(:op_modal)# Handle the callback+ redirect_to some_path, op_modal: retrieve_callback_op_modal_flashend```diff --git a/modules/storages/app/controllers/concerns/storages/oauth_access_grantable.rb b/modules/storages/app/controllers/concerns/storages/oauth_access_grantable.rbindex 64e6830124c..418304f75b8 100644--- a/modules/storages/app/controllers/concerns/storages/oauth_access_grantable.rb+++ b/modules/storages/app/controllers/concerns/storages/oauth_access_grantable.rb@@ -38,12 +38,12 @@ module Storagesexpires: 1.hour}- session[:oauth_callback_flash_modal] = case callback_modal_for- when :storage- storage_oauth_access_granted_modal(storage:)- when :project_storage- project_storage_oauth_access_granted_modal(storage:)- end+ store_callback_op_modal_flash(**(case callback_modal_for+ when :storage+ storage_oauth_access_granted_modal(storage:)+ when :project_storage+ project_storage_oauth_access_granted_modal(storage:)+ end))redirect_to(storage.oauth_configuration.authorization_uri(state: nonce), allow_other_host: true)end@@ -54,21 +54,21 @@ module Storagesdef project_storage_oauth_access_grant_nudge_modal(project_storage:){- type: ::Storages::ProjectStorages::OAuthAccessGrantNudgeModalComponent.name,+ component: ::Storages::ProjectStorages::OAuthAccessGrantNudgeModalComponent.name,parameters: { project_storage: project_storage.id }}enddef project_storage_oauth_access_granted_modal(storage:){- type: ::Storages::ProjectStorages::OAuthAccessGrantedModalComponent.name,+ component: ::Storages::ProjectStorages::OAuthAccessGrantedModalComponent.name,parameters: { storage: storage.id }}enddef storage_oauth_access_granted_modal(storage:){- type: ::Storages::Admin::Storages::OAuthAccessGrantedModalComponent.name,+ component: ::Storages::Admin::Storages::OAuthAccessGrantedModalComponent.name,parameters: { storage: storage.id }}enddiff --git a/modules/storages/app/controllers/storages/admin/project_storages_controller.rb b/modules/storages/app/controllers/storages/admin/project_storages_controller.rbindex 1dee1094775..d5eece1e70a 100644--- a/modules/storages/app/controllers/storages/admin/project_storages_controller.rb+++ b/modules/storages/app/controllers/storages/admin/project_storages_controller.rb@@ -165,7 +165,7 @@ class Storages::Admin::ProjectStoragesController < Projects::SettingsControllerdef redirect_to_project_storages_path_with_nudge_modalredirect_to(external_file_storages_project_settings_project_storages_path,- flash: { modal: project_storage_oauth_access_grant_nudge_modal(project_storage: @project_storage) }+ op_modal: project_storage_oauth_access_grant_nudge_modal(project_storage: @project_storage))endenddiff --git a/modules/storages/app/controllers/storages/project_storages_controller.rb b/modules/storages/app/controllers/storages/project_storages_controller.rbindex cb3b738e4d3..ec93ed41a42 100644--- a/modules/storages/app/controllers/storages/project_storages_controller.rb+++ b/modules/storages/app/controllers/storages/project_storages_controller.rb@@ -118,14 +118,12 @@ class Storages::ProjectStoragesController < ApplicationControllerdef redirect_to_project_overview_with_modalredirect_to(project_overview_path(project_id: @project.identifier),- flash: {- modal: {- type: "Storages::OpenProjectStorageModalComponent",- parameters: {- project_storage_open_url: request.path,- redirect_url: api_v3_project_storage_open,- state: :waiting- }+ op_modal: {+ component: "Storages::OpenProjectStorageModalComponent",+ parameters: {+ project_storage_open_url: request.path,+ redirect_url: api_v3_project_storage_open,+ state: :waiting}})diff --git a/modules/storages/spec/requests/project_storages_open_spec.rb b/modules/storages/spec/requests/project_storages_open_spec.rbindex 4baeb7b0f06..c4e5099e7e3 100644--- a/modules/storages/spec/requests/project_storages_open_spec.rb+++ b/modules/storages/spec/requests/project_storages_open_spec.rb@@ -113,8 +113,8 @@ RSpec.describe "projects/:project_id/project_storages/:id/open" doexpect(last_response.headers["Location"]).to eq("http://#{Setting.host_name}/projects/#{project.identifier}")expect(last_request.session["flash"]["flashes"]).to eq({- "modal" => {- type: "Storages::OpenProjectStorageModalComponent",+ "op_modal" => {+ component: "Storages::OpenProjectStorageModalComponent",parameters: { project_storage_open_url: "/projects/#{project.identifier}/project_storages/#{project_storage.id}/open",redirect_url: expected_redirect_path,state: :waiting }@@ -144,8 +144,8 @@ RSpec.describe "projects/:project_id/project_storages/:id/open" doexpect(last_response.headers["Location"]).to eq("http://#{Setting.host_name}/projects/#{project.identifier}")expect(last_request.session["flash"]["flashes"]).to eq({- "modal" => {- type: "Storages::OpenProjectStorageModalComponent",+ "op_modal" => {+ component: "Storages::OpenProjectStorageModalComponent",parameters: { project_storage_open_url: "/projects/#{project.identifier}/project_storages/#{project_storage.id}/open",redirect_url: expected_redirect_path,state: :waiting }