2018-07-16 16:31:01 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-13 12:45:31 +00:00
|
|
|
module Groups
|
|
|
|
class DestroyService < Groups::BaseService
|
2018-08-13 11:54:31 +00:00
|
|
|
DestroyError = Class.new(StandardError)
|
|
|
|
|
2016-08-13 12:45:31 +00:00
|
|
|
def async_execute
|
|
|
|
job_id = GroupDestroyWorker.perform_async(group.id, current_user.id)
|
2020-05-21 21:08:31 +00:00
|
|
|
Gitlab::AppLogger.info("User #{current_user.id} scheduled a deletion of group ID #{group.id} with job ID #{job_id}")
|
2016-08-13 12:45:31 +00:00
|
|
|
end
|
|
|
|
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2016-08-13 12:45:31 +00:00
|
|
|
def execute
|
2017-02-16 07:50:05 +00:00
|
|
|
group.prepare_for_destroy
|
|
|
|
|
2018-08-23 22:52:35 +00:00
|
|
|
group.projects.includes(:project_feature).each do |project|
|
2016-08-13 12:45:31 +00:00
|
|
|
# Execute the destruction of the models immediately to ensure atomic cleanup.
|
2018-08-13 11:54:31 +00:00
|
|
|
success = ::Projects::DestroyService.new(project, current_user).execute
|
|
|
|
raise DestroyError, "Project #{project.id} can't be deleted" unless success
|
2016-08-13 12:45:31 +00:00
|
|
|
end
|
|
|
|
|
2018-08-23 22:52:35 +00:00
|
|
|
# reload the relation to prevent triggering destroy hooks on the projects again
|
2019-04-08 13:33:36 +00:00
|
|
|
group.projects.reset
|
2018-08-23 22:52:35 +00:00
|
|
|
|
2016-08-13 12:45:31 +00:00
|
|
|
group.children.each do |group|
|
2017-02-16 01:15:54 +00:00
|
|
|
# This needs to be synchronous since the namespace gets destroyed below
|
|
|
|
DestroyService.new(group, current_user).execute
|
2016-08-13 12:45:31 +00:00
|
|
|
end
|
|
|
|
|
2017-05-14 10:57:08 +00:00
|
|
|
group.chat_team&.remove_mattermost_team(current_user)
|
|
|
|
|
2021-01-05 12:10:36 +00:00
|
|
|
# If any other groups are shared with the group that is being destroyed,
|
|
|
|
# we should specifically trigger update of all project authorizations
|
|
|
|
# for users that are the members of this group.
|
|
|
|
# If not, the project authorization records of these users to projects within the shared groups
|
|
|
|
# will never be removed, causing inconsistencies with access permissions.
|
|
|
|
if any_other_groups_are_shared_with_this_group?
|
|
|
|
user_ids_for_project_authorizations_refresh = group.user_ids_for_project_authorizations
|
|
|
|
end
|
2020-04-15 03:09:11 +00:00
|
|
|
|
2018-01-02 16:21:28 +00:00
|
|
|
group.destroy
|
2020-04-15 03:09:11 +00:00
|
|
|
|
2021-01-05 12:10:36 +00:00
|
|
|
if user_ids_for_project_authorizations_refresh.present?
|
|
|
|
UserProjectAccessChangedService
|
|
|
|
.new(user_ids_for_project_authorizations_refresh)
|
|
|
|
.execute(blocking: true)
|
|
|
|
end
|
2020-04-15 03:09:11 +00:00
|
|
|
|
|
|
|
group
|
2016-08-13 12:45:31 +00:00
|
|
|
end
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2021-01-05 12:10:36 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def any_other_groups_are_shared_with_this_group?
|
|
|
|
group.shared_group_links.any?
|
|
|
|
end
|
2016-08-13 12:45:31 +00:00
|
|
|
end
|
|
|
|
end
|
2019-09-13 13:26:31 +00:00
|
|
|
|
|
|
|
Groups::DestroyService.prepend_if_ee('EE::Groups::DestroyService')
|