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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

111 lines
3.8 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
2022-05-07 20:08:51 +05:30
# TODO - add a policy check here https://gitlab.com/gitlab-org/gitlab/-/issues/353082
raise DestroyError, "You can't delete this group because you're blocked." if current_user.blocked?
2017-08-17 22:00:37 +05:30
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
2022-05-07 20:08:51 +05:30
2018-11-20 20:47:30 +05:30
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
2022-07-29 17:44:30 +05:30
destroy_group_bots
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)
2023-04-23 21:23:45 +05:30
.execute
2021-03-08 18:12:59 +05:30
end
2020-04-15 14:45:12 +05:30
2022-08-27 11:52:29 +05:30
publish_event
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
2022-07-29 17:44:30 +05:30
# rubocop:disable CodeReuse/ActiveRecord
def destroy_group_bots
bot_ids = group.members_and_requesters.joins(:user).merge(User.project_bot).pluck(:user_id)
current_user_id = current_user.id
group.run_after_commit do
bot_ids.each do |user_id|
DeleteUserWorker.perform_async(current_user_id, user_id, skip_authorization: true)
end
end
end
# rubocop:enable CodeReuse/ActiveRecord
2022-08-27 11:52:29 +05:30
def publish_event
event = Groups::GroupDeletedEvent.new(
data: {
group_id: group.id,
root_namespace_id: group.root_ancestor.id
}
)
Gitlab::EventStore.publish(event)
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')