From 3459054349e8997e39a58801e02e35aca9c43a02 Mon Sep 17 00:00:00 2001 From: xxq250 Date: Thu, 22 Dec 2022 17:04:50 +0800 Subject: [PATCH] =?UTF-8?q?fixed=20=E6=8E=88=E6=9D=83=E7=99=BB=E5=BD=95?= =?UTF-8?q?=E7=BB=91=E5=AE=9A=E8=B4=A6=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/accounts_controller.rb | 6 ++++- app/controllers/bind_users_controller.rb | 25 +++---------------- app/controllers/oauth/callbacks_controller.rb | 24 +++++++++++++++++- 3 files changed, 31 insertions(+), 24 deletions(-) diff --git a/app/controllers/accounts_controller.rb b/app/controllers/accounts_controller.rb index 28313f169..4a104129b 100644 --- a/app/controllers/accounts_controller.rb +++ b/app/controllers/accounts_controller.rb @@ -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 diff --git a/app/controllers/bind_users_controller.rb b/app/controllers/bind_users_controller.rb index 353842efe..764e15910 100644 --- a/app/controllers/bind_users_controller.rb +++ b/app/controllers/bind_users_controller.rb @@ -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) - - 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 + "OpenUsers::#{params[:type].to_s.capitalize}".constantize.create!(user: bind_user, uid: session[:unionid]) + render_ok end def new_user diff --git a/app/controllers/oauth/callbacks_controller.rb b/app/controllers/oauth/callbacks_controller.rb index b97fdc023..8a06a6611 100644 --- a/app/controllers/oauth/callbacks_controller.rb +++ b/app/controllers/oauth/callbacks_controller.rb @@ -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?