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

[#57077] Admin only User Custom field is visible to non-admin users

Dombi Attila87bd93a5a4e7
diff --git a/app/models/user.rb b/app/models/user.rb
index df5c4a0f48a..040f1005f47 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -187,6 +187,15 @@ class User < Principal
write_attribute(:mail, arg.to_s.strip)
end
+ def self.available_custom_fields(_user)
+ user = User.current
+ RequestStore.fetch(:"#{name.underscore}_custom_fields_#{user.id}_#{user.admin?}") do
+ scope = CustomField.where(type: "#{name}CustomField").order(:position)
+ scope = scope.where(admin_only: false) if !user.admin?
+ scope
+ end
+ end
+
def self.search_in_project(query, options)
options.fetch(:project).users.like(query)
end
diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb
index e0f53d9fdba..2cb717f4e12 100644
--- a/spec/models/user_spec.rb
+++ b/spec/models/user_spec.rb
@@ -1016,4 +1016,30 @@ RSpec.describe User do
let(:model_instance) { user }
let(:custom_field) { create(:user_custom_field, :string) }
end
+
+ describe ".available_custom_fields" do
+ let(:admin) { build_stubbed(:admin) }
+ let(:user) { build_stubbed(:user) }
+
+ shared_let(:user_cf) { create(:user_custom_field) }
+ shared_let(:admin_user_cf) { create(:user_custom_field, admin_only: true) }
+
+ context "for an admin" do
+ current_user { admin }
+
+ it "returns all fields including admin-only" do
+ expect(user.available_custom_fields)
+ .to contain_exactly(user_cf, admin_user_cf)
+ end
+ end
+
+ context "for a member" do
+ current_user { user }
+
+ it "does not return admin-only field" do
+ expect(user.available_custom_fields)
+ .to contain_exactly(user_cf)
+ end
+ end
+ end
end