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

Switch attribute help text to check permission instead of admin

Klaus Zanders9cb5fd4de369
diff --git a/app/contracts/attribute_help_texts/base_contract.rb b/app/contracts/attribute_help_texts/base_contract.rb
index 40ffe84bb56..e95b7ceff8e 100644
--- a/app/contracts/attribute_help_texts/base_contract.rb
+++ b/app/contracts/attribute_help_texts/base_contract.rb
@@ -28,7 +28,6 @@
module AttributeHelpTexts
class BaseContract < ::ModelContract
- include RequiresAdminGuard
include Attachments::ValidateReplacements
def self.model
@@ -38,5 +37,13 @@ module AttributeHelpTexts
attribute :type
attribute :attribute_name
attribute :help_text
+
+ validate :user_allowed
+
+ private
+
+ def user_allowed
+ errors.add :base, :error_unauthorized unless user.allowed_globally?(:edit_attribute_help_texts)
+ end
end
end
diff --git a/app/controllers/attribute_help_texts_controller.rb b/app/controllers/attribute_help_texts_controller.rb
index f9f33b1f3a3..6ba1d5e982d 100644
--- a/app/controllers/attribute_help_texts_controller.rb
+++ b/app/controllers/attribute_help_texts_controller.rb
@@ -30,7 +30,7 @@ class AttributeHelpTextsController < ApplicationController
layout 'admin'
menu_item :attribute_help_texts
- before_action :require_admin
+ before_action :authorize_global
before_action :require_ee_token, except: %i[upsale]
before_action :find_entry, only: %i(edit update destroy)
before_action :find_type_scope
diff --git a/config/initializers/permissions.rb b/config/initializers/permissions.rb
index 5d3849891b5..5ff57501ff8 100644
--- a/config/initializers/permissions.rb
+++ b/config/initializers/permissions.rb
@@ -173,7 +173,10 @@ Rails.application.reloader.to_prepare do
contract_actions: { projects: %i[copy] }
map.permission :edit_attribute_help_texts,
- {},
+ {
+ admin: %i[index],
+ attribute_help_texts: %i[index new edit upsale create update destroy]
+ },
permissible_on: :global,
require: :loggedin,
grant_to_admin: true
diff --git a/lib/api/v3/help_texts/help_text_representer.rb b/lib/api/v3/help_texts/help_text_representer.rb
index ba31be3f966..fbc23bae8bf 100644
--- a/lib/api/v3/help_texts/help_text_representer.rb
+++ b/lib/api/v3/help_texts/help_text_representer.rb
@@ -39,8 +39,8 @@ module API
title_getter: ->(*) {}
link :editText,
- cache_if: -> { current_user.admin? } do
- if represented.persisted?
+ cache_if: -> { current_user.allowed_globally?(:edit_attribute_help_texts) } do
+ if represented.persisted? && current_user.allowed_globally?(:edit_attribute_help_texts)
{
href: edit_attribute_help_text_path(represented.id),
type: 'text/html'
diff --git a/spec/controllers/attribute_help_texts_controller_spec.rb b/spec/controllers/attribute_help_texts_controller_spec.rb
index 28cd47739e5..8b2947bfb9a 100644
--- a/spec/controllers/attribute_help_texts_controller_spec.rb
+++ b/spec/controllers/attribute_help_texts_controller_spec.rb
@@ -1,7 +1,7 @@
require 'spec_helper'
RSpec.describe AttributeHelpTextsController, with_ee: %i[attribute_help_texts] do
- let(:user) { build_stubbed(:admin) }
+ let(:user) { build_stubbed(:user) }
let(:model) { build(:work_package_help_text) }
let(:find_expectation) do
@@ -13,6 +13,10 @@ RSpec.describe AttributeHelpTextsController, with_ee: %i[attribute_help_texts] d
before do
login_as user
+
+ mock_permissions_for(user) do |mock|
+ mock.allow_globally :edit_attribute_help_texts
+ end
end
describe '#index' do
diff --git a/spec/features/admin/attribute_help_texts_spec.rb b/spec/features/admin/attribute_help_texts_spec.rb
index 615c28f4f9b..8451a0b268e 100644
--- a/spec/features/admin/attribute_help_texts_spec.rb
+++ b/spec/features/admin/attribute_help_texts_spec.rb
@@ -28,10 +28,8 @@
require 'spec_helper'
-RSpec.describe 'Attribute help texts',
- js: true,
- with_cuprite: true do
- shared_let(:admin) { create(:admin) }
+RSpec.describe 'Attribute help texts', :js, :with_cuprite do
+ shared_let(:user_with_permission) { create(:user, global_permissions: [:edit_attribute_help_texts]) }
let(:instance) { AttributeHelpText.last }
let(:modal) { Components::AttributeHelpTextModal.new(instance) }
@@ -41,12 +39,12 @@ RSpec.describe 'Attribute help texts',
describe 'Work package help texts', with_ee: %i[attribute_help_texts] do
before do
- login_as(admin)
+ login_as(user_with_permission)
visit attribute_help_texts_path
end
# TODO: Migrate to cuprite when the `better_cuprite_billy` driver is added
- context 'with direct uploads (Regression #34285)', with_cuprite: false, with_direct_uploads: true do
+ context 'with direct uploads (Regression #34285)', :with_direct_uploads, with_cuprite: false do
before do
allow_any_instance_of(Attachment).to receive(:diskfile).and_return image_fixture
end
@@ -70,7 +68,7 @@ RSpec.describe 'Attribute help texts',
context 'with help texts allowed by the enterprise token' do
it 'allows CRUD to attribute help texts' do
- expect(page).to have_selector('.generic-table--no-results-container')
+ expect(page).to have_css('.generic-table--no-results-container')
# Create help text
# -> new
@@ -88,7 +86,7 @@ RSpec.describe 'Attribute help texts',
click_button 'Save'
# Should now show on index for editing
- expect(page).to have_selector('.attribute-help-text--entry td', text: 'Status')
+ expect(page).to have_css('.attribute-help-text--entry td', text: 'Status')
expect(instance.attribute_scope).to eq 'WorkPackage'
expect(instance.attribute_name).to eq 'status'
expect(instance.help_text).to include 'My attribute help text'
@@ -97,11 +95,11 @@ RSpec.describe 'Attribute help texts',
# Open help text modal
modal.open!
expect(modal.modal_container).to have_text 'My attribute help text'
- expect(modal.modal_container).to have_selector('img')
- modal.expect_edit(admin: true)
+ expect(modal.modal_container).to have_css('img')
+ modal.expect_edit(editable: true)
# Expect files section to be present
- expect(modal.modal_container).to have_selector('.form--fieldset-legend', text: 'ATTACHMENTS')
+ expect(modal.modal_container).to have_css('.form--fieldset-legend', text: 'ATTACHMENTS')
expect(modal.modal_container).to have_test_selector('op-files-tab--file-list-item-title')
modal.close!
@@ -110,39 +108,39 @@ RSpec.describe 'Attribute help texts',
SeleniumHubWaiter.wait
find('.attribute-help-text--entry td a', text: 'Status').click
SeleniumHubWaiter.wait
- expect(page).to have_selector('#attribute_help_text_attribute_name[disabled]')
+ expect(page).to have_css('#attribute_help_text_attribute_name[disabled]')
editor.set_markdown(' ')
click_button 'Save'
# Handle errors
- expect(page).to have_selector('#errorExplanation', text: "Help text can't be blank.")
+ expect(page).to have_css('#errorExplanation', text: "Help text can't be blank.")
SeleniumHubWaiter.wait
editor.set_markdown('New**help**text')
click_button 'Save'
# On index again
- expect(page).to have_selector('.attribute-help-text--entry td', text: 'Status')
+ expect(page).to have_css('.attribute-help-text--entry td', text: 'Status')
instance.reload
expect(instance.help_text).to eq 'New**help**text'
# Open help text modal
modal.open!
- expect(modal.modal_container).to have_selector('strong', text: 'help')
- modal.expect_edit(admin: true)
+ expect(modal.modal_container).to have_css('strong', text: 'help')
+ modal.expect_edit(editable: true)
modal.close!
- expect(page).to have_selector('.attribute-help-text--entry td', text: 'Status')
+ expect(page).to have_css('.attribute-help-text--entry td', text: 'Status')
# Open again and edit this time
modal.open!
modal.edit_button.click
- expect(page).to have_selector('#attribute_help_text_attribute_name[disabled]')
+ expect(page).to have_css('#attribute_help_text_attribute_name[disabled]')
visit attribute_help_texts_path
# Create new, status is now blocked
find('.attribute-help-texts--create-button').click
- expect(page).to have_selector('#attribute_help_text_attribute_name option', text: 'Assignee')
- expect(page).not_to have_selector('#attribute_help_text_attribute_name option', text: 'Status')
+ expect(page).to have_css('#attribute_help_text_attribute_name option', text: 'Assignee')
+ expect(page).not_to have_css('#attribute_help_text_attribute_name option', text: 'Status')
visit attribute_help_texts_path
# Destroy
@@ -150,7 +148,7 @@ RSpec.describe 'Attribute help texts',
find('.attribute-help-text--entry .icon-delete').click
end
- expect(page).to have_selector('.generic-table--no-results-container')
+ expect(page).to have_css('.generic-table--no-results-container')
expect(AttributeHelpText.count).to be_zero
end
end
diff --git a/spec/features/support/components/attribute_help_text_modal.rb b/spec/features/support/components/attribute_help_text_modal.rb
index d7f2ca13ce7..cb8e80aa901 100644
--- a/spec/features/support/components/attribute_help_text_modal.rb
+++ b/spec/features/support/components/attribute_help_text_modal.rb
@@ -54,7 +54,7 @@ module Components
def open!
SeleniumHubWaiter.wait
container.find("[data-qa-help-text-for='#{help_text.attribute_name}']").click
- expect(page).to have_selector('[data-test-selector="attribute-help-text--header"]', text: help_text.attribute_caption)
+ expect(page).to have_css('[data-test-selector="attribute-help-text--header"]', text: help_text.attribute_caption)
end
def close!
@@ -69,14 +69,14 @@ module Components
end
element.click(x: -((width / 2) - 10), y: -((height / 2) - 10))
end
- expect(page).not_to have_selector('[data-test-selector="attribute-help-text--header"]', text: help_text.attribute_caption)
+ expect(page).not_to have_css('[data-test-selector="attribute-help-text--header"]', text: help_text.attribute_caption)
end
- def expect_edit(admin:)
- if admin
- expect(page).to have_selector('.help-text--edit-button')
+ def expect_edit(editable:)
+ if editable
+ expect(page).to have_css('.help-text--edit-button')
else
- expect(page).not_to have_selector('.help-text--edit-button')
+ expect(page).not_to have_css('.help-text--edit-button')
end
end