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

Show delete warning for deleting SSO providers (#16981)

Oliver Günther7989cdb55e63
diff --git a/app/models/auth_provider.rb b/app/models/auth_provider.rb
index 769d63d65cc..8a38a4894b0 100644
--- a/app/models/auth_provider.rb
+++ b/app/models/auth_provider.rb
@@ -32,10 +32,16 @@ class AuthProvider < ApplicationRecord
validates :display_name, presence: true
validates :display_name, uniqueness: true
+ after_destroy :unset_direct_provider
+
def self.slug_fragment
raise NotImplementedError
end
+ def user_count
+ @user_count ||= User.where("identity_url LIKE ?", "#{slug}%").count
+ end
+
def human_type
raise NotImplementedError
end
@@ -48,4 +54,12 @@ class AuthProvider < ApplicationRecord
def callback_url
URI.join(auth_url, "callback").to_s
end
+
+ protected
+
+ def unset_direct_provider
+ if Setting.omniauth_direct_login_provider == slug
+ Setting.omniauth_direct_login_provider = ""
+ end
+ end
end
diff --git a/modules/auth_saml/app/components/saml/providers/row_component.rb b/modules/auth_saml/app/components/saml/providers/row_component.rb
index 30f6a3de52f..96c140e7d58 100644
--- a/modules/auth_saml/app/components/saml/providers/row_component.rb
+++ b/modules/auth_saml/app/components/saml/providers/row_component.rb
@@ -53,7 +53,7 @@ module Saml
end
def users
- User.where("identity_url LIKE ?", "#{provider.slug}%").count.to_s
+ provider.user_count.to_s
end
def creator
diff --git a/modules/auth_saml/app/controllers/saml/providers_controller.rb b/modules/auth_saml/app/controllers/saml/providers_controller.rb
index 7574bf18502..641206537bc 100644
--- a/modules/auth_saml/app/controllers/saml/providers_controller.rb
+++ b/modules/auth_saml/app/controllers/saml/providers_controller.rb
@@ -7,7 +7,7 @@ module Saml
before_action :require_admin
before_action :check_ee
- before_action :find_provider, only: %i[show edit import_metadata update destroy]
+ before_action :find_provider, only: %i[show edit import_metadata update confirm_destroy destroy]
before_action :check_provider_writable, only: %i[update import_metadata]
before_action :set_edit_state, only: %i[create edit update import_metadata]
@@ -96,6 +96,8 @@ module Saml
end
end
+ def confirm_destroy; end
+
def destroy
call = ::Saml::Providers::DeleteService
.new(model: @provider, user: User.current)
diff --git a/modules/auth_saml/app/views/saml/providers/confirm_destroy.html.erb b/modules/auth_saml/app/views/saml/providers/confirm_destroy.html.erb
new file mode 100644
index 00000000000..1eb1e53eb0a
--- /dev/null
+++ b/modules/auth_saml/app/views/saml/providers/confirm_destroy.html.erb
@@ -0,0 +1,66 @@
+<%#-- 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.
+
+++#%>
+<%= styled_form_tag(saml_provider_path(@provider),
+ class: 'danger-zone',
+ method: :delete) do %>
+ <section class="form--section">
+ <h3 class="form--section-title">
+ <%= t('saml.delete_title') %>
+ </h3>
+ <p>
+ <%= t('provider.delete_warning.provider', name: content_tag(:strong, @provider.display_name)).html_safe %>
+ </p>
+ <ul class="mb-3">
+ <li> <%= t('provider.delete_warning.delete_result_1') %>
+ <li> <%= t('provider.delete_warning.delete_result_user_count', count: @provider.user_count) %>
+ <% if Setting.omniauth_direct_login_provider == @provider.slug %>
+ <li> <%= t('provider.delete_warning.delete_result_direct') %>
+ <% end %>
+ </ul>
+ <p class="danger-zone--warning">
+ <span class="icon icon-error"></span>
+ <span><%= t('provider.delete_warning.irreversible_notice') %></span>
+ </p>
+ <p>
+ <%= t('provider.delete_warning.input_delete_confirmation', name: "<em class=\"danger-zone--expected-value\">#{h(@provider.display_name)}</em>").html_safe %>
+ </p>
+ <div class="danger-zone--verification">
+ <%= text_field_tag :delete_confirmation %>
+ <%= styled_button_tag title: t(:button_delete), class: '-primary', disabled: true do
+ concat content_tag :i, '', class: 'button--icon icon-delete'
+ concat content_tag :span, t(:button_delete), class: 'button--text'
+ end %>
+ <%= link_to saml_providers_path,
+ title: t(:button_cancel),
+ class: 'button -with-icon icon-cancel' do %>
+ <%= t(:button_cancel) %>
+ <% end %>
+ </div>
+ </section>
+<% end %>
diff --git a/modules/auth_saml/app/views/saml/providers/show.html.erb b/modules/auth_saml/app/views/saml/providers/show.html.erb
index 64d09eaad67..89c532a1a65 100644
--- a/modules/auth_saml/app/views/saml/providers/show.html.erb
+++ b/modules/auth_saml/app/views/saml/providers/show.html.erb
@@ -14,12 +14,8 @@
mobile_icon: :trash,
mobile_label: t(:button_delete),
size: :medium,
- href: saml_provider_path(@provider),
+ href: confirm_destroy_saml_provider_path(@provider),
aria: { label: I18n.t(:button_delete) },
- data: {
- confirm: t(:text_are_you_sure),
- method: :delete,
- },
title: I18n.t(:button_delete)
) do |button|
button.with_leading_visual_icon(icon: :trash)
diff --git a/modules/auth_saml/config/locales/en.yml b/modules/auth_saml/config/locales/en.yml
index 00d78f16416..d7f1e1ff879 100644
--- a/modules/auth_saml/config/locales/en.yml
+++ b/modules/auth_saml/config/locales/en.yml
@@ -33,6 +33,7 @@ en:
unmatched_private_key: "does not belong to the given certificate"
saml:
menu_title: SAML providers
+ delete_title: Delete SAML provider
info:
title: "SAML Protocol Configuration Parameters"
description: >
diff --git a/modules/auth_saml/config/routes.rb b/modules/auth_saml/config/routes.rb
index 926f2427ae9..2645187796f 100644
--- a/modules/auth_saml/config/routes.rb
+++ b/modules/auth_saml/config/routes.rb
@@ -4,6 +4,7 @@ Rails.application.routes.draw do
resources :providers do
member do
post :import_metadata
+ get :confirm_destroy
end
end
end
diff --git a/modules/auth_saml/spec/features/administration/saml_crud_spec.rb b/modules/auth_saml/spec/features/administration/saml_crud_spec.rb
index b1685cbe836..0efcc2763a0 100644
--- a/modules/auth_saml/spec/features/administration/saml_crud_spec.rb
+++ b/modules/auth_saml/spec/features/administration/saml_crud_spec.rb
@@ -33,6 +33,7 @@ RSpec.describe "SAML administration CRUD",
:js,
:with_cuprite do
shared_let(:user) { create(:admin) }
+ let(:danger_zone) { DangerZone.new(page) }
before do
login_as(user)
@@ -105,11 +106,24 @@ RSpec.describe "SAML administration CRUD",
expect(provider.mapping_uid).to eq "uid"
expect(provider.authn_requests_signed).to be true
- accept_confirm do
- click_link_or_button "Delete"
- end
+ click_link_or_button "Delete"
+ # Confirm the deletion
+ # Without confirmation, the button is disabled
+ expect(danger_zone).to be_disabled
+
+ # With wrong confirmation, the button is disabled
+ danger_zone.confirm_with("foo")
+
+ expect(danger_zone).to be_disabled
+
+ # With correct confirmation, the button is enabled
+ # and the project can be deleted
+ danger_zone.confirm_with(provider.display_name)
+ expect(danger_zone).not_to be_disabled
+ danger_zone.danger_button.click
expect(page).to have_text "No SAML providers configured yet."
+ expect { provider.reload }.to raise_error ActiveRecord::RecordNotFound
end
it "can import metadata from XML" do
diff --git a/modules/auth_saml/spec/models/saml/provider_spec.rb b/modules/auth_saml/spec/models/saml/provider_spec.rb
index 3c88b8624d8..c2fee27da4d 100644
--- a/modules/auth_saml/spec/models/saml/provider_spec.rb
+++ b/modules/auth_saml/spec/models/saml/provider_spec.rb
@@ -281,4 +281,16 @@ RSpec.describe Saml::Provider do
it { is_expected.to be false }
end
end
+
+ describe "#destroy" do
+ let(:provider) { create(:saml_provider) }
+
+ it "unsets the setting" do
+ Setting.omniauth_direct_login_provider = provider.slug
+
+ provider.destroy!
+
+ expect(Setting.omniauth_direct_login_provider).to be_blank
+ end
+ end
end
diff --git a/modules/openid_connect/app/components/openid_connect/providers/row_component.rb b/modules/openid_connect/app/components/openid_connect/providers/row_component.rb
index 027d9c91157..0eae5bfaabc 100644
--- a/modules/openid_connect/app/components/openid_connect/providers/row_component.rb
+++ b/modules/openid_connect/app/components/openid_connect/providers/row_component.rb
@@ -45,7 +45,7 @@ module OpenIDConnect
end
def users
- User.where("identity_url LIKE ?", "#{provider.slug}%").count.to_s
+ provider.user_count.to_s
end
def creator
diff --git a/modules/openid_connect/app/controllers/openid_connect/providers_controller.rb b/modules/openid_connect/app/controllers/openid_connect/providers_controller.rb
index ab54f477fff..e2477b25b1e 100644
--- a/modules/openid_connect/app/controllers/openid_connect/providers_controller.rb
+++ b/modules/openid_connect/app/controllers/openid_connect/providers_controller.rb
@@ -7,7 +7,7 @@ module OpenIDConnect
before_action :require_admin
before_action :check_ee
- before_action :find_provider, only: %i[edit update destroy]
+ before_action :find_provider, only: %i[edit update confirm_destroy destroy]
before_action :set_edit_state, only: %i[create edit update]
def index
@@ -63,6 +63,8 @@ module OpenIDConnect
end
end
+ def confirm_destroy; end
+
def destroy
if @provider.destroy
flash[:notice] = I18n.t(:notice_successful_delete)
diff --git a/modules/openid_connect/app/views/openid_connect/providers/confirm_destroy.html.erb b/modules/openid_connect/app/views/openid_connect/providers/confirm_destroy.html.erb
new file mode 100644
index 00000000000..20e95fd78a7
--- /dev/null
+++ b/modules/openid_connect/app/views/openid_connect/providers/confirm_destroy.html.erb
@@ -0,0 +1,66 @@
+<%#-- 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.
+
+++#%>
+<%= styled_form_tag(openid_connect_provider_path(@provider),
+ class: 'danger-zone',
+ method: :delete) do %>
+ <section class="form--section">
+ <h3 class="form--section-title">
+ <%= t('openid_connect.delete_title') %>
+ </h3>
+ <p>
+ <%= t('provider.delete_warning.provider', name: content_tag(:strong, @provider.display_name)).html_safe %>
+ </p>
+ <ul class="mb-3">
+ <li> <%= t('provider.delete_warning.delete_result_1') %>
+ <li> <%= t('provider.delete_warning.delete_result_user_count', count: @provider.user_count) %>
+ <% if Setting.omniauth_direct_login_provider == @provider.slug %>
+ <li> <%= t('provider.delete_warning.delete_result_direct') %>
+ <% end %>
+ </ul>
+ <p class="danger-zone--warning">
+ <span class="icon icon-error"></span>
+ <span><%= t('provider.delete_warning.irreversible_notice') %></span>
+ </p>
+ <p>
+ <%= t('provider.delete_warning.input_delete_confirmation', name: "<em class=\"danger-zone--expected-value\">#{h(@provider.display_name)}</em>").html_safe %>
+ </p>
+ <div class="danger-zone--verification">
+ <%= text_field_tag :delete_confirmation %>
+ <%= styled_button_tag title: t(:button_delete), class: '-primary', disabled: true do
+ concat content_tag :i, '', class: 'button--icon icon-delete'
+ concat content_tag :span, t(:button_delete), class: 'button--text'
+ end %>
+ <%= link_to openid_connect_providers_path,
+ title: t(:button_cancel),
+ class: 'button -with-icon icon-cancel' do %>
+ <%= t(:button_cancel) %>
+ <% end %>
+ </div>
+ </section>
+<% end %>
diff --git a/modules/openid_connect/app/views/openid_connect/providers/edit.html.erb b/modules/openid_connect/app/views/openid_connect/providers/edit.html.erb
index d436f2fb034..186c079beb8 100644
--- a/modules/openid_connect/app/views/openid_connect/providers/edit.html.erb
+++ b/modules/openid_connect/app/views/openid_connect/providers/edit.html.erb
@@ -16,12 +16,8 @@
mobile_icon: :trash,
mobile_label: t(:button_delete),
size: :medium,
- href: openid_connect_provider_path(@provider),
+ href: confirm_destroy_openid_connect_provider_path(@provider),
aria: { label: I18n.t(:button_delete) },
- data: {
- confirm: t(:text_are_you_sure),
- method: :delete,
- },
title: I18n.t(:button_delete)
) do |button|
button.with_leading_visual_icon(icon: :trash)
diff --git a/modules/openid_connect/config/locales/en.yml b/modules/openid_connect/config/locales/en.yml
index 5cf78c3bd80..0b839bf25e6 100644
--- a/modules/openid_connect/config/locales/en.yml
+++ b/modules/openid_connect/config/locales/en.yml
@@ -37,8 +37,22 @@ en:
response_is_not_json: " does not return JSON body."
response_misses_required_attributes: " does not return required attributes. Missing attributes are: %{missing_attributes}."
+ provider:
+ delete_warning:
+ input_delete_confirmation: Enter the provider name %{name} to confirm deletion.
+ irreversible_notice: Deleting an SSO provider is an irreversible action.
+ provider: 'Are you sure you want to delete the SSO provider %{name}? To confirm this action please enter the name of the provider in the field below, this will:'
+ delete_result_1: Remove the provider from the list of available providers.
+ delete_result_user_count:
+ zero: No users are currently using this provider. No further action is required.
+ one: "One user is currently still using this provider. They will need to be re-invited or logging in with another provider."
+ other: "%{count} users are currently still using this provider. They will need to be re-invited or logging in with another provider."
+ delete_result_direct: This provider is marked as a direct login provider. The setting will be removed and users will no longer be redirected to the provider for login.
+
openid_connect:
menu_title: OpenID providers
+ delete_title: "Delete OpenID Connect provider"
+
instructions:
endpoint_url: The endpoint URL given to you by the OpenID Connect provider
metadata_none: I don't have this information
diff --git a/modules/openid_connect/config/routes.rb b/modules/openid_connect/config/routes.rb
index f799801c0a0..aa31fbcf26f 100644
--- a/modules/openid_connect/config/routes.rb
+++ b/modules/openid_connect/config/routes.rb
@@ -3,7 +3,9 @@ Rails.application.routes.draw do
scope :admin do
namespace :openid_connect do
- resources :providers, except: %i[show]
+ resources :providers, except: %i[show] do
+ get :confirm_destroy, on: :member
+ end
end
end
end
diff --git a/modules/openid_connect/spec/features/administration/oidc_custom_crud_spec.rb b/modules/openid_connect/spec/features/administration/oidc_custom_crud_spec.rb
index dee4f64db89..71472304107 100644
--- a/modules/openid_connect/spec/features/administration/oidc_custom_crud_spec.rb
+++ b/modules/openid_connect/spec/features/administration/oidc_custom_crud_spec.rb
@@ -33,6 +33,7 @@ RSpec.describe "OIDC administration CRUD",
:js,
:with_cuprite do
shared_let(:user) { create(:admin) }
+ let(:danger_zone) { DangerZone.new(page) }
before do
login_as(user)
@@ -107,11 +108,24 @@ RSpec.describe "OIDC administration CRUD",
expect(provider.mapping_first_name).to eq "myName"
expect(provider.mapping_last_name).to eq "myLastName"
- accept_confirm do
- click_link_or_button "Delete"
- end
+ click_link_or_button "Delete"
+ # Confirm the deletion
+ # Without confirmation, the button is disabled
+ expect(danger_zone).to be_disabled
+
+ # With wrong confirmation, the button is disabled
+ danger_zone.confirm_with("foo")
+
+ expect(danger_zone).to be_disabled
+
+ # With correct confirmation, the button is enabled
+ # and the project can be deleted
+ danger_zone.confirm_with(provider.display_name)
+ expect(danger_zone).not_to be_disabled
+ danger_zone.danger_button.click
expect(page).to have_text "No OpenID providers configured yet."
+ expect { provider.reload }.to raise_error ActiveRecord::RecordNotFound
end
it "can import metadata from URL", :webmock do