add: trace users and get trace token
This commit is contained in:
parent
747646cd9d
commit
4bbb843edf
|
@ -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
|
|
@ -173,6 +173,7 @@ class User < Owner
|
||||||
|
|
||||||
has_many :system_notification_histories
|
has_many :system_notification_histories
|
||||||
has_many :system_notifications, through: :system_notification_histories
|
has_many :system_notifications, through: :system_notification_histories
|
||||||
|
has_one :trace_user, dependent: :destroy
|
||||||
|
|
||||||
# Groups and active users
|
# Groups and active users
|
||||||
scope :active, lambda { where(status: STATUS_ACTIVE) }
|
scope :active, lambda { where(status: STATUS_ACTIVE) }
|
||||||
|
@ -776,6 +777,22 @@ class User < Owner
|
||||||
self.nickname.present? && self.mail.present?
|
self.nickname.present? && self.mail.present?
|
||||||
end
|
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
|
protected
|
||||||
def validate_password_length
|
def validate_password_length
|
||||||
# 管理员的初始密码是5位
|
# 管理员的初始密码是5位
|
||||||
|
|
|
@ -0,0 +1,17 @@
|
||||||
|
class CreateTraceUsers < ActiveRecord::Migration[5.2]
|
||||||
|
def change
|
||||||
|
create_table :trace_users do |t|
|
||||||
|
t.references :user
|
||||||
|
t.string :username
|
||||||
|
t.string :password
|
||||||
|
t.string :unit
|
||||||
|
t.string :telnumber
|
||||||
|
t.string :email
|
||||||
|
t.string :name
|
||||||
|
t.text :token
|
||||||
|
t.datetime :expired_at
|
||||||
|
|
||||||
|
t.timestamps
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -0,0 +1,5 @@
|
||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
RSpec.describe TraceUser, type: :model do
|
||||||
|
pending "add some examples to (or delete) #{__FILE__}"
|
||||||
|
end
|
Loading…
Reference in New Issue