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

Do not move the view_project_attributes permission check from the ProjectCustomField yet

Dombi Attila00ff9680d858
diff --git a/app/models/project_custom_field.rb b/app/models/project_custom_field.rb
index b8aa73aba1b..aedf7809e81 100644
--- a/app/models/project_custom_field.rb
+++ b/app/models/project_custom_field.rb
@@ -39,14 +39,31 @@ class ProjectCustomField < CustomField
validates :custom_field_section_id, presence: true
class << self
- def visible(user = User.current)
+ def visible(user = User.current, project: nil)
if user.admin?
all
- else
+ elsif user.allowed_in_any_project?(:select_project_custom_fields) || user.allowed_globally?(:add_project)
where(visible: true)
+ else
+ where(visible: true).where(mappings_with_view_project_attributes_permission(user, project).exists)
end
end
+ private
+
+ def mappings_with_view_project_attributes_permission(user, project) # rubocop:disable Metrics/AbcSize
+ allowed_projects = Project.allowed_to(user, :view_project_attributes)
+ mapping_table = ProjectCustomFieldProjectMapping.arel_table
+
+ mapping_condition = mapping_table[:custom_field_id].eq(arel_table[:id])
+ .and(mapping_table[:project_id].in(allowed_projects.select(:id).arel))
+
+ if project&.persisted?
+ mapping_condition = mapping_condition.and(mapping_table[:project_id].eq(project.id))
+ end
+
+ mapping_table.project(Arel.star).where(mapping_condition)
+ end
end
def type_name
diff --git a/app/models/projects/custom_fields.rb b/app/models/projects/custom_fields.rb
index 4b9161175a6..8ae6368f04d 100644
--- a/app/models/projects/custom_fields.rb
+++ b/app/models/projects/custom_fields.rb
@@ -41,12 +41,8 @@ module Projects::CustomFields
def available_custom_fields
return all_visible_custom_fields if new_record?
- if User.current.allowed_in_project?(:view_project_attributes, self)
- all_visible_custom_fields.where(id: project_custom_field_project_mappings.select(:custom_field_id))
- .or(ProjectCustomField.required)
- else
- all_visible_custom_fields.none
- end
+ all_visible_custom_fields.where(id: project_custom_field_project_mappings.select(:custom_field_id))
+ .or(ProjectCustomField.required)
end
# Note:
@@ -65,7 +61,7 @@ module Projects::CustomFields
end
def all_visible_custom_fields
- all_available_custom_fields.visible
+ all_available_custom_fields.visible(project: self)
end
def custom_field_values_to_validate