You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
4.2 KiB

class Token < ActiveRecord::Base
include ApplicationHelper
belongs_to :user
validates_uniqueness_of :value
before_create :delete_previous_tokens, :generate_new_token
@@validity_time = 1.day
STATUS_LOGIN = 0
STATUS_LOGOUT = 1
STATUS_EX_LOGIN = 2
STATUS_EX_LOGOUT = 3
STATUS_SELF_LOGOUT = 4
STATUS_EX_OUT_LOGOUT = 5
# status 0=正常登陆1=正常退出登陆失效2=考试期间输入考试密码登录3=考试期间被挤出登录失效4=同一个浏览器登录导致失效5考试期间进入考试导致其他登录用户失效
def generate_new_token
self.value = Token.generate_token_value if action != User::SESSION_ACTION
end
def self.get_or_create_permanent_login_token(user, type)
token = Token.get_token_from_user(user, type)
unless token
token = Token.create(:user => user, :action => type)
else
token.update_attribute(:created_on, Time.now)
end
token
end
# 更新session的token的状态值
def self.update_token_by_session(user_id,session_id = nil,status = self::STATUS_SELF_LOGOUT)
if session_id.present?
token = Token.find_by(:action => User::SESSION_ACTION, :user_id => user_id, :value => session_id)
if token.present?
token.status = status
token.save!
end
else # 同一把所有的token记录状态修改掉并清空对应session_id的缓存
self.delete_user_session(user_id,nil,status)
end
end
# 删除用户的session记录
def self.delete_user_session(user_id,now_session_id = nil,status = self::STATUS_SELF_LOGOUT)
session_store = Rails.application.config.session_store.new(:cache_store, Rails.application.config.session_options)
tokens = Token.where(:action => User::SESSION_ACTION, :user_id => user_id,status:[self::STATUS_LOGIN,self::STATUS_EX_LOGIN])
tokens = tokens.where.not(value:now_session_id) if now_session_id.present?
tokens.each do |token|
token.status = status
if token.save! # 开始清空session缓存
rr = session_store.delete_session(ENV, token.value,Rails.application.config.session_options)
Rails.logger.info("调试删除session===#{token.value}===#{rr}")
end
end
end
def self.get_token_from_user(user, action)
token = Token.where(:action => action, :user_id => user).first
unless token
token = Token.create!(user_id: user.id, action: action)
end
token
end
# Return true if token has expired
def expired?
return Time.now > self.created_on + @@validity_time
end
# Delete all expired tokens
def self.destroy_expired
Token.delete_all ["action NOT IN (?) AND created_on < ?", ['feeds', 'api', 'autologin'], Time.now - @@validity_time]
end
# Returns the active user who owns the key for the given action
def self.find_active_user(action, key, validity_days=nil)
user = find_user(action, key, validity_days)
if user && user.active?
user
end
end
# Returns the user who owns the key for the given action
def self.find_user(action, key, validity_days=nil)
token = find_token(action, key, validity_days)
if token
token.user
end
end
# Returns the token for action and key with an optional
# validity duration (in number of days)
def self.find_token(action, key, validity_days=nil)
action = action.to_s
key = key.to_s
return nil unless action.present? && key =~ /\A[a-z0-9]+\z/i
# if action == User::SESSION_ACTION # 如果是session的则需要判断token是否有效
# token = Token.where(value: key, action: action).first
# else
# token = Token.where(value: key, action: action).first
# end
token = Token.where(value: key, action: action).first
if token && (token.action == action) && (token.value == key) && token.user
if validity_days.nil? || (token.created_on > validity_days.days.ago)
token
end
end
end
def self.generate_token_value
Edu::Utils.random_hex(20)
end
def self.delete_user_all_tokens(user)
Token.delete_all(user_id: user.id)
end
private
# Removes obsolete tokens (same user and action)
def delete_previous_tokens
if user && action!='_educoder_session'
Token.where(['user_id = ? AND action = ?', user.id, action]).delete_all
end
end
end