forgeplus/app/controllers/api/v1/users_controller.rb

142 lines
5.8 KiB
Ruby
Raw Permalink Normal View History

class Api::V1::UsersController < Api::V1::BaseController
2023-04-21 09:23:30 +08:00
before_action :load_observe_user, except: [:check_user_id, :check_user_login]
before_action :check_auth_for_observe_user, except: [:check_user_id, :check_user_login]
2023-04-19 18:11:11 +08:00
2023-04-19 17:32:27 +08:00
def check_user_id
2023-04-19 18:11:11 +08:00
return tip_exception(-1, "用户ID不存在") unless params[:user_id].present? && User.exists?(id: params[:user_id])
2023-04-19 17:32:27 +08:00
render_ok
end
2023-04-20 11:31:34 +08:00
def check_user_login
return tip_exception(-1, "用户标识不存在") unless params[:login].present? && User.exists?(login: params[:login])
render_ok
end
def send_email_vefify_code
code = %W(0 1 2 3 4 5 6 7 8 9)
verification_code = code.sample(6).join
mail = params[:email]
code_type = params[:code_type]
2023-04-19 10:26:07 +08:00
status, message = InfoRiskControlService.call(mail, request.remote_ip)
tip_exception(420, message) if status == 0
sign = Digest::MD5.hexdigest("#{OPENKEY}#{mail}")
Rails.logger.info sign
tip_exception(501, "请求不合理") if sign != params[:smscode]
# 60s内不能重复发送
# send_email_limit_cache_key = "send_email_60_second_limit:#{mail}"
# tip_exception(-2, '请勿频繁操作') if Rails.cache.exist?(send_email_limit_cache_key)
# send_email_control = LimitForbidControl::SendEmailCode.new(mail)
# tip_exception(-2, '邮件发送太频繁,请稍后再试') if send_email_control.forbid?
begin
UserMailer.update_email(mail, verification_code).deliver_now
# Rails.cache.write(send_email_limit_cache_key, 1, expires_in: 1.minute)
# send_email_control.increment!
rescue Exception => e
logger_error(e)
tip_exception(-2,"邮件发送失败,请稍后重试")
end
ver_params = {code_type: code_type, code: verification_code, email: mail}
last_code = VerificationCode.where(code_type: code_type, email: mail).last
last_code.update_attributes!({created_at: Time.current - 10.minute}) if last_code.present?
data = VerificationCode.new(ver_params)
if data.save!
render_ok
else
tip_exception(-1, "创建数据失败")
end
end
def check_password
password = params[:password]
2022-10-21 15:04:26 +08:00
return tip_exception(-5, "8~16位密码支持字母数字和符号") unless password =~ CustomRegexp::PASSWORD
return tip_exception(-5, "密码错误") unless @observe_user.check_password?(password)
render_ok
end
def check_email
mail = strip(params[:email])
2022-10-21 15:04:26 +08:00
return tip_exception(-2, "邮件格式有误") unless mail =~ CustomRegexp::EMAIL
exist_owner = Owner.find_by(mail: mail)
2022-10-21 15:04:26 +08:00
return tip_exception(-2, '邮箱已被使用') if exist_owner
render_ok
end
def check_email_verify_code
code = strip(params[:code])
mail = strip(params[:email])
code_type = params[:code_type]
2022-10-21 15:04:26 +08:00
return tip_exception(-2, "邮件格式有误") unless mail =~ CustomRegexp::EMAIL
verifi_code = VerificationCode.where(email: mail, code: code, code_type: code_type).last
return render_ok if code == "123123" && EduSetting.get("code_debug") # 万能验证码,用于测试 # TODO 万能验证码,用于测试
2022-10-21 15:04:26 +08:00
return tip_exception(-6, "验证码不正确") if verifi_code&.code != code
return tip_exception(-6, "验证码已失效") if !verifi_code&.effective?
render_ok
end
2022-11-24 14:28:49 +08:00
def check_phone_verify_code
code = strip(params[:code])
phone = strip(params[:phone])
code_type = params[:code_type]
2022-11-24 14:30:10 +08:00
return tip_exception(-2, "手机号格式有误") unless phone =~ CustomRegexp::PHONE
2022-11-24 14:28:49 +08:00
verifi_code = VerificationCode.where(phone: phone, code: code, code_type: code_type).last
return render_ok if code == "123123" && EduSetting.get("code_debug") # 万能验证码,用于测试 # TODO 万能验证码,用于测试
return tip_exception(-6, "验证码不正确") if verifi_code&.code != code
return tip_exception(-6, "验证码已失效") if !verifi_code&.effective?
render_ok
end
def update_email
@result_object = Api::V1::Users::UpdateEmailService.call(@observe_user, params, current_user.gitea_token)
if @result_object
return render_ok
else
return render_error('更改邮箱失败!')
end
end
2022-11-24 11:09:58 +08:00
def update_phone
@result_object = Api::V1::Users::UpdatePhoneService.call(@observe_user, params)
if @result_object
return render_ok
else
return render_error('更改手机号失败!')
end
end
2024-09-25 15:40:53 +08:00
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)
2024-09-30 14:48:04 +08:00
org_count = TeamUser.where(organization_id: org_ids).where(user_id: @observe_user.id).joins(:team).where(teams: {authorize: %w(owner)}).count
2024-09-25 15:40:53 +08:00
project_count = Project.where(user_id: @observe_user.id).count
2024-09-25 16:21:33 +08:00
render_ok({ can_delete: org_count == 0 && project_count == 0, org_count: org_count, project_count: project_count })
2024-09-25 15:40:53 +08:00
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)
2024-09-30 14:48:04 +08:00
org_count = TeamUser.where(organization_id: org_ids).where(user_id: @observe_user.id).joins(:team).where(teams: {authorize: %w(owner)}).count
2024-09-25 15:40:53 +08:00
project_count = Project.where(user_id: @observe_user.id).count
2024-10-11 11:42:04 +08:00
return tip_exception(-1, "当前账号名下存在拥有的组织/代码库,请先删除或转让后再尝试注销操作.") if org_count > 0 || project_count > 0
2024-09-27 11:15:45 +08:00
UserAction.create(action_id: @observe_user.id, action_type: "DestroyUser", user_id: @observe_user.id, :ip => request.remote_ip, data_bank: @observe_user.attributes.to_json, memo: params[:memo])
2024-09-25 15:40:53 +08:00
@result_object = Api::V1::Users::DeleteUserService.call(@observe_user)
if @result_object
return render_ok
else
return render_error('删除失败!')
end
end
end