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

Return all visible ProjectCustomFields when the user is allowed to select project custom fields in any project

Dombi Attilac6980611fc2d
diff --git a/app/contracts/project_custom_field_project_mappings/base_contract.rb b/app/contracts/project_custom_field_project_mappings/base_contract.rb
index 0ed19cf6d5a..6032e72e723 100644
--- a/app/contracts/project_custom_field_project_mappings/base_contract.rb
+++ b/app/contracts/project_custom_field_project_mappings/base_contract.rb
@@ -52,7 +52,8 @@ module ProjectCustomFieldProjectMappings
def visbile_to_user
# "invisible" custom fields can only be seen and edited by admins
# using visible scope to check if the custom field is actually visible to the user
- return if model.project_custom_field.nil? || ProjectCustomField.visible.pluck(:id).include?(model.project_custom_field.id)
+ return if model.project_custom_field.nil? ||
+ ProjectCustomField.visible(user).pluck(:id).include?(model.project_custom_field.id)
errors.add :custom_field_id, :invalid
end
diff --git a/app/models/project_custom_field.rb b/app/models/project_custom_field.rb
index d178d0f0748..251e74b4a2c 100644
--- a/app/models/project_custom_field.rb
+++ b/app/models/project_custom_field.rb
@@ -45,17 +45,19 @@ class ProjectCustomField < CustomField
def self.visible(user = User.current)
if user.admin?
all
+ elsif user.allowed_in_any_project?(:select_project_custom_fields)
+ where(visible: true)
else
- allowed_project_ids = Project.allowed_to(User.current, :view_project_attributes).select(:id).arel
+ allowed_project_ids = Project.allowed_to(user, :view_project_attributes).select(:id).arel
mapping_table = ProjectCustomFieldProjectMapping.arel_table
mapping_query = mapping_table.project(Arel.star)
.where(
- mapping_table[:custom_field_id].eq(ProjectCustomField.arel_table[:id])
+ mapping_table[:custom_field_id].eq(arel_table[:id])
.and(mapping_table[:project_id].in(allowed_project_ids))
)
- ProjectCustomField.where(visible: true).where(mapping_query.exists)
+ where(visible: true).where(mapping_query.exists)
end
end