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

[#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.rb
index 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 < AttributeHelpText
def 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.rb
index 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 < CustomField
source: :customized,
source_type: "WorkPackage"
- scope :visible_by_user, ->(user) {
+ scope :manageable_by_user, ->(user) {
if user.allowed_in_any_project?(:select_custom_fields)
all
else
- 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.rb
index 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 do
project.work_package_custom_fields << text_cf
@@ -62,13 +50,48 @@ RSpec.describe WorkPackageCustomField, :model do
# hierarchy_cf is not enabled in any project
end
- 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)
end
end
+ 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)
+ end
end
end
end
diff --git a/spec/models/query/results_sums_integration_spec.rb b/spec/models/query/results_sums_integration_spec.rb
index 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" do
create(:type) do |t|
t.custom_fields << int_cf
t.custom_fields << float_cf
+ project.types << t
end
end
let(:current_user) do
create(:user, member_with_permissions: { project => permissions })
end
let(: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]
end
let(:group_by) { nil }
let(:query) do
diff --git a/spec/models/query_spec.rb b/spec/models/query_spec.rb
index 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] do
let(: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,
end
describe ".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 })
end
context "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" do
expected_columns = %i(id project assigned_to author
@@ -502,7 +505,7 @@ RSpec.describe Query,
expect(described_class.available_columns.map(&:name)).to include *expected_columns
end
- context "when the user does not have the necessary custom field permissions" do
+ context "when the user cannot see the project" do
current_user { user_restricted }
it "does not list custom field columns" do
@@ -518,7 +521,7 @@ RSpec.describe Query,
end
context "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" do
expected_columns = %i(id project assigned_to author