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
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.rbindex 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 AttributeHelpTextsclass BaseContract < ::ModelContract- include RequiresAdminGuardinclude Attachments::ValidateReplacementsdef self.model@@ -38,5 +37,13 @@ module AttributeHelpTextsattribute :typeattribute :attribute_nameattribute :help_text++ validate :user_allowed++ private++ def user_allowed+ errors.add :base, :error_unauthorized unless user.allowed_globally?(:edit_attribute_help_texts)+ endendenddiff --git a/app/controllers/attribute_help_texts_controller.rb b/app/controllers/attribute_help_texts_controller.rbindex 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 < ApplicationControllerlayout 'admin'menu_item :attribute_help_texts- before_action :require_admin+ before_action :authorize_globalbefore_action :require_ee_token, except: %i[upsale]before_action :find_entry, only: %i(edit update destroy)before_action :find_type_scopediff --git a/config/initializers/permissions.rb b/config/initializers/permissions.rbindex 5d3849891b5..5ff57501ff8 100644--- a/config/initializers/permissions.rb+++ b/config/initializers/permissions.rb@@ -173,7 +173,10 @@ Rails.application.reloader.to_prepare docontract_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: truediff --git a/lib/api/v3/help_texts/help_text_representer.rb b/lib/api/v3/help_texts/help_text_representer.rbindex 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 APItitle_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.rbindex 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] dbefore dologin_as user++ mock_permissions_for(user) do |mock|+ mock.allow_globally :edit_attribute_help_texts+ endenddescribe '#index' dodiff --git a/spec/features/admin/attribute_help_texts_spec.rb b/spec/features/admin/attribute_help_texts_spec.rbindex 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] dobefore do- login_as(admin)+ login_as(user_with_permission)visit attribute_help_texts_pathend# 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 dobefore doallow_any_instance_of(Attachment).to receive(:diskfile).and_return image_fixtureend@@ -70,7 +68,7 @@ RSpec.describe 'Attribute help texts',context 'with help texts allowed by the enterprise token' doit '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 modalmodal.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.waitfind('.attribute-help-text--entry td a', text: 'Status').clickSeleniumHubWaiter.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.waiteditor.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.reloadexpect(instance.help_text).to eq 'New**help**text'# Open help text modalmodal.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 timemodal.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 blockedfind('.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').clickend- 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_zeroendenddiff --git a/spec/features/support/components/attribute_help_text_modal.rb b/spec/features/support/components/attribute_help_text_modal.rbindex 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 Componentsdef open!SeleniumHubWaiter.waitcontainer.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)enddef close!@@ -69,14 +69,14 @@ module Componentsendelement.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')endend