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
[#66925] Activity shows changes to admin only custom fields also to non admin users
Ivan Kuchinbe3f82ba9536
diff --git a/app/controllers/journals_controller.rb b/app/controllers/journals_controller.rbindex 0e3ce394057..1e3498ee58a 100644--- a/app/controllers/journals_controller.rb+++ b/app/controllers/journals_controller.rb@@ -32,6 +32,7 @@ class JournalsController < ApplicationControllerbefore_action :load_and_authorize_in_optional_project, only: [:index]before_action :find_journal,:ensure_permitted,+ :ensure_valid_for_diffing,only: [:diff]authorization_checked! :diff@@ -62,8 +63,6 @@ class JournalsController < ApplicationControllerenddef diff- return render_404 unless valid_field_for_diffing?-unless @journal.details[field_param] in [from, to]return render_400 message: I18n.t(:error_journal_attribute_not_present, attribute: field_param)end@@ -103,14 +102,22 @@ class JournalsController < ApplicationController@field_param ||= params[:field].parameterize.underscoreend- def valid_field_for_diffing?+ def ensure_valid_for_diffingcase field_paramwhen "description","status_explanation",/\Aagenda_items_\d+_notes\z/- true+ # no additional checkswhen /\Acustom_fields_(?<id>\d+)\z/- ::CustomField.exists?(id: Regexp.last_match[:id], field_format: "text")+ cf = CustomField.select(:field_format, :admin_only).find_by(id: Regexp.last_match[:id])++ if cf.admin_only && !User.current.admin?+ render_403+ elsif cf.field_format != "text"+ render_404+ end+ else+ render_404endenddiff --git a/lib/open_project/journal_formatter/custom_field.rb b/lib/open_project/journal_formatter/custom_field.rbindex 99d3dfb95fb..0c5044b7df1 100644--- a/lib/open_project/journal_formatter/custom_field.rb+++ b/lib/open_project/journal_formatter/custom_field.rb@@ -35,6 +35,8 @@ class OpenProject::JournalFormatter::CustomFieldenddef render(key, values, options = { html: true })+ return if custom_field_for_key(key)&.admin_only && !User.current.admin?+formatter_for_key(key).render(key, values, options)enddiff --git a/spec/controllers/journals_controller_spec.rb b/spec/controllers/journals_controller_spec.rbindex 46b2ea2e859..aa2b3bf5438 100644--- a/spec/controllers/journals_controller_spec.rb+++ b/spec/controllers/journals_controller_spec.rb@@ -76,80 +76,126 @@ RSpec.describe JournalsController doenddescribe "with a user not having the :view_work_package permission" do- before do- RolePermission.delete_all- end+ let(:user) { build_stubbed(:user) }it { expect(response).to have_http_status(:forbidden) }endend- context "for work package text custom field" do+ context "for work package custom field" doshared_let(:type) { project.types.first }+ shared_let(:work_package) do+ create(:work_package, type:,+ author: user,+ project:)+ end- shared_let(:custom_field) do- create(:text_wp_custom_field).tap do |custom_field|+ let!(:custom_field) do+ create(factory_name).tap do |custom_field|project.work_package_custom_fields << custom_fieldtype.custom_fields << custom_fieldendend- shared_let(:work_package) do- create(:work_package, type:,- author: user,- project:)- endlet(:params) { { id: work_package.last_journal.id.to_s, field: "custom_fields_#{custom_field.id}", format: "js" } }before dowork_package.update custom_field_values: { custom_field.id => "foo" }end- describe "with a user having :view_work_package permission" do- it { expect(response).to have_http_status(:ok) }+ context "with format text" do+ let(:factory_name) { :text_wp_custom_field }++ describe "with a user having :view_work_package permission" do+ it { expect(response).to have_http_status(:ok) }++ it "presents the diff correctly" do+ expect(response.body.strip).to be_html_eql <<-HTML+ <div class="text-diff">+ <label class="sr-only">Begin of the insertion</label>+ <ins class="diffmod">foo</ins>+ <label class="sr-only">End of the insertion</label>+ </div>+ HTML+ end+ end- it "presents the diff correctly" do- expect(response.body.strip).to be_html_eql <<-HTML- <div class="text-diff">- <label class="sr-only">Begin of the insertion</label>- <ins class="diffmod">foo</ins>- <label class="sr-only">End of the insertion</label>- </div>- HTML+ describe "with a user not having the :view_work_package permission" do+ let(:user) { build_stubbed(:user) }++ it { expect(response).to have_http_status(:forbidden) }endend- describe "with a user not having the :view_work_package permission" do- before do- RolePermission.delete_all- end+ context "with format string" do+ let(:factory_name) { :wp_custom_field }- it { expect(response).to have_http_status(:forbidden) }+ it { expect(response).to have_http_status(:not_found) }endend- context "for work package string custom field" do- shared_let(:type) { project.types.first }+ context "for project custom field" do+ let(:params) { { id: project.last_journal.id.to_s, field: "custom_fields_#{custom_field.id}", format: "js" } }- shared_let(:custom_field) do- create(:wp_custom_field).tap do |custom_field|- project.work_package_custom_fields << custom_field- type.custom_fields << custom_field- end+ before do+ project.update custom_field_values: { custom_field.id => "foo" }end- shared_let(:work_package) do- create(:work_package, type:,- author: user,- project:)- end- let(:params) { { id: work_package.last_journal.id.to_s, field: "custom_fields_#{custom_field.id}", format: "js" } }+ context "with format text" do+ context "when visible to everyone" do+ let!(:custom_field) { create(:text_project_custom_field, projects: [project]) }++ describe "with a user being project member" do+ it { expect(response).to have_http_status(:ok) }++ it "presents the diff correctly" do+ expect(response.body.strip).to be_html_eql <<-HTML+ <div class="text-diff">+ <label class="sr-only">Begin of the insertion</label>+ <ins class="diffmod">foo</ins>+ <label class="sr-only">End of the insertion</label>+ </div>+ HTML+ end+ end++ describe "with a user not being project member" do+ let(:user) { build_stubbed(:user) }++ it { expect(response).to have_http_status(:forbidden) }+ end+ end- before do- work_package.update custom_field_values: { custom_field.id => "foo" }+ context "when admin only" do+ let!(:custom_field) { create(:text_project_custom_field, :admin_only, projects: [project]) }++ describe "with a non admin user being a project member" do+ it { expect(response).to have_http_status(:forbidden) }+ end++ describe "with an admin user" do+ let(:user) { build_stubbed(:admin) }++ it { expect(response).to have_http_status(:ok) }++ it "presents the diff correctly" do+ expect(response.body.strip).to be_html_eql <<-HTML+ <div class="text-diff">+ <label class="sr-only">Begin of the insertion</label>+ <ins class="diffmod">foo</ins>+ <label class="sr-only">End of the insertion</label>+ </div>+ HTML+ end+ end+ endend- it { expect(response).to have_http_status(:not_found) }+ context "with format string" do+ let!(:custom_field) { create(:string_project_custom_field, projects: [project]) }++ it { expect(response).to have_http_status(:not_found) }+ endendcontext "for project description" do@@ -174,9 +220,7 @@ RSpec.describe JournalsController doenddescribe "with a user not being member of the project" do- before do- Member.delete_all- end+ let(:user) { build_stubbed(:user) }it { expect(response).to have_http_status(:forbidden) }end@@ -204,6 +248,7 @@ RSpec.describe JournalsController doauthor: user,project:)end+let(:params) { { id: work_package.last_journal.id.to_s, field: :another_field, format: "js" } }it { expect(response).to have_http_status(:not_found) }@@ -220,9 +265,7 @@ RSpec.describe JournalsController doenddescribe "even with a user having all permissions" do- before do- user.update(admin: true)- end+ let(:user) { build_stubbed(:admin) }it { expect(response).to have_http_status(:forbidden) }enddiff --git a/spec/lib/open_project/journal_formatter/custom_field_spec.rb b/spec/lib/open_project/journal_formatter/custom_field_spec.rbindex 474bdfbff00..f541764b41e 100644--- a/spec/lib/open_project/journal_formatter/custom_field_spec.rb+++ b/spec/lib/open_project/journal_formatter/custom_field_spec.rb@@ -184,6 +184,24 @@ RSpec.describe OpenProject::JournalFormatter::CustomField doendend+ context "with admin only custom field" do+ let(:values) { [nil, "1"] }++ before { custom_field.admin_only = true }++ context "as a non admin user" do+ current_user { build_stubbed(:user) }++ it { expect(rendered).to be_nil }+ end++ context "as an admin" do+ current_user { build_stubbed(:admin) }++ it { expect(rendered).to be_a(String) }+ end+ end+context "for a multi-select custom field" dolet(:custom_field) { build_stubbed(:user_wp_custom_field) }