Skip to content

Commit

Permalink
limit select for user cf value options
Browse files Browse the repository at this point in the history
  • Loading branch information
ulferts committed Dec 4, 2024
1 parent 478d308 commit 7efed5b
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 14 deletions.
15 changes: 9 additions & 6 deletions app/models/custom_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -326,12 +326,15 @@ def possible_version_values_options(obj)

def possible_user_values_options(obj)
mapped_with_deduced_project(obj) do |project|
if project&.persisted?
project.principals
else
Principal
.in_visible_project_or_me(User.current)
end
scope = if project&.persisted?
project.principals
else
Principal
.in_visible_project_or_me(User.current)
end

scope
.select(*(User::USER_FORMATS_STRUCTURE[Setting.user_format].map(&:to_s) << "id"))
end
end

Expand Down
9 changes: 7 additions & 2 deletions spec/lib/custom_field_form_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@
let(:project) { build_stubbed(:project) }
let(:user1) { build_stubbed(:user) }
let(:user2) { build_stubbed(:user) }
let(:scope) { instance_double(ActiveRecord::Relation) }

let(:resource) { project }

Expand All @@ -268,8 +269,12 @@
end

allow(project)
.to(receive(:principals))
.and_return([user1, user2])
.to receive(:principals)
.and_return(scope)

allow(scope)
.to receive(:select)
.and_return([user1, user2])
end

it_behaves_like "wrapped in container", "select-container" do
Expand Down
18 changes: 14 additions & 4 deletions spec/models/custom_actions/actions/custom_field_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -441,12 +441,17 @@
build_stubbed(:user),
build_stubbed(:user)]
end
let(:scope) { instance_double(ActiveRecord::Relation) }

before do
allow(Principal)
.to receive(:in_visible_project_or_me)
.with(User.current)
.and_return(users)
.with(User.current)
.and_return(scope)

allow(scope)
.to receive(:select)
.and_return(users)
end

context "for a non required field" do
Expand Down Expand Up @@ -515,12 +520,17 @@
build_stubbed(:user),
build_stubbed(:user)]
end
let(:scope) { instance_double(ActiveRecord::Relation) }

before do
allow(Principal)
.to receive(:in_visible_project_or_me)
.with(User.current)
.and_return(users)
.with(User.current)
.and_return(scope)

allow(scope)
.to receive(:select)
.and_return(users)
end

it_behaves_like "associated custom action validations" do
Expand Down
14 changes: 12 additions & 2 deletions spec/models/custom_field_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -215,17 +215,27 @@
let(:project) { build_stubbed(:project) }
let(:user1) { build_stubbed(:user) }
let(:user2) { build_stubbed(:user) }
let(:in_visible_scope) { instance_double(ActiveRecord::Relation) }
let(:principals_scope) { instance_double(ActiveRecord::Relation) }

context "for a user custom field" do
before do
field.field_format = "user"
allow(project)
.to receive(:principals)
.and_return([user1, user2])
.and_return(principals_scope)

allow(principals_scope)
.to receive(:select)
.and_return([user1, user2])

allow(Principal)
.to receive(:in_visible_project_or_me)
.and_return([user2])
.and_return(in_visible_scope)

allow(in_visible_scope)
.to receive(:select)
.and_return([user2])
end

context "for a project" do
Expand Down

0 comments on commit 7efed5b

Please sign in to comment.