debian-mirror-gitlab/app/services/groups/destroy_service.rb

79 lines
2.9 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
module Groups
class DestroyService < Groups::BaseService
2018-11-20 20:47:30 +05:30
DestroyError = Class.new(StandardError)
2017-08-17 22:00:37 +05:30
def async_execute
job_id = GroupDestroyWorker.perform_async(group.id, current_user.id)
2020-06-23 00:09:42 +05:30
Gitlab::AppLogger.info("User #{current_user.id} scheduled a deletion of group ID #{group.id} with job ID #{job_id}")
2017-08-17 22:00:37 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2017-08-17 22:00:37 +05:30
def execute
group.prepare_for_destroy
2018-11-20 20:47:30 +05:30
group.projects.includes(:project_feature).each do |project|
2017-08-17 22:00:37 +05:30
# Execute the destruction of the models immediately to ensure atomic cleanup.
2018-11-20 20:47:30 +05:30
success = ::Projects::DestroyService.new(project, current_user).execute
raise DestroyError, "Project #{project.id} can't be deleted" unless success
2017-08-17 22:00:37 +05:30
end
2018-11-20 20:47:30 +05:30
# reload the relation to prevent triggering destroy hooks on the projects again
2019-07-31 22:56:46 +05:30
group.projects.reset
2018-11-20 20:47:30 +05:30
2017-08-17 22:00:37 +05:30
group.children.each do |group|
# This needs to be synchronous since the namespace gets destroyed below
DestroyService.new(group, current_user).execute
end
2017-09-10 17:25:29 +05:30
group.chat_team&.remove_mattermost_team(current_user)
2021-09-04 01:27:46 +05:30
user_ids_for_project_authorizations_refresh = obtain_user_ids_for_project_authorizations_refresh
2020-04-15 14:45:12 +05:30
2018-03-17 18:26:18 +05:30
group.destroy
2020-04-15 14:45:12 +05:30
2021-03-08 18:12:59 +05:30
if user_ids_for_project_authorizations_refresh.present?
UserProjectAccessChangedService
.new(user_ids_for_project_authorizations_refresh)
.execute(blocking: true)
end
2020-04-15 14:45:12 +05:30
group
2017-08-17 22:00:37 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2021-03-08 18:12:59 +05:30
private
2021-09-04 01:27:46 +05:30
def any_groups_shared_with_this_group?
2021-03-08 18:12:59 +05:30
group.shared_group_links.any?
end
2021-09-04 01:27:46 +05:30
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
2017-08-17 22:00:37 +05:30
end
end
2019-12-04 20:38:33 +05:30
2021-06-08 01:23:25 +05:30
Groups::DestroyService.prepend_mod_with('Groups::DestroyService')