Commit d39de0ea authored by Yorick Peterse's avatar Yorick Peterse

Resolved merge conflicts in ProjectsFinder

This also required some changes to the specs (as they weren't entirely
working correctly) and the ProjectsFinder itself to make it possible to
UNION more than 2 queries together.
parent a07be7bc
......@@ -23,17 +23,17 @@ class ProjectsFinder
group = options[:group]
if group
base, extra = group_projects(current_user, group)
segments = group_projects(current_user, group)
else
base, extra = all_projects(current_user)
segments = all_projects(current_user)
end
if base and extra
union = Gitlab::SQL::Union.new([base.select(:id), extra.select(:id)])
if segments.length > 1
union = Gitlab::SQL::Union.new(segments.map { |s| s.select(:id) })
Project.where("projects.id IN (#{union.to_sql})")
else
base
segments.first
end
end
......@@ -41,57 +41,11 @@ class ProjectsFinder
def group_projects(current_user, group)
if current_user
<<<<<<< HEAD
if group.users.include?(current_user)
# User is group member
#
# Return ALL group projects
group.projects
else
projects_members = ProjectMember.in_projects(group.projects).
with_user(current_user)
if projects_members.any?
# User is a project member
#
# Return only:
# public projects
# internal projects
# joined projects
#
group.projects.where(
"projects.id IN (?) OR projects.visibility_level IN (?)",
projects_members.pluck(:source_id),
Project.public_and_internal_levels
)
else
# User has no access to group or group projects
# or has access through shared project
#
# Return only:
# public projects
# internal projects
# shared projects
projects_ids = []
ProjectGroupLink.where(project_id: group.projects).each do |shared_project|
if shared_project.group.users.include?(current_user) || shared_project.project.users.include?(current_user)
projects_ids << shared_project.project.id
end
end
group.projects.where(
"projects.id IN (?) OR projects.visibility_level IN (?)",
projects_ids,
Project.public_and_internal_levels
)
end
end
=======
[
group_projects_for_user(current_user, group),
group.projects.public_and_internal_only
group.projects.public_and_internal_only,
group.shared_projects.visible_to_user(current_user)
]
>>>>>>> b6f0eddce552d7423869e9072a7a0706e309dbdf
else
[group.projects.public_only]
end
......
require 'spec_helper'
describe ProjectsFinder do
<<<<<<< HEAD
let(:user) { create :user }
let(:group) { create :group }
let(:group2) { create :group }
let(:project1) { create(:empty_project, :public, group: group) }
let(:project2) { create(:empty_project, :internal, group: group) }
let(:project3) { create(:empty_project, :private, group: group) }
let(:project4) { create(:empty_project, :private, group: group) }
let(:project5) { create(:empty_project, :private, group: group2) }
let(:project6) { create(:empty_project, :internal, group: group2) }
let(:project7) { create(:empty_project, :public, group: group2) }
let(:project8) { create(:empty_project, :private, group: group2) }
=======
describe '#execute' do
let(:user) { create(:user) }
let(:group) { create(:group) }
let!(:private_project) { create(:project, :private) }
let!(:internal_project) { create(:project, :internal) }
let!(:public_project) { create(:project, :public) }
>>>>>>> b6f0eddce552d7423869e9072a7a0706e309dbdf
let!(:private_project) do
create(:project, :private, name: 'A', path: 'A')
end
let!(:internal_project) do
create(:project, :internal, group: group, name: 'B', path: 'B')
end
let!(:public_project) do
create(:project, :public, group: group, name: 'C', path: 'C')
end
let!(:shared_project) do
create(:project, :private, name: 'D', path: 'D')
end
let(:finder) { described_class.new }
......@@ -53,8 +51,6 @@ describe ProjectsFinder do
end
describe 'with a group' do
let(:group) { public_project.group }
describe 'without a user' do
subject { finder.execute(nil, group: group) }
......@@ -64,22 +60,36 @@ describe ProjectsFinder do
describe 'with a user' do
subject { finder.execute(user, group: group) }
describe 'without shared projects' do
it { is_expected.to eq([public_project, internal_project]) }
end
end
end
context 'authenticated, group member with project shared with group' do
describe 'with shared projects and group membership' do
before do
group.add_user(user, Gitlab::Access::DEVELOPER)
project5.project_group_links.create group_access: Gitlab::Access::MASTER, group: group
shared_project.project_group_links.
create(group_access: Gitlab::Access::MASTER, group: group)
end
subject { ProjectsFinder.new.execute(user, group: group2) }
it do
is_expected.to eq([shared_project, public_project, internal_project])
end
end
describe 'with shared projects and project membership' do
before do
shared_project.team.add_user(user, Gitlab::Access::DEVELOPER)
shared_project.project_group_links.
create(group_access: Gitlab::Access::MASTER, group: group)
end
it { should include(project5) }
it { should include(project6) }
it { should include(project7) }
it { should_not include(project8) }
it do
is_expected.to eq([shared_project, public_project, internal_project])
end
end
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment