2018-07-16 12:31:01 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-13 08:45:31 -04:00
|
|
|
module Groups
|
|
|
|
class DestroyService < Groups::BaseService
|
2018-08-13 07:54:31 -04:00
|
|
|
DestroyError = Class.new(StandardError)
|
|
|
|
|
2016-08-13 08:45:31 -04:00
|
|
|
def async_execute
|
|
|
|
job_id = GroupDestroyWorker.perform_async(group.id, current_user.id)
|
2020-05-21 17:08:31 -04:00
|
|
|
Gitlab::AppLogger.info("User #{current_user.id} scheduled a deletion of group ID #{group.id} with job ID #{job_id}")
|
2016-08-13 08:45:31 -04:00
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2016-08-13 08:45:31 -04:00
|
|
|
def execute
|
2017-02-16 02:50:05 -05:00
|
|
|
group.prepare_for_destroy
|
|
|
|
|
2018-08-23 18:52:35 -04:00
|
|
|
group.projects.includes(:project_feature).each do |project|
|
2016-08-13 08:45:31 -04:00
|
|
|
# Execute the destruction of the models immediately to ensure atomic cleanup.
|
2018-08-13 07:54:31 -04:00
|
|
|
success = ::Projects::DestroyService.new(project, current_user).execute
|
|
|
|
raise DestroyError, "Project #{project.id} can't be deleted" unless success
|
2016-08-13 08:45:31 -04:00
|
|
|
end
|
|
|
|
|
2018-08-23 18:52:35 -04:00
|
|
|
# reload the relation to prevent triggering destroy hooks on the projects again
|
2019-04-08 09:33:36 -04:00
|
|
|
group.projects.reset
|
2018-08-23 18:52:35 -04:00
|
|
|
|
2016-08-13 08:45:31 -04:00
|
|
|
group.children.each do |group|
|
2017-02-15 20:15:54 -05:00
|
|
|
# This needs to be synchronous since the namespace gets destroyed below
|
|
|
|
DestroyService.new(group, current_user).execute
|
2016-08-13 08:45:31 -04:00
|
|
|
end
|
|
|
|
|
2017-05-14 06:57:08 -04:00
|
|
|
group.chat_team&.remove_mattermost_team(current_user)
|
|
|
|
|
2021-08-31 17:10:43 -04:00
|
|
|
user_ids_for_project_authorizations_refresh = obtain_user_ids_for_project_authorizations_refresh
|
2020-04-14 23:09:11 -04:00
|
|
|
|
2018-01-02 11:21:28 -05:00
|
|
|
group.destroy
|
2020-04-14 23:09:11 -04:00
|
|
|
|
2021-01-05 07:10:36 -05:00
|
|
|
if user_ids_for_project_authorizations_refresh.present?
|
|
|
|
UserProjectAccessChangedService
|
|
|
|
.new(user_ids_for_project_authorizations_refresh)
|
|
|
|
.execute(blocking: true)
|
|
|
|
end
|
2020-04-14 23:09:11 -04:00
|
|
|
|
|
|
|
group
|
2016-08-13 08:45:31 -04:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2021-01-05 07:10:36 -05:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-08-31 17:10:43 -04:00
|
|
|
def any_groups_shared_with_this_group?
|
2021-01-05 07:10:36 -05:00
|
|
|
group.shared_group_links.any?
|
|
|
|
end
|
2021-08-31 17:10:43 -04:00
|
|
|
|
|
|
|
def any_projects_shared_with_this_group?
|
|
|
|
group.project_group_links.any?
|
|
|
|
end
|
|
|
|
|
|
|
|
# Destroying a group automatically destroys all project authorizations directly
|
|
|
|
# associated with the group and descendents. However, project authorizations
|
|
|
|
# for projects and groups this group is shared with are not. Without a manual
|
|
|
|
# refresh, the project authorization records of these users to shared projects
|
|
|
|
# and projects within the shared groups will never be removed, causing
|
|
|
|
# inconsistencies with access permissions.
|
|
|
|
#
|
|
|
|
# This method retrieves the user IDs that need to be refreshed. If only
|
|
|
|
# groups are shared with this group, only direct members need to be refreshed.
|
|
|
|
# If projects are also shared with the group, direct members *and* shared
|
|
|
|
# members of other groups need to be refreshed.
|
|
|
|
# `Group#user_ids_for_project_authorizations` returns both direct and shared
|
|
|
|
# members' user IDs.
|
|
|
|
def obtain_user_ids_for_project_authorizations_refresh
|
|
|
|
return unless any_projects_shared_with_this_group? || any_groups_shared_with_this_group?
|
|
|
|
return group.user_ids_for_project_authorizations if any_projects_shared_with_this_group?
|
|
|
|
|
|
|
|
group.users_ids_of_direct_members
|
|
|
|
end
|
2016-08-13 08:45:31 -04:00
|
|
|
end
|
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
Groups::DestroyService.prepend_mod_with('Groups::DestroyService')
|