fix: merge from develop

This commit is contained in:
2022-03-31 13:00:36 +08:00
13 changed files with 451 additions and 0 deletions
+58
View File
@@ -0,0 +1,58 @@
# == Schema Information
#
# Table name: trace_users
#
# id :integer not null, primary key
# user_id :integer
# username :string(255)
# password :string(255)
# unit :string(255)
# telnumber :string(255)
# email :string(255)
# name :string(255)
# token :text(65535)
# expired_at :datetime
# created_at :datetime not null
# updated_at :datetime not null
#
# Indexes
#
# index_trace_users_on_user_id (user_id)
#
# 代码溯源 用户
class TraceUser < ApplicationRecord
belongs_to :user
def build_self_data
return if user.nil?
self.username = user.login
self.password = SecureRandom.hex
self.unit = user.custom_department.blank? ? 'GitLink' : user.custom_department
self.telnumber = user.phone.blank? ? '13800000000' : user.phone
self.email = user.mail
self.name = user.nickname.blank? ? user.login : user.nickname
self
end
def build_token
return if username.blank? || password.blank? || unit.blank? || telnumber.blank? || email.blank? || name.blank?
response = Trace::AddUserService.call(username, password, unit, telnumber, email, name)
self.token = response[1]['token']
self.expired_at = Time.now + 1.hours
end
def refresh_token
return if username.blank? || password.blank? || unit.blank? || telnumber.blank? || email.blank? || name.blank?
response = Trace::LoginService.call(username, password)
self.token = response[1]['token']
self.expired_at = Time.now + 1.hours
end
end
+17
View File
@@ -174,6 +174,7 @@ class User < Owner
has_many :system_notification_histories
has_many :system_notifications, through: :system_notification_histories
has_one :trace_user, dependent: :destroy
# Groups and active users
scope :active, lambda { where(status: [STATUS_ACTIVE, STATUS_EDIT_INFO]) }
@@ -789,6 +790,22 @@ class User < Owner
self.nickname.present? && self.mail.present?
end
def trace_token
if trace_user.present?
if trace_user.expired_at < Time.now
trace_user.refresh_token
trace_user.save
end
else
trace_user = TraceUser.new
trace_user.build_self_data
trace_user.build_token
trace_user.save
end
trace_user.token
end
protected
def validate_password_length
# 管理员的初始密码是5位