用户删除接口

This commit is contained in:
2024-09-25 15:40:53 +08:00
parent 3cce800746
commit 75153cb099
4 changed files with 79 additions and 16 deletions

View File

@@ -0,0 +1,31 @@
class Api::V1::Users::DeleteUserService < ApplicationService
attr_reader :user
def initialize(user)
@user = user
end
def call
begin
ActiveRecord::Base.transaction do
org_ids = TeamUser.where(user_id: @user.id).pluck(:organization_id) | OrganizationUser.where(user_id: @user.id).pluck(:organization_id)
organizations = Organization.where(id: org_ids)
organizations.each do |org|
# org.team_users.joins(:team).where(user_id: @user.id, teams: {authorize: %w(owner)})
owner_count = org.team_users.joins(:team).where(teams: {authorize: %w(owner)}).count
# 多个owner时,仅将用户从组织移除, 一个时直接删除
if owner_count > 1
org.team_users.joins(:team).where(user_id: @user.id, teams: {authorize: %w(owner)}).destroy_all
org.organization_users.where(user_id: @user.id, organization_id: org.id).destroy_all
else
org.destroy
end
end
@user.destroy!
Gitea::User::DeleteService.call(@user.login, true)
end
return true
rescue
raise Error, "服务器错误,请联系系统管理员!"
end
end
end