fixed 增加自动登录

This commit is contained in:
xiaoxiaoqiong 2022-05-24 10:25:58 +08:00
parent 0ae3544235
commit 2959414d56
2 changed files with 47 additions and 44 deletions

View File

@ -8,7 +8,7 @@ class UsersController < ApplicationController
before_action :connect_to_ci_db, only: [:get_user_info] before_action :connect_to_ci_db, only: [:get_user_info]
before_action :convert_image!, only: [:update, :update_image] before_action :convert_image!, only: [:update, :update_image]
skip_before_action :check_sign, only: [:attachment_show] skip_before_action :check_sign, only: [:attachment_show]
# before_action :sso_login, only: [:get_user_info] before_action :sso_login, only: [:get_user_info]
def connect_to_ci_db(options={}) def connect_to_ci_db(options={})
if !(current_user && !current_user.is_a?(AnonymousUser) && current_user.devops_certification?) if !(current_user && !current_user.is_a?(AnonymousUser) && current_user.devops_certification?)
@ -331,26 +331,29 @@ class UsersController < ApplicationController
end end
def sso_login def sso_login
if params[:ticket].present? && !current_user.logged? && params[:websiteName].nil? if params[:login].present? && !current_user.logged? && params[:websiteName].nil?
info = Base64.decode64(params[:ticket]) || Base64.decode64(params[:info].gsub(" ", "+")).force_encoding("utf-8") params = { "login" => "#{params[:login]}", "private_token" => "hriEn3UwXfJs3PmyXnSH" }
# login 邮箱 手机号 姓名 学校/单位 api_url= "https://pre-data.educoder.net"
user_info = info.split("&&") client = Faraday.new(url: api_url)
# Rails.logger.info("user_info====== #{info}") response = client.public_send("get", "/api/sources/get_user_info_by_login", params)
login = user_info[0] result = JSON.parse(response.body)
email = user_info[1]
phone = user_info[2]
real_name = user_info[3]
department_name = user_info[4]
# 没有登录时,新建用户并登录 if result["status"].to_s == "0"
if current_user.logged? # login 邮箱 手机号 姓名 学校/单位
user = current_user user_info = result["data"]
else Rails.logger.info("user_info====== #{user_info}")
user = User.where("login = ? or phone = ? or mail = ? ", "edu_#{login}", phone, email).first login = user_info["login"]
email = user_info["mail"]
phone = user_info["phone"]
real_name = user_info["username"]
department_name = user_info["school"]
# 没有用户时,新建用户并登录
user = User.where("login = ? or phone = ? or mail = ? ", "#{login}", phone, email).first
unless user unless user
ActiveRecord::Base.transaction do ActiveRecord::Base.transaction do
phone_rand = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9].sample(4).join phone_rand = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9].sample(4).join
user_params = { status: 1, type: 'User', login: "e_#{login}", lastname: "#{real_name}", mail: "#{email}", user_params = { status: 1, type: 'User', login: "#{login}", lastname: "#{real_name}", mail: "#{email}",
nickname: "#{real_name}", professional_certification: 0, certification: 0, grade: 0, nickname: "#{real_name}", professional_certification: 0, certification: 0, grade: 0,
password: "12345678", phone: "#{phone_rand}", profile_completed: 1 } password: "12345678", phone: "#{phone_rand}", profile_completed: 1 }
user = User.create!(user_params) user = User.create!(user_params)

View File

@ -1,28 +1,28 @@
json.username @user.full_name json.username @user.full_name
json.real_name @user.real_name json.real_name @user.real_name
json.nickname @user.nickname json.nickname @user.nickname
json.gender @user.gender json.gender @user.gender
json.login @user.login json.login @user.login
json.user_id @user.id json.user_id @user.id
json.image_url url_to_avatar(@user) json.image_url url_to_avatar(@user)
json.admin @user.admin? json.admin @user.admin?
json.is_teacher @user.user_extension&.teacher? json.is_teacher @user.user_extension&.teacher?
json.user_identity @user.identity json.user_identity @user.identity
json.tidding_count 0 json.tidding_count 0
json.user_phone_binded @user.phone.present? json.user_phone_binded @user.phone.present?
json.need_edit_info @user.need_edit_info? json.need_edit_info @user.need_edit_info?
# json.phone @user.phone json.phone @user.phone
# json.email @user.mail # json.email @user.mail
json.profile_completed @user.profile_is_completed? json.profile_completed @user.profile_is_completed?
json.professional_certification @user.professional_certification json.professional_certification @user.professional_certification
json.devops_step @user.devops_step json.devops_step @user.devops_step
json.ci_certification @user.ci_certification? json.ci_certification @user.ci_certification?
json.email @user.mail json.email @user.mail
json.province @user.province json.province @user.province
json.city @user.city json.city @user.city
json.custom_department @user.custom_department json.custom_department @user.custom_department
json.description @user.description json.description @user.description
json.super_description @user.super_description json.super_description @user.super_description
json.(@user, :show_email, :show_department, :show_location, :show_super_description) json.(@user, :show_email, :show_department, :show_location, :show_super_description)
json.message_unread_total @message_unread_total json.message_unread_total @message_unread_total
json.has_trace_user @user.trace_user.present? json.has_trace_user @user.trace_user.present?