mirror of
https://gitlink.org.cn/Gitlink/forgeplus.git
synced 2026-05-17 02:05:58 +08:00
用户删除接口
This commit is contained in:
31
app/services/api/v1/users/delete_user_service.rb
Normal file
31
app/services/api/v1/users/delete_user_service.rb
Normal 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
|
||||
Reference in New Issue
Block a user