| @@ -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 | |||
| @@ -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 | |||
| @@ -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 | |||
| @@ -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 | |||