fixed 授权登录绑定账号

This commit is contained in:
xxq250 2022-12-22 17:04:50 +08:00
parent d821006cb5
commit 3459054349
3 changed files with 31 additions and 24 deletions

View File

@ -153,6 +153,10 @@ class AccountsController < ApplicationController
user.gitea_uid = gitea_user[:body]['id']
if user.save!
UserExtension.create!(user_id: user.id)
# 绑定授权账号
if ["qq", "wechat", "gitee", "github", "educoder"].include?(params[:type].to_s) && session[:unionid].present?
"OpenUsers::#{params[:type].to_s.capitalize}".constantize.create!(user: user, uid: session[:unionid])
end
successful_authentication(user)
render_ok
end
@ -394,7 +398,7 @@ class AccountsController < ApplicationController
end
def register_params
params.permit(:login, :namespace, :password, :password_confirmation, :code)
params.permit(:login, :namespace, :password, :password_confirmation, :code, :type)
end
def reset_password_params

View File

@ -1,9 +1,6 @@
class BindUsersController < ApplicationController
# before_action :require_login
def create
# user = CreateBindUserService.call(create_params)
#
Rails.logger.debug "--------------开始绑定用户------------"
Rails.logger.debug "--------------params: #{params.to_unsafe_h}"
tip_exception '系统错误' if session[:unionid].blank?
@ -11,27 +8,11 @@ class BindUsersController < ApplicationController
bind_user = User.try_to_login(params[:username], params[:password])
tip_exception '用户名或者密码错误' if bind_user.blank?
tip_exception '用户名或者密码错误' unless bind_user.check_password?(params[:password].to_s)
tip_exception '参数错误' unless ["qq", "wechat", "gitee", "github", "educoder"].include?(params[:type].to_s)
tip_exception '该账号已被绑定,请更换其他账号进行绑定' if bind_user.bind_open_user?(params[:type].to_s)
if params[:type] == "qq"
begin
OpenUsers::QQ.create!(user: bind_user, uid: session[:unionid])
successful_authentication(bind_user)
"OpenUsers::#{params[:type].to_s.capitalize}".constantize.create!(user: bind_user, uid: session[:unionid])
render_ok
rescue ApplicationService::Error => ex
render_error(ex.message)
end
else
begin
OpenUsers::Wechat.create!(user: bind_user, uid: session[:unionid])
successful_authentication(bind_user)
render_ok
rescue Exception => e
render_error(e.message)
end
end
end
def new_user

View File

@ -1,6 +1,6 @@
class Oauth::CallbacksController < Oauth::BaseController
def create
process_callback
process_callback_new
rescue Exception => e
Rails.logger.info "授权失败:#{e}"
tip_exception("授权失败")
@ -57,6 +57,28 @@ class Oauth::CallbacksController < Oauth::BaseController
redirect_to root_path(new_user: new_user)
end
def process_callback_new
Rails.logger.info("[OAuth2] omniauth.auth -> #{request.env['omniauth.auth'].inspect}")
if auth_hash.blank?
redirect_to("/login") && return
end
platform = auth_hash[:provider]
uid = auth_hash[:uid]
uid = auth_hash.info.unionid if platform == "wechat"
open_user = "OpenUsers::#{platform.to_s.capitalize}".constantize.find_by(uid: uid)
if open_user.present? && open_user.user.present?
successful_authentication(open_user.user)
else
if current_user.blank? || !current_user.logged?
session[:unionid] = uid
else
"OpenUsers::#{platform.to_s.capitalize}".constantize.create!(user: current_user, uid: uid)
end
end
redirect_to "/bindlogin/#{platform}"
end
# gitee,github nickname=login,如果系统未占用保留原用户名
def build_login_name(provider, nickname)
if ["gitee", "github"].include?(provider) && User.find_by(login: nickname).blank?