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

Differentiate between form edit mode and single edits

Oliver Günther9bebddadeb35
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 075ba237f51..b2503096c44 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -1450,6 +1450,7 @@ en:
button_expand_all: "Expand all"
button_favorite: "Add to favorites"
button_filter: "Filter"
+ button_finish_setup: "Finish setup"
button_generate: "Generate"
button_list: "List"
button_lock: "Lock"
diff --git a/modules/auth_saml/app/components/saml/providers/sections/form_component.html.erb b/modules/auth_saml/app/components/saml/providers/sections/form_component.html.erb
index 64490434c0f..dfab890d718 100644
--- a/modules/auth_saml/app/components/saml/providers/sections/form_component.html.erb
+++ b/modules/auth_saml/app/components/saml/providers/sections/form_component.html.erb
@@ -4,6 +4,7 @@
model: provider,
url:,
method: form_method,
+ data: { turbo: true, turbo_stream: true }
) do |form|
flex_layout do |flex|
if @heading
@@ -19,8 +20,11 @@
end
flex.with_row(mt: 4) do
- render(Saml::Providers::SubmitOrCancelForm.new(form,
- provider:))
+ render(Saml::Providers::SubmitOrCancelForm.new(
+ form,
+ provider:,
+ submit_button_options: { label: button_label }
+ ))
end
end
end
diff --git a/modules/auth_saml/app/components/saml/providers/sections/form_component.rb b/modules/auth_saml/app/components/saml/providers/sections/form_component.rb
index bcf8541645a..98e854d4ccf 100644
--- a/modules/auth_saml/app/components/saml/providers/sections/form_component.rb
+++ b/modules/auth_saml/app/components/saml/providers/sections/form_component.rb
@@ -30,19 +30,23 @@
#
module Saml::Providers::Sections
class FormComponent < SectionComponent
- def initialize(provider, edit_state:, form_class:, heading:)
+ attr_reader :edit_state, :next_edit_state, :edit_mode
+
+ def initialize(provider, edit_state:, form_class:, heading:, next_edit_state: nil, edit_mode: nil)
super(provider)
@edit_state = edit_state
+ @next_edit_state = next_edit_state
+ @edit_mode = edit_mode
@form_class = form_class
@heading = heading
end
def url
if provider.new_record?
- saml_providers_path(state: @edit_state)
+ saml_providers_path(edit_state:, edit_mode:, next_edit_state:)
else
- saml_provider_path(provider, state: @edit_state)
+ saml_provider_path(provider, edit_state:, edit_mode:, next_edit_state:)
end
end
@@ -53,5 +57,13 @@ module Saml::Providers::Sections
:put
end
end
+
+ def button_label
+ if edit_mode
+ I18n.t(:button_continue)
+ else
+ I18n.t(:button_save)
+ end
+ end
end
end
diff --git a/modules/auth_saml/app/components/saml/providers/sections/metadata_form_component.html.erb b/modules/auth_saml/app/components/saml/providers/sections/metadata_form_component.html.erb
index 26ee6189d80..d648e641bf3 100644
--- a/modules/auth_saml/app/components/saml/providers/sections/metadata_form_component.html.erb
+++ b/modules/auth_saml/app/components/saml/providers/sections/metadata_form_component.html.erb
@@ -2,7 +2,7 @@
primer_form_with(
model: provider,
id: "saml-providers-edit-form",
- url: import_metadata_saml_provider_path(provider),
+ url: import_metadata_saml_provider_path(provider, edit_mode:),
data: {
controller: "show-when-value-selected"
},
@@ -36,9 +36,11 @@
end
flex.with_row(mt: 4) do
- render(Saml::Providers::SubmitOrCancelForm.new(form,
- provider:,
- state: :metadata))
+ render(Saml::Providers::SubmitOrCancelForm.new(
+ form,
+ provider:,
+ submit_button_options: { label: button_label },
+ state: :metadata))
end
end
end
diff --git a/modules/auth_saml/app/components/saml/providers/sections/metadata_form_component.rb b/modules/auth_saml/app/components/saml/providers/sections/metadata_form_component.rb
index 516a559e0b4..11d114d6390 100644
--- a/modules/auth_saml/app/components/saml/providers/sections/metadata_form_component.rb
+++ b/modules/auth_saml/app/components/saml/providers/sections/metadata_form_component.rb
@@ -29,6 +29,9 @@
#++
#
module Saml::Providers::Sections
- class MetadataFormComponent < SectionComponent
+ class MetadataFormComponent < FormComponent
+ def initialize(provider, edit_mode: nil)
+ super(provider, edit_state: :metadata, edit_mode:, form_class: nil, heading: nil)
+ end
end
end
diff --git a/modules/auth_saml/app/components/saml/providers/sections/request_attributes_form_component.rb b/modules/auth_saml/app/components/saml/providers/sections/request_attributes_form_component.rb
new file mode 100644
index 00000000000..e0a736b2cb5
--- /dev/null
+++ b/modules/auth_saml/app/components/saml/providers/sections/request_attributes_form_component.rb
@@ -0,0 +1,49 @@
+# 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 Saml::Providers::Sections
+ class RequestAttributesFormComponent < FormComponent
+ def initialize(provider, edit_mode: nil)
+ super(provider,
+ edit_state: :requested_attributes,
+ edit_mode:,
+ form_class: Saml::Providers::RequestAttributesForm,
+ heading: I18n.t("saml.instructions.requested_attributes"))
+ end
+
+ def button_label
+ if edit_mode
+ I18n.t(:button_finish_setup)
+ else
+ I18n.t(:button_save)
+ end
+ end
+ end
+end
diff --git a/modules/auth_saml/app/components/saml/providers/view_component.html.erb b/modules/auth_saml/app/components/saml/providers/view_component.html.erb
index 7a5f055091b..c8eaf5dc8ab 100644
--- a/modules/auth_saml/app/components/saml/providers/view_component.html.erb
+++ b/modules/auth_saml/app/components/saml/providers/view_component.html.erb
@@ -18,6 +18,8 @@
provider,
form_class: Saml::Providers::NameInputForm,
edit_state:,
+ next_edit_state: :metadata,
+ edit_mode:,
heading: nil
))
else
@@ -37,7 +39,10 @@
component.with_row(scheme: :default) do
if edit_state == :metadata
- render(Saml::Providers::Sections::MetadataFormComponent.new(provider))
+ render(Saml::Providers::Sections::MetadataFormComponent.new(
+ provider,
+ edit_mode:,
+ ))
else
render(Saml::Providers::Sections::ShowComponent.new(
provider,
@@ -61,6 +66,8 @@
provider,
form_class: Saml::Providers::ConfigurationForm,
edit_state:,
+ next_edit_state: :encryption,
+ edit_mode:,
heading: t("saml.providers.section_texts.configuration_form")
))
else
@@ -81,6 +88,8 @@
provider,
form_class: Saml::Providers::EncryptionForm,
edit_state:,
+ next_edit_state: :mapping,
+ edit_mode:,
heading: t("saml.providers.section_texts.encryption_form")
))
else
@@ -104,6 +113,8 @@
provider,
form_class: Saml::Providers::MappingForm,
edit_state:,
+ next_edit_state: :requested_attributes,
+ edit_mode:,
heading: t("saml.instructions.mapping")
))
else
@@ -120,11 +131,9 @@
end
component.with_row(scheme: :default) do
if edit_state == :requested_attributes
- render(Saml::Providers::Sections::FormComponent.new(
+ render(Saml::Providers::Sections::RequestAttributesFormComponent.new(
provider,
- form_class: Saml::Providers::RequestAttributesForm,
- edit_state:,
- heading: t("saml.instructions.requested_attributes")
+ edit_mode:
))
else
render(Saml::Providers::Sections::ShowComponent.new(
diff --git a/modules/auth_saml/app/components/saml/providers/view_component.rb b/modules/auth_saml/app/components/saml/providers/view_component.rb
index 75cc6121ee8..a909c7f651f 100644
--- a/modules/auth_saml/app/components/saml/providers/view_component.rb
+++ b/modules/auth_saml/app/components/saml/providers/view_component.rb
@@ -33,7 +33,7 @@ module Saml::Providers
include OpTurbo::Streamable
include OpPrimer::ComponentHelpers
- options :view_mode, :edit_state
+ options :view_mode, :edit_state, :edit_mode
alias_method :provider, :model
end
diff --git a/modules/auth_saml/app/controllers/saml/providers_controller.rb b/modules/auth_saml/app/controllers/saml/providers_controller.rb
index e9c225f09f5..9d71804f14a 100644
--- a/modules/auth_saml/app/controllers/saml/providers_controller.rb
+++ b/modules/auth_saml/app/controllers/saml/providers_controller.rb
@@ -9,18 +9,18 @@ module Saml
before_action :check_ee
before_action :find_provider, only: %i[show edit import_metadata update destroy]
before_action :check_provider_writable, only: %i[update import_metadata]
+ before_action :set_edit_state, only: %i[create edit update import_metadata]
def index
@providers = Saml::Provider.order(display_name: :asc)
end
def edit
- @edit_state = params[:edit_state].to_sym if params.key?(:edit_state)
-
respond_to do |format|
format.turbo_stream do
component = Saml::Providers::ViewComponent.new(@provider,
view_mode: :edit,
+ edit_mode: @edit_mode,
edit_state: @edit_state)
update_via_turbo_stream(component:)
scroll_into_view_via_turbo_stream("saml-providers-edit-form", behavior: :instant)
@@ -49,9 +49,14 @@ module Saml
def import_metadata
if params[:saml_provider][:metadata] != "none"
update_provider_metadata
+ return if performed?
end
- redirect_to edit_saml_provider_path(@provider, edit_state: :configuration) unless performed?
+ if @edit_mode
+ redirect_to edit_saml_provider_path(@provider, edit_mode: @edit_mode, edit_state: :configuration)
+ else
+ redirect_to saml_provider_path(@provider)
+ end
end
def create
@@ -59,12 +64,13 @@ module Saml
.new(user: User.current)
.call(**create_params)
+ @provider = call.result
+ binding.pry
+
if call.success?
- flash[:notice] = I18n.t(:notice_successful_create)
- redirect_to edit_saml_provider_path(call.result, edit_state: :metadata)
+ successful_save_response
else
flash[:error] = call.message
- @provider = call.result
render action: :new
end
end
@@ -76,10 +82,9 @@ module Saml
if call.success?
flash[:notice] = I18n.t(:notice_successful_update)
- redirect_to saml_provider_path(call.result)
+ successful_save_response
else
@provider = call.result
- @edit_state = params[:state].to_sym
render action: :edit
end
end
@@ -100,18 +105,26 @@ module Saml
private
- def success_redirect
- if params[:edit_state].present?
- redirect_to edit_saml_provider_path(@provider, edit_state: params[:edit_state])
- else
- redirect_to saml_provider_path(@provider)
+ def successful_save_response
+ respond_to do |format|
+ format.turbo_stream do
+ component = Saml::Providers::ViewComponent.new(@provider,
+ edit_mode: @edit_mode,
+ edit_state: @next_edit_state,
+ view_mode: :show)
+ update_via_turbo_stream(component:)
+ render turbo_stream: turbo_streams
+ end
+ format.html do
+ if @edit_mode && @next_edit_state
+ redirect_to edit_saml_provider_path(@provider, edit_mode: true, edit_state: @next_edit_state)
+ else
+ redirect_to saml_provider_path(@provider)
+ end
+ end
end
end
- def defaults
- {}
- end
-
def check_ee
unless EnterpriseToken.allows_to?(:openid_providers)
render template: "/saml/providers/upsale"
@@ -195,5 +208,11 @@ module Saml
redirect_to saml_provider_path(@provider)
end
end
+
+ def set_edit_state
+ @edit_state = params[:edit_state].to_sym if params.key?(:edit_state)
+ @edit_mode = ActiveRecord::Type::Boolean.new.cast(params[:edit_mode])
+ @next_edit_state = params[:next_edit_state].to_sym if params.key?(:next_edit_state)
+ end
end
end
diff --git a/modules/auth_saml/app/views/saml/providers/edit.html.erb b/modules/auth_saml/app/views/saml/providers/edit.html.erb
index d4aee70f933..09f47ebefb2 100644
--- a/modules/auth_saml/app/views/saml/providers/edit.html.erb
+++ b/modules/auth_saml/app/views/saml/providers/edit.html.erb
@@ -20,4 +20,7 @@
<% end %>
<% end %>
-<%= render(Saml::Providers::ViewComponent.new(@provider, view_mode: :edit, edit_state: @edit_state)) %>
+<%= render(Saml::Providers::ViewComponent.new(@provider,
+ view_mode: :edit,
+ edit_mode: @edit_mode,
+ edit_state: @edit_state)) %>
diff --git a/modules/auth_saml/app/views/saml/providers/new.html.erb b/modules/auth_saml/app/views/saml/providers/new.html.erb
index 9826181dbdf..8c66e670f24 100644
--- a/modules/auth_saml/app/views/saml/providers/new.html.erb
+++ b/modules/auth_saml/app/views/saml/providers/new.html.erb
@@ -18,4 +18,4 @@
<% end %>
<% end %>
-<%= render(Saml::Providers::ViewComponent.new(@provider, edit_state: :name)) %>
+<%= render(Saml::Providers::ViewComponent.new(@provider, edit_mode: true, edit_state: :name)) %>