Skip to content
Snippets Groups Projects
Commit 90790adc authored by Vasilii Iakliushin's avatar Vasilii Iakliushin
Browse files

Combine the logic in GroupsFinder

parent 6bf573a4
No related merge requests found
......@@ -42,8 +42,16 @@ def execute
attr_reader :current_user, :params
def project
params[:project]
end
def authorized?
true
if project
Ability.allowed?(current_user, :read_project, project)
else
true
end
end
def apply_filters_on(item)
......@@ -54,17 +62,37 @@ def apply_filters_on(item)
item
end
# rubocop: disable CodeReuse/ActiveRecord
# rubocop: disable Metrics/CyclomaticComplexity
# rubocop: disable Metrics/PerceivedComplexity
def all_groups
return [owned_groups] if params[:owned]
return [groups_with_min_access_level] if min_access_level?
return [Group.all] if current_user&.can_read_all_resources? && all_available?
groups = []
groups << Gitlab::ObjectHierarchy.new(groups_for_ancestors, groups_for_descendants).all_objects if current_user
groups << Group.unscoped.public_to_user(current_user) if include_public_groups?
groups << Gitlab::ObjectHierarchy.new(groups_for_ancestors, groups_for_descendants).all_objects if current_user && !project
groups << project.group.self_and_ancestors if project&.group
if params[:with_shared]
shared_groups = project.invited_groups
if params[:shared_min_access_level]
shared_groups = shared_groups.where(
'project_group_links.group_access >= ?', params[:shared_min_access_level]
)
end
groups << shared_groups
end
groups << Group.unscoped.public_to_user(current_user) if include_public_groups? && !project
groups << Group.none if groups.empty?
groups
end
# rubocop: enable Metrics/CyclomaticComplexity
# rubocop: enable Metrics/PerceivedComplexity
# rubocop: enable CodeReuse/ActiveRecord
def groups_for_ancestors
current_user.authorized_groups
......
# frozen_string_literal: true
# ProjectGroupsFinder
#
# Used to filter ancestor and shared project's Groups by a set of params
#
# Arguments:
# project
# current_user - which user is requesting groups
# params:
# with_shared: boolean (optional)
# shared_min_access_level: integer (optional)
# skip_groups: array of integers (optional)
#
class ProjectGroupsFinder < GroupsFinder
def initialize(project:, current_user: nil, params: {})
@project = project
super(current_user, params)
end
private
attr_reader :project
def authorized?
Ability.allowed?(current_user, :read_project, project)
end
# rubocop: disable CodeReuse/ActiveRecord
def all_groups
groups = []
groups << project.group.self_and_ancestors if project.group
if params[:with_shared]
shared_groups = project.invited_groups
if params[:shared_min_access_level]
shared_groups = shared_groups.where(
'project_group_links.group_access >= ?', params[:shared_min_access_level]
)
end
groups << shared_groups
end
groups << Group.none if groups.compact.empty?
groups
end
# rubocop: enable CodeReuse/ActiveRecord
def apply_filters_on(item)
item = exclude_group_ids(item)
item
end
end
......@@ -2,7 +2,7 @@
require 'spec_helper'
RSpec.describe ProjectGroupsFinder do
RSpec.describe GroupsFinder do
include AdminModeHelper
describe '#execute' do
......@@ -24,7 +24,7 @@
let(:params) { {} }
let(:current_user) { user }
let(:finder) { described_class.new(project: project, current_user: current_user, params: params) }
let(:finder) { described_class.new(current_user, params.merge(project: project)) }
subject { finder.execute }
......@@ -95,11 +95,5 @@
end
end
end
describe 'Missing project' do
let(:project) { nil }
it { is_expected.to eq([]) }
end
end
end
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