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

[37136] Disable delete button and check for deletion (#9338)

Oliver Güntherfc090ac02466
diff --git a/app/cells/placeholder_users/row_cell.rb b/app/cells/placeholder_users/row_cell.rb
index 6062efe3b21..4cecc448d61 100644
--- a/app/cells/placeholder_users/row_cell.rb
+++ b/app/cells/placeholder_users/row_cell.rb
@@ -32,6 +32,8 @@ module PlaceholderUsers
class RowCell < ::RowCell
include AvatarHelper
include UsersHelper
+ include PlaceholderUsersHelper
+ include TooltipHelper
def placeholder_user
model
@@ -46,15 +48,12 @@ module PlaceholderUsers
end
def delete_link
- if PlaceholderUsers::DeleteContract.deletion_allowed?(placeholder_user,
- User.current,
- table.user_allowed_service)
-
+ if can_delete_placeholder_user?(placeholder_user, User.current)
link_to deletion_info_placeholder_user_path(placeholder_user) do
- "<span class=\"tooltip--left\" data-tooltip=\"#{I18n.t('placeholder_users.delete_tooltip')}\"><i class=\"icon icon-delete\"></i></span>".html_safe
+ tooltip_tag I18n.t('placeholder_users.delete_tooltip'), icon: 'icon-delete'
end
else
- "<span class=\"tooltip--left\" data-tooltip=\"#{I18n.t('placeholder_users.right_to_manage_members_missing')}\"><i class=\"icon icon-help2\"></i></span>".html_safe
+ tooltip_tag I18n.t('placeholder_users.right_to_manage_members_missing'), icon: 'icon-help2'
end
end
diff --git a/app/controllers/placeholder_users_controller.rb b/app/controllers/placeholder_users_controller.rb
index fcce9e2800c..45d8cb66272 100644
--- a/app/controllers/placeholder_users_controller.rb
+++ b/app/controllers/placeholder_users_controller.rb
@@ -41,6 +41,9 @@ class PlaceholderUsersController < ApplicationController
deletion_info
destroy]
+ before_action :authorize_deletion, only: %i[deletion_info destroy]
+
+
def index
@placeholder_users = PlaceholderUsers::PlaceholderUserFilterCell.query params
@@ -149,6 +152,12 @@ class PlaceholderUsersController < ApplicationController
protected
+ def authorize_deletion
+ unless helpers.can_delete_placeholder_user?(@placeholder_user, current_user)
+ render_403 message: I18n.t('placeholder_users.right_to_manage_members_missing')
+ end
+ end
+
def default_breadcrumb
if action_name == 'index'
t('label_placeholder_user_plural')
diff --git a/app/helpers/placeholder_users_helper.rb b/app/helpers/placeholder_users_helper.rb
new file mode 100644
index 00000000000..ff52f476fa8
--- /dev/null
+++ b/app/helpers/placeholder_users_helper.rb
@@ -0,0 +1,39 @@
+#-- encoding: UTF-8
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2021 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 docs/COPYRIGHT.rdoc for more details.
+#++
+
+module PlaceholderUsersHelper
+ ##
+ # Determine whether the given actor can delete the placeholder user
+ def can_delete_placeholder_user?(placeholder, actor = User.current)
+ PlaceholderUsers::DeleteContract.deletion_allowed? placeholder,
+ actor,
+ Authorization::UserAllowedService.new(actor)
+ end
+end
diff --git a/app/helpers/tooltip_helper.rb b/app/helpers/tooltip_helper.rb
new file mode 100644
index 00000000000..ea35a4f84ff
--- /dev/null
+++ b/app/helpers/tooltip_helper.rb
@@ -0,0 +1,48 @@
+#-- encoding: UTF-8
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2021 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 docs/COPYRIGHT.rdoc for more details.
+#++
+
+module TooltipHelper
+ include OpenProject::FormTagHelper
+
+ ##
+ # Render a tooltip span
+ #
+ # @param text [string] Content of the tooltip
+ # @param placement [string] placement (top, left, right, bottom)
+ # @param span_classes [string] Additional classes on the span
+ # @param icon [string] icon class
+ def tooltip_tag(text, placement: 'left', icon: 'icon-help', span_classes: nil)
+ content_tag :span,
+ class: "tooltip--#{placement} #{span_classes}",
+ data: { tooltip: text } do
+ op_icon "icon #{icon}"
+ end
+ end
+end
diff --git a/app/views/placeholder_users/_toolbar.html.erb b/app/views/placeholder_users/_toolbar.html.erb
index 02a004aa18b..de880598466 100644
--- a/app/views/placeholder_users/_toolbar.html.erb
+++ b/app/views/placeholder_users/_toolbar.html.erb
@@ -34,12 +34,6 @@ See docs/COPYRIGHT.rdoc for more details.
<span class="button--text"><%= t(:label_profile) %></span>
<% end %>
</li>
- <li class="toolbar-item">
- <%= link_to deletion_info_placeholder_user_path(@placeholder_user),
- class: 'button' do %>
- <%= op_icon('button--icon icon-delete') %>
- <span class="button--text"><%= t(:button_delete) %></span>
- <% end %>
- </li>
+ <%= render partial: 'placeholder_users/toolbar_delete' %>
<% end %>
<% end %>
diff --git a/app/views/placeholder_users/_toolbar_delete.html.erb b/app/views/placeholder_users/_toolbar_delete.html.erb
new file mode 100644
index 00000000000..93f03637f62
--- /dev/null
+++ b/app/views/placeholder_users/_toolbar_delete.html.erb
@@ -0,0 +1,48 @@
+<%#-- copyright
+OpenProject is an open source project management software.
+Copyright (C) 2012-2021 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 docs/COPYRIGHT.rdoc for more details.
+
+++#%>
+
+<% if can_delete_placeholder_user?(@placeholder_user) %>
+ <li class="toolbar-item">
+ <%= link_to deletion_info_placeholder_user_path(@placeholder_user),
+ class: 'button' do %>
+ <%= op_icon('button--icon icon-delete') %>
+ <span class="button--text"><%= t(:button_delete) %></span>
+ <% end %>
+ </li>
+<% else %>
+ <li class="toolbar-item">
+ <%= content_tag :span, title: I18n.t('placeholder_users.right_to_manage_members_missing') do %>
+ <%= link_to '#',
+ class: 'button -disabled' do %>
+ <%= op_icon('button--icon icon-delete') %>
+ <span class="button--text"><%= t(:button_delete) %></span>
+ <% end %>
+ <% end %>
+ </li>
+<% end %>
\ No newline at end of file
diff --git a/app/views/placeholder_users/show.html.erb b/app/views/placeholder_users/show.html.erb
index 93a1207cb8a..091ccab0475 100644
--- a/app/views/placeholder_users/show.html.erb
+++ b/app/views/placeholder_users/show.html.erb
@@ -39,13 +39,7 @@ See docs/COPYRIGHT.rdoc for more details.
<span class="button--text"><%= t(:button_edit) %></span>
<% end %>
</li>
- <li class="toolbar-item">
- <%= link_to deletion_info_placeholder_user_path(@placeholder_user),
- class: 'button' do %>
- <%= op_icon('button--icon icon-delete') %>
- <span class="button--text"><%= t(:button_delete) %></span>
- <% end %>
- </li>
+ <%= render partial: 'placeholder_users/toolbar_delete' %>
<% end %>
<% end %>
diff --git a/spec/controllers/placeholder_users_controller_spec.rb b/spec/controllers/placeholder_users_controller_spec.rb
index 9a0ddb58463..0741bd1bd68 100644
--- a/spec/controllers/placeholder_users_controller_spec.rb
+++ b/spec/controllers/placeholder_users_controller_spec.rb
@@ -332,5 +332,36 @@ describe PlaceholderUsersController, type: :controller do
it_behaves_like 'do not allow non-admins'
end
end
+
+ context 'as a user that may not delete the placeholder' do
+ current_user { FactoryBot.create :user }
+
+ before do
+ allow(PlaceholderUsers::DeleteContract)
+ .to receive(:deletion_allowed?).and_return false
+ end
+
+ describe 'GET deletion_info' do
+ before do
+ get :deletion_info, params: { id: placeholder_user.id }
+ end
+
+ it 'responds with unauthorized status' do
+ expect(response).to_not be_successful
+ expect(response.status).to eq 403
+ end
+ end
+
+ describe 'POST destroy' do
+ before do
+ delete :destroy, params: { id: placeholder_user.id }
+ end
+
+ it 'responds with unauthorized status' do
+ expect(response).to_not be_successful
+ expect(response.status).to eq 403
+ end
+ end
+ end
end
diff --git a/spec/features/placeholder_users/delete_spec.rb b/spec/features/placeholder_users/delete_spec.rb
index 048ea75d7ad..10eca8dd10d 100644
--- a/spec/features/placeholder_users/delete_spec.rb
+++ b/spec/features/placeholder_users/delete_spec.rb
@@ -72,6 +72,31 @@ describe 'delete placeholder user', type: :feature, js: true do
it_behaves_like 'placeholders delete flow'
end
+ context 'as user with global permission, but placeholder in an invisble project' do
+ current_user { FactoryBot.create :user, global_permission: %i[manage_placeholder_user] }
+
+ let!(:project) { FactoryBot.create :project }
+ let!(:member) do
+ FactoryBot.create :member,
+ principal: placeholder_user,
+ project: project,
+ roles: [FactoryBot.create(:role)]
+ end
+
+ it 'returns an error when trying to delete and disables the button' do
+ visit deletion_info_placeholder_user_path(placeholder_user)
+ expect(page).to have_content I18n.t('placeholder_users.right_to_manage_members_missing').strip
+
+ visit placeholder_user_path(placeholder_user)
+
+ expect(page).to have_selector '.button.-disabled', text: 'Delete'
+
+ visit edit_placeholder_user_path(placeholder_user)
+
+ expect(page).to have_selector '.button.-disabled', text: 'Delete'
+ end
+ end
+
context 'as user without global permission' do
current_user { FactoryBot.create :user }