Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit select for user cf value options #17350

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading