2023-04-23 21:23:45 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# AcceptingProjectSharesFinder
|
|
|
|
#
|
|
|
|
# Used to filter Shareable Groups by a set of params
|
|
|
|
#
|
|
|
|
# Arguments:
|
|
|
|
# current_user - which user is requesting groups
|
|
|
|
# params:
|
|
|
|
# search: string
|
|
|
|
module Groups
|
|
|
|
class AcceptingProjectSharesFinder < Base
|
|
|
|
def initialize(current_user, project_to_be_shared, params = {})
|
|
|
|
@current_user = current_user
|
|
|
|
@params = params
|
|
|
|
@project_to_be_shared = project_to_be_shared
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
return Group.none unless can_share_project?
|
|
|
|
|
|
|
|
groups = if has_admin_access?
|
|
|
|
Group.all
|
|
|
|
else
|
|
|
|
groups_with_guest_access_plus
|
|
|
|
end
|
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
groups = by_hierarchy(groups)
|
|
|
|
groups = by_ignorable(groups)
|
2023-05-27 22:25:52 +05:30
|
|
|
groups = by_search(groups)
|
2023-04-23 21:23:45 +05:30
|
|
|
|
|
|
|
sort(groups).with_route
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
attr_reader :current_user, :project_to_be_shared, :params
|
|
|
|
|
|
|
|
def has_admin_access?
|
|
|
|
current_user&.can_read_all_resources?
|
|
|
|
end
|
|
|
|
|
|
|
|
# rubocop: disable CodeReuse/Finder
|
|
|
|
def groups_with_guest_access_plus
|
|
|
|
GroupsFinder.new(current_user, min_access_level: Gitlab::Access::GUEST).execute
|
|
|
|
end
|
|
|
|
# rubocop: enable CodeReuse/Finder
|
|
|
|
|
|
|
|
def can_share_project?
|
|
|
|
Ability.allowed?(current_user, :admin_project, project_to_be_shared) &&
|
|
|
|
project_to_be_shared.allowed_to_share_with_group?
|
|
|
|
end
|
2023-06-20 00:43:36 +05:30
|
|
|
|
|
|
|
def by_ignorable(groups)
|
|
|
|
# groups already linked to this project or groups above the project's
|
|
|
|
# current hierarchy needs to be ignored.
|
|
|
|
groups.id_not_in(project_to_be_shared.related_group_ids)
|
|
|
|
end
|
|
|
|
|
|
|
|
def by_hierarchy(groups)
|
|
|
|
return groups if project_to_be_shared.personal? || sharing_outside_hierarchy_allowed?
|
|
|
|
|
|
|
|
groups.id_in(root_ancestor.self_and_descendants_ids)
|
|
|
|
end
|
|
|
|
|
|
|
|
def sharing_outside_hierarchy_allowed?
|
|
|
|
!root_ancestor.prevent_sharing_groups_outside_hierarchy
|
|
|
|
end
|
|
|
|
|
|
|
|
def root_ancestor
|
|
|
|
project_to_be_shared.root_ancestor
|
|
|
|
end
|
2023-04-23 21:23:45 +05:30
|
|
|
end
|
|
|
|
end
|