From 75153cb099b166b2e21af1dc5a2f2be3e75dfe9c Mon Sep 17 00:00:00 2001 From: xxq250 Date: Wed, 25 Sep 2024 15:40:53 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=A8=E6=88=B7=E5=88=A0=E9=99=A4=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/admins/users_controller.rb | 39 +++++++++++-------- app/controllers/api/v1/users_controller.rb | 24 ++++++++++++ .../api/v1/users/delete_user_service.rb | 31 +++++++++++++++ config/routes/api.rb | 1 + 4 files changed, 79 insertions(+), 16 deletions(-) create mode 100644 app/services/api/v1/users/delete_user_service.rb diff --git a/app/controllers/admins/users_controller.rb b/app/controllers/admins/users_controller.rb index f5974b47d..f552c9cd1 100644 --- a/app/controllers/admins/users_controller.rb +++ b/app/controllers/admins/users_controller.rb @@ -27,23 +27,30 @@ class Admins::UsersController < Admins::BaseController def destroy UserAction.create(action_id: @user.id, action_type: "DestroyUser", user_id: current_user.id, :ip => request.remote_ip, data_bank: @user.attributes.to_json) - 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 + # 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) + # + # render_delete_success + + @result_object = Api::V1::Users::DeleteUserService.call(@user) + if @result_object + render_delete_success + else + render_js_error('删除失败!') end - @user.destroy! - Gitea::User::DeleteService.call(@user.login, true) - - render_delete_success end def lock diff --git a/app/controllers/api/v1/users_controller.rb b/app/controllers/api/v1/users_controller.rb index 47087c523..8127c557c 100644 --- a/app/controllers/api/v1/users_controller.rb +++ b/app/controllers/api/v1/users_controller.rb @@ -115,4 +115,28 @@ class Api::V1::UsersController < Api::V1::BaseController return render_error('更改手机号失败!') end end + + + def check_user_can_delete + org_ids = TeamUser.where(user_id: @observe_user.id).pluck(:organization_id) | OrganizationUser.where(user_id: @observe_user.id).pluck(:organization_id) + org_count = Organization.where(id: org_ids).count + project_count = Project.where(user_id: @observe_user.id).count + render_ok({ org_count: org_count, project_count: project_count }) + end + + + def destroy + return tip_exception(-1, "密码不正确.") unless @observe_user.check_password?(params[:password]) + org_ids = TeamUser.where(user_id: @observe_user.id).pluck(:organization_id) | OrganizationUser.where(user_id: @observe_user.id).pluck(:organization_id) + org_count = Organization.where(id: org_ids).count + project_count = Project.where(user_id: @observe_user.id).count + return tip_exception(-1, "当前账号名下存在未删除或退出的组织/仓库,请先删除或退出后再尝试注销操作.") if org_count > 0 || project_count > 0 + UserAction.create(action_id: @user.id, action_type: "DestroyUser", user_id: nil, :ip => request.remote_ip, data_bank: @user.attributes.to_json) + @result_object = Api::V1::Users::DeleteUserService.call(@observe_user) + if @result_object + return render_ok + else + return render_error('删除失败!') + end + end end \ No newline at end of file diff --git a/app/services/api/v1/users/delete_user_service.rb b/app/services/api/v1/users/delete_user_service.rb new file mode 100644 index 000000000..67714de06 --- /dev/null +++ b/app/services/api/v1/users/delete_user_service.rb @@ -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 \ No newline at end of file diff --git a/config/routes/api.rb b/config/routes/api.rb index 945a1dc29..91900545b 100644 --- a/config/routes/api.rb +++ b/config/routes/api.rb @@ -58,6 +58,7 @@ defaults format: :json do post :check_email post :check_email_verify_code post :check_phone_verify_code + post :check_user_can_delete patch :update_email patch :update_phone end