新增:更改邮箱service逻辑
This commit is contained in:
parent
649d9c4ea6
commit
d68e7f38a3
|
@ -3,10 +3,11 @@ class Api::V1::Users::UsersController < Api::V1::BaseController
|
||||||
before_action :load_observe_user
|
before_action :load_observe_user
|
||||||
before_action :check_auth_for_observe_user
|
before_action :check_auth_for_observe_user
|
||||||
|
|
||||||
def send_update_email_vefify_code
|
def send_vefify_code
|
||||||
code = %W(0 1 2 3 4 5 6 7 8 9)
|
code = %W(0 1 2 3 4 5 6 7 8 9)
|
||||||
verification_code = code.sample(6).join
|
verification_code = code.sample(6).join
|
||||||
mail = params[:email]
|
mail = params[:email]
|
||||||
|
code_type = params[:code_type]
|
||||||
|
|
||||||
sign = Digest::MD5.hexdigest("#{OPENKEY}#{value}")
|
sign = Digest::MD5.hexdigest("#{OPENKEY}#{value}")
|
||||||
tip_exception(501, "请求不合理") if sign != params[:smscode]
|
tip_exception(501, "请求不合理") if sign != params[:smscode]
|
||||||
|
@ -25,7 +26,7 @@ class Api::V1::Users::UsersController < Api::V1::BaseController
|
||||||
logger_error(e)
|
logger_error(e)
|
||||||
tip_exception(-2,"邮件发送失败,请稍后重试")
|
tip_exception(-2,"邮件发送失败,请稍后重试")
|
||||||
end
|
end
|
||||||
ver_params = {code_type: send_type, code: code, email: mail}
|
ver_params = {code_type: code_type, code: code, email: mail}
|
||||||
data = VerificationCode.new(ver_params)
|
data = VerificationCode.new(ver_params)
|
||||||
if data.save!
|
if data.save!
|
||||||
render_ok
|
render_ok
|
||||||
|
@ -53,10 +54,11 @@ class Api::V1::Users::UsersController < Api::V1::BaseController
|
||||||
def check_email_verifi_code
|
def check_email_verifi_code
|
||||||
code = strip(params[:code])
|
code = strip(params[:code])
|
||||||
mail = strip(params[:email])
|
mail = strip(params[:email])
|
||||||
|
code_type = params[:code_type]
|
||||||
|
|
||||||
return render_error("邮件格式有误") unless mail =~ CustomRegexp::EMAIL
|
return render_error("邮件格式有误") unless mail =~ CustomRegexp::EMAIL
|
||||||
|
|
||||||
verifi_code = VerificationCode.where(email: mail, code: code, code_type: 8).last
|
verifi_code = VerificationCode.where(email: mail, code: code, code_type: code_type).last
|
||||||
|
|
||||||
return render_error("验证码不正确") if verifi_code&.code != code
|
return render_error("验证码不正确") if verifi_code&.code != code
|
||||||
return render_error("验证码已失效") if !verifi_code&.effective?
|
return render_error("验证码已失效") if !verifi_code&.effective?
|
||||||
|
|
|
@ -0,0 +1,63 @@
|
||||||
|
class Api::V1::Users::UpdateEmailService < ApplicationService
|
||||||
|
include ActiveModel::Model
|
||||||
|
|
||||||
|
attr_reader :user, :token, :password, :mail, :old_mail, :code, :verify_code
|
||||||
|
attr_accessor :gitea_token
|
||||||
|
|
||||||
|
validates :mail
|
||||||
|
|
||||||
|
def initialize(user, params, token =nil)
|
||||||
|
@user = user
|
||||||
|
@token = token
|
||||||
|
@password = params[:password]
|
||||||
|
@mail = params[:email]
|
||||||
|
@old_mail = user.mail
|
||||||
|
@code = params[:code]
|
||||||
|
@verify_code = VerificationCode.where(email: @mail, code: @code, code_type: params[:code_type]).last
|
||||||
|
end
|
||||||
|
|
||||||
|
def call
|
||||||
|
raise Error, errors.full_messages.join(",") unless valid?
|
||||||
|
raise Error, "密码不正确." if @user.check_password?(@password)
|
||||||
|
raise Error, "验证码不正确." if @verifi_code&.code != @code
|
||||||
|
raise Error, "验证码已失效." if !verifi_code&.effective?
|
||||||
|
|
||||||
|
ActiveRecord::Base.transaction do
|
||||||
|
change_user_email
|
||||||
|
excute_data_to_gitea
|
||||||
|
excute_remove_email_from_gitea
|
||||||
|
end
|
||||||
|
|
||||||
|
return gitea_data
|
||||||
|
|
||||||
|
rescue
|
||||||
|
raise Error, "服务器错误,请联系系统管理员!"
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
def request_params
|
||||||
|
{
|
||||||
|
access_token: token
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
def request_body
|
||||||
|
{
|
||||||
|
email: @email,
|
||||||
|
login_name: @user.login,
|
||||||
|
source_id: 0
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
def change_user_email
|
||||||
|
@user.update_attributes!({mail: @mail})
|
||||||
|
end
|
||||||
|
|
||||||
|
def excute_data_to_gitea
|
||||||
|
@gitea_token = $gitea_client.patch_admin_users_by_username(@user.login, {body: request_body.to_json})
|
||||||
|
end
|
||||||
|
|
||||||
|
def excute_remove_email_from_gitea
|
||||||
|
@gitea_token = $gitea_client.delete_user_emails({body: {emails: [@old_mail]}, query: request_params})
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in New Issue