2022-08-23 20:12:25 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Groups
|
|
|
|
class AcceptingGroupTransfersFinder < Base
|
2022-09-13 14:12:21 -04:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
2022-08-23 20:12:25 -04:00
|
|
|
def initialize(current_user, group_to_be_transferred, params = {})
|
|
|
|
@current_user = current_user
|
|
|
|
@group_to_be_transferred = group_to_be_transferred
|
|
|
|
@params = params
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
return Group.none unless can_transfer_group?
|
|
|
|
|
2022-09-19 17:13:43 -04:00
|
|
|
items = find_all_groups
|
2022-08-23 20:12:25 -04:00
|
|
|
items = by_search(items)
|
|
|
|
|
|
|
|
sort(items)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
attr_reader :current_user, :group_to_be_transferred, :params
|
|
|
|
|
|
|
|
def find_groups
|
|
|
|
GroupsFinder.new( # rubocop: disable CodeReuse/Finder
|
|
|
|
current_user,
|
|
|
|
min_access_level: Gitlab::Access::OWNER,
|
|
|
|
exclude_group_ids: exclude_groups
|
|
|
|
).execute.without_order
|
|
|
|
end
|
|
|
|
|
2022-09-13 14:12:21 -04:00
|
|
|
def find_all_groups
|
|
|
|
::Namespace.from_union(
|
|
|
|
[
|
|
|
|
find_groups,
|
|
|
|
groups_originating_from_group_shares_with_owner_access
|
|
|
|
]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def groups_originating_from_group_shares_with_owner_access
|
|
|
|
GroupGroupLink
|
|
|
|
.with_owner_access
|
|
|
|
.groups_accessible_via(
|
|
|
|
current_user.owned_groups.select(:id)
|
|
|
|
).id_not_in(exclude_groups)
|
|
|
|
end
|
|
|
|
|
2022-08-23 20:12:25 -04:00
|
|
|
def exclude_groups
|
2022-09-13 14:12:21 -04:00
|
|
|
strong_memoize(:exclude_groups) do
|
|
|
|
exclude_groups = group_to_be_transferred.self_and_descendants.pluck_primary_key
|
|
|
|
exclude_groups << group_to_be_transferred.parent_id if group_to_be_transferred.parent_id
|
2022-08-23 20:12:25 -04:00
|
|
|
|
2022-09-13 14:12:21 -04:00
|
|
|
exclude_groups
|
|
|
|
end
|
2022-08-23 20:12:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def can_transfer_group?
|
|
|
|
Ability.allowed?(current_user, :admin_group, group_to_be_transferred)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|