新增:同步acge用户并同时创建平台账号

This commit is contained in:
yystopf 2024-03-21 14:58:35 +08:00
parent bf7289032a
commit 6e2816af75
3 changed files with 38 additions and 4 deletions

View File

@ -3,12 +3,16 @@ class Oauth::AcgeController < Oauth::BaseController
def create
begin
code = params['code'].to_s.strip
tip_exception("code不能为空") if code.blank?
uid = params['uid'].to_s.strip
tip_exception("uid不能为空") if uid.blank?
redirect_uri = params['redirect_uri'].to_s.strip
tip_exception("redirect_uri不能为空") if redirect_uri.blank?
email = params['email'].to_s.strip
tip_exception("email不能为空") if email.blank?
phone = params['phone'].to_s.strip
tip_exception("phone不能为空") if phone.blank?
name = params['name'].to_s.strip
tip_exception("name不能为空") if name.blank?
open_user = OpenUsers::Acge.find_by(uid: uid)
if open_user.present? && open_user.user.present?
@ -18,13 +22,41 @@ class Oauth::AcgeController < Oauth::BaseController
else
if current_user.blank? || !current_user.logged?
session[:unionid] = uid
user = User.find_by(mail: email) || User.find_by(phone: phone)
if user.present?
OpenUsers::Acge.create!(user: user, uid: uid)
successful_authentication(user)
redirect_to redirect_uri
return
else
username = uid[0..7]
password = SecureRandom.hex(4)
reg_result = autologin_register(username, email, password, 'acge', phone, name)
CSV.open("public/操作系统大赛用户信息.csv", 'wb') do |csv|
csv << [username, email, password, phone, name]
end
if reg_result[:message].blank?
open_user = OpenUsers::Acge.create!(user_id: reg_result[:user][:id], uid: uid)
successful_authentication(open_user.user)
redirect_to redirect_uri
return
else
render_error(reg_result[:message])
end
end
else
OpenUsers::Acge.create!(user: current_user, uid: uid)
successful_authentication(current_user)
redirect_to redirect_uri
return
end
end
Rails.logger.info("[OAuth2] session[:unionid] -> #{session[:unionid]}")
redirect_to "/bindlogin/acge?redirect_uri=#{redirect_uri}"
# redirect_to "/bindlogin/acge?redirect_uri=#{redirect_uri}"
rescue Exception => ex
render_error(ex.message)
end

View File

@ -115,7 +115,7 @@ class User < Owner
# trustie: 来自Trustie平台
# forge: 平台本身注册的用户
# military: 军科的用户
enumerize :platform, in: [:forge, :educoder, :trustie, :military, :github, :gitee, :qq, :wechat, :bot], default: :forge, scope: :shallow
enumerize :platform, in: [:forge, :educoder, :trustie, :military, :github, :gitee, :qq, :wechat, :bot, :acge], default: :forge, scope: :shallow
belongs_to :laboratory, optional: true
has_one :user_extension, dependent: :destroy

View File

@ -0,0 +1,2 @@
用户名,邮箱,密码,手机号,昵称
123456789,yystopf1@163.com,9b653a7d,15386415122,何慧
1 用户名 邮箱 密码 手机号 昵称
2 123456789 yystopf1@163.com 9b653a7d 15386415122 何慧