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
[#62317] redefine WorkPackageCustomField#visible_by_user scope
Tobias Dillmanna40079f7ad50
diff --git a/app/models/attribute_help_text/work_package.rb b/app/models/attribute_help_text/work_package.rbindex 8ba82dd5b67..2d034ddcd98 100644--- a/app/models/attribute_help_text/work_package.rb+++ b/app/models/attribute_help_text/work_package.rb@@ -49,7 +49,7 @@ class AttributeHelpText::WorkPackage < AttributeHelpTextdef self.visible_condition(user)visible_cf_names = WorkPackageCustomField- .visible_by_user(user)+ .manageable_by_user(user).pluck(:id).map { |id| "custom_field_#{id}" }diff --git a/app/models/work_package_custom_field.rb b/app/models/work_package_custom_field.rbindex 5af119827e6..c41c2553ce1 100644--- a/app/models/work_package_custom_field.rb+++ b/app/models/work_package_custom_field.rb@@ -38,17 +38,21 @@ class WorkPackageCustomField < CustomFieldsource: :customized,source_type: "WorkPackage"- scope :visible_by_user, ->(user) {+ scope :manageable_by_user, ->(user) {if user.allowed_in_any_project?(:select_custom_fields)allelse- where(projects: { id: Project.visible(user) })- .where(types: { id: Type.enabled_in(Project.visible(user)) })- .or(where(is_for_all: true).references(:projects, :types))- .includes(:projects, :types)+ visible_by_user(user)end}+ scope :visible_by_user, ->(user) {+ where(projects: { id: Project.visible(user) })+ .where(types: { id: Type.enabled_in(Project.visible(user)) })+ .or(where(is_for_all: true).references(:projects, :types))+ .includes(:projects, :types)+ }+scope :usable_as_custom_action, -> {where.not(field_format: %w[hierarchy]).order(:name)diff --git a/spec/models/custom_field/work_package_custom_field_spec.rb b/spec/models/custom_field/work_package_custom_field_spec.rbindex 6f1c6afd9b1..545816419b3 100644--- a/spec/models/custom_field/work_package_custom_field_spec.rb+++ b/spec/models/custom_field/work_package_custom_field_spec.rb@@ -31,28 +31,16 @@require "spec_helper"RSpec.describe WorkPackageCustomField, :model do- let!(:bool_cf) { create(:boolean_wp_custom_field) }- let!(:text_cf) { create(:text_wp_custom_field) }- let!(:hierarchy_cf) { create(:hierarchy_wp_custom_field) }+ let(:feature) { create(:type_feature) }+ let(:task) { create(:type_task) }+ let(:project) { create(:project, types: [feature, task]) }+ let(:project_without_user) { create(:project, types: [feature, task]) }describe "scopes" do- context "if user has permission to select custom fields" do- let(:user) { create(:user) }- let(:role) { create(:project_role, permissions: [:select_custom_fields]) }- let!(:project) { create(:project, members: { user => role }) }-- it "returns all custom fields" do- expect(described_class.visible_by_user(user)).to contain_exactly(bool_cf, text_cf, hierarchy_cf)- end- end-- context "if user has no access to a project with the enabled custom field" do- let(:user) { create(:user) }- let(:role) { create(:project_role, permissions: []) }- let(:feature) { create(:type_feature) }- let(:task) { create(:type_task) }- let(:project) { create(:project, members: { user => role }, types: [feature, task]) }- let(:project_without_user) { create(:project, types: [feature, task]) }+ describe "manageable_by_user" do+ let!(:bool_cf) { create(:boolean_wp_custom_field) }+ let!(:text_cf) { create(:text_wp_custom_field) }+ let!(:hierarchy_cf) { create(:hierarchy_wp_custom_field) }before doproject.work_package_custom_fields << text_cf@@ -62,13 +50,48 @@ RSpec.describe WorkPackageCustomField, :model do# hierarchy_cf is not enabled in any projectend- it "returns only custom fields that are enabled in projects the user has access to" do- expect(described_class.visible_by_user(user)).to contain_exactly(text_cf)+ context "if user has permission to select custom fields" do+ let(:user) { create(:user, member_with_permissions: { project => [:select_custom_fields] }) }++ it "returns all custom fields" do+ expect(described_class.manageable_by_user(user)).to contain_exactly(bool_cf, text_cf, hierarchy_cf)+ end+ end++ context "if user does not have permission to select custom fields" do+ let(:user) { create(:user) }+ let(:role) { create(:project_role, permissions: []) }+ let(:project) { create(:project, members: { user => role }, types: [feature, task]) }++ it "returns only custom fields that are enabled in projects the user has access to" do+ expect(described_class.manageable_by_user(user)).to contain_exactly(text_cf)+ end+ end++ it "returns custom fields that are usable as custom action" do+ expect(described_class.usable_as_custom_action).to contain_exactly(bool_cf, text_cf)endend+ end++ describe "visible_by_user" do+ let(:user) { create(:user, member_with_permissions: { project => [] }) }++ it "returns an empty array" do+ expect(described_class.visible_by_user(user)).to be_empty+ end- it "returns custom fields that are usable as custom action" do- expect(described_class.usable_as_custom_action).to contain_exactly(bool_cf, text_cf)+ context "with custom fields and types added to the project" do+ # User cannot see this field as its type is not enabled in the project:+ let!(:text_cf) { create(:text_wp_custom_field, projects: [project, project_without_user], types: []) }+ # User cannot see this field as they are not a member:+ let!(:hierarchy_cf) { create(:hierarchy_wp_custom_field, projects: [project_without_user], types: [feature, task]) }+ # User can see this field:+ let!(:bool_cf) { create(:boolean_wp_custom_field, projects: [project], types: [feature]) }++ it "returns custom fields with types that are enabled in the project" do+ expect(described_class.visible_by_user(user)).to contain_exactly(bool_cf)+ endendendenddiff --git a/spec/models/query/results_sums_integration_spec.rb b/spec/models/query/results_sums_integration_spec.rbindex ad2fa972537..f46f8f59cfa 100644--- a/spec/models/query/results_sums_integration_spec.rb+++ b/spec/models/query/results_sums_integration_spec.rb@@ -132,13 +132,14 @@ RSpec.describe Query::Results, "sums" docreate(:type) do |t|t.custom_fields << int_cft.custom_fields << float_cf+ project.types << tendendlet(:current_user) docreate(:user, member_with_permissions: { project => permissions })endlet(:permissions) do- %i[view_work_packages view_cost_entries view_time_entries view_cost_rates view_hourly_rates select_custom_fields]+ %i[view_work_packages view_cost_entries view_time_entries view_cost_rates view_hourly_rates]endlet(:group_by) { nil }let(:query) dodiff --git a/spec/models/query_spec.rb b/spec/models/query_spec.rbindex f5b844e390f..d71d7250c1e 100644--- a/spec/models/query_spec.rb+++ b/spec/models/query_spec.rb@@ -34,7 +34,7 @@ RSpec.describe Query,with_ee: %i[baseline_comparison conditional_highlighting work_package_query_relation_columns] dolet(:query) { build(:query) }let(:project) { create(:project) }- let(:user_cf) { create(:user, member_with_permissions: { project => [:select_custom_fields] }) }+ let(:project_member) { create(:user, member_with_permissions: { project => [:view_project] }) }let(:user_restricted) { create(:user) }describe ".new_default" do@@ -477,17 +477,20 @@ RSpec.describe Query,enddescribe ".available_columns" do- let(:custom_field) { create(:list_wp_custom_field) }let(:type) { create(:type) }+ let(:custom_field) { create(:list_wp_custom_field, types: [type], projects: [project]) }before do+ custom_field+ project.types << type+stub_const("Relation::TYPES",relation1: { name: :label_relates_to, sym_name: :label_relates_to, order: 1, sym: :relation1 },relation2: { name: :label_duplicates, sym_name: :label_duplicated_by, order: 2, sym: :relation2 })endcontext "with the enterprise token allowing relation columns" do- current_user { user_cf }+ current_user { project_member }it "has all static columns, cf columns and relation columns" doexpected_columns = %i(id project assigned_to author@@ -502,7 +505,7 @@ RSpec.describe Query,expect(described_class.available_columns.map(&:name)).to include *expected_columnsend- context "when the user does not have the necessary custom field permissions" do+ context "when the user cannot see the project" docurrent_user { user_restricted }it "does not list custom field columns" do@@ -518,7 +521,7 @@ RSpec.describe Query,endcontext "with the enterprise token disallowing relation columns", with_ee: false do- current_user { user_cf }+ current_user { project_member }it "has all static columns, cf columns but no relation columns" doexpected_columns = %i(id project assigned_to author