Merge branch 'develop' of https://gitlink.org.cn/Trustie/forgeplus into develop

This commit is contained in:
yystopf 2022-12-08 11:52:06 +08:00
commit dfa0f1161d
6 changed files with 13 additions and 10 deletions

View File

@ -2,6 +2,7 @@ class Oauth::CallbacksController < Oauth::BaseController
def create def create
process_callback process_callback
rescue Exception => e rescue Exception => e
Rails.logger.info "授权失败:#{e}"
tip_exception("授权失败") tip_exception("授权失败")
end end
@ -38,7 +39,9 @@ class Oauth::CallbacksController < Oauth::BaseController
new_user = true new_user = true
login = build_login_name(platform, auth_hash.info.nickname) login = build_login_name(platform, auth_hash.info.nickname)
mail = "#{login}@example.org" if mail.blank? mail = "#{login}@example.org" if mail.blank?
reg_result = autologin_register(login, mail, "Ec#{login}2022#", platform, nil, nickname) code = %W(0 1 2 3 4 5 6 7 8 9)
rand_password = code.sample(10).join
reg_result = autologin_register(login, mail, rand_password, platform, nil, nickname)
Rails.logger.info("[OAuth2] omniauth.auth [reg_result] #{reg_result} ") Rails.logger.info("[OAuth2] omniauth.auth [reg_result] #{reg_result} ")
if reg_result[:message].blank? if reg_result[:message].blank?
open_user = "OpenUsers::#{platform.to_s.capitalize}".constantize.create!(user_id: reg_result[:user][:id], uid: uid, extra: auth_hash.extra) open_user = "OpenUsers::#{platform.to_s.capitalize}".constantize.create!(user_id: reg_result[:user][:id], uid: uid, extra: auth_hash.extra)

View File

@ -16,7 +16,7 @@
# index_open_users_on_user_id (user_id) # index_open_users_on_user_id (user_id)
# #
class OpenUsers::QQ < OpenUser class OpenUsers::Qq < OpenUser
def nickname def nickname
extra&.[]('nickname') extra&.[]('nickname')
end end

View File

@ -119,7 +119,7 @@ class User < Owner
has_one :user_extension, dependent: :destroy has_one :user_extension, dependent: :destroy
has_many :open_users, dependent: :destroy has_many :open_users, dependent: :destroy
has_one :wechat_open_user, class_name: 'OpenUsers::Wechat' has_one :wechat_open_user, class_name: 'OpenUsers::Wechat'
has_one :qq_open_user, class_name: 'OpenUsers::QQ' has_one :qq_open_user, class_name: 'OpenUsers::Qq'
accepts_nested_attributes_for :user_extension, update_only: true accepts_nested_attributes_for :user_extension, update_only: true
has_many :fork_users, dependent: :destroy has_many :fork_users, dependent: :destroy

View File

@ -10,7 +10,7 @@ class Oauth::CreateOrFindQqAccountService < ApplicationService
def call def call
new_user = false new_user = false
# 存在该用户 # 存在该用户
open_user = OpenUsers::QQ.find_by(uid: params['uid']) open_user = OpenUsers::Qq.find_by(uid: params['uid'])
return [open_user.user, new_user] if open_user.present? return [open_user.user, new_user] if open_user.present?
if user.blank? || !user.logged? if user.blank? || !user.logged?
@ -32,7 +32,7 @@ class Oauth::CreateOrFindQqAccountService < ApplicationService
Util.download_file(params.dig('info', 'image'), avatar_path) Util.download_file(params.dig('info', 'image'), avatar_path)
end end
new_open_user = OpenUsers::QQ.create!(user: user, uid: params['uid']) new_open_user = OpenUsers::Qq.create!(user: user, uid: params['uid'])
Rails.cache.write(new_open_user.can_bind_cache_key, 1, expires_in: 1.hours) if new_user # 方便后面进行账号绑定 Rails.cache.write(new_open_user.can_bind_cache_key, 1, expires_in: 1.hours) if new_user # 方便后面进行账号绑定
end end

View File

@ -1,9 +1,9 @@
config = Rails.application.config_for(:configuration) config = Rails.application.config_for(:configuration)
OmniAuth.config.add_camelization 'qq', 'QQ' if config.dig("oauth", "qq") OmniAuth.config.add_camelization 'qq', 'QQ' if config.dig("oauth", "qq")
OmniAuth.config.add_camelization 'github', 'GitHub' if config.dig("oauth", "github") # OmniAuth.config.add_camelization 'github', 'GitHub' if config.dig("oauth", "github")
OmniAuth.config.add_camelization 'gitee', 'Gitee' if config.dig("oauth", "gitee") # OmniAuth.config.add_camelization 'gitee', 'Gitee' if config.dig("oauth", "gitee")
OmniAuth.config.add_camelization 'wechat', 'Wechat' if config.dig("oauth", "wechat") # OmniAuth.config.add_camelization 'wechat', 'Wechat' if config.dig("oauth", "wechat")
OmniAuth.config.logger = Rails.logger OmniAuth.config.logger = Rails.logger
OmniAuth.config.before_request_phase = nil OmniAuth.config.before_request_phase = nil
OmniAuth.config.before_callback_phase = nil OmniAuth.config.before_callback_phase = nil
@ -22,6 +22,6 @@ Rails.application.config.middleware.use OmniAuth::Builder do
provider :gitee, config.dig("oauth", "gitee", "appid"), config.dig("oauth", "gitee", "secret"), { provider_ignores_state: true, scope: "user_info emails" } provider :gitee, config.dig("oauth", "gitee", "appid"), config.dig("oauth", "gitee", "secret"), { provider_ignores_state: true, scope: "user_info emails" }
end end
if config.dig("oauth", "wechat").present? if config.dig("oauth", "wechat").present?
provider :gitee, config.dig("oauth", "wechat", "appid"), config.dig("oauth", "wechat", "secret"), { provider_ignores_state: true, scope: "snsapi_login" } provider :wechat, config.dig("oauth", "wechat", "appid"), config.dig("oauth", "wechat", "secret"), { provider_ignores_state: true, scope: "snsapi_login" }
end end
end end

View File

@ -20,7 +20,7 @@ Rails.application.routes.draw do
get 'attachments/download/:id', to: 'attachments#show' get 'attachments/download/:id', to: 'attachments#show'
get 'attachments/download/:id/:filename', to: 'attachments#show' get 'attachments/download/:id/:filename', to: 'attachments#show'
get 'auth/qq/callback', to: 'oauth/qq#create' # get 'auth/qq/callback', to: 'oauth/qq#create'
get 'auth/failure', to: 'oauth/base#auth_failure' get 'auth/failure', to: 'oauth/base#auth_failure'
get 'auth/cas/callback', to: 'oauth/cas#create' get 'auth/cas/callback', to: 'oauth/cas#create'
get 'auth/:provider/callback', to: 'oauth/callbacks#create' get 'auth/:provider/callback', to: 'oauth/callbacks#create'