mirror of
https://github.com/discourse/discourse.git
synced 2024-11-22 15:16:08 +08:00
refactor and organise current_user better
This commit is contained in:
parent
ab97dc8fd6
commit
b66db4153d
|
@ -111,15 +111,6 @@ class ApplicationController < ActionController::Base
|
||||||
@guardian ||= Guardian.new(current_user)
|
@guardian ||= Guardian.new(current_user)
|
||||||
end
|
end
|
||||||
|
|
||||||
def log_on_user(user)
|
|
||||||
session[:current_user_id] = user.id
|
|
||||||
unless user.auth_token
|
|
||||||
user.auth_token = SecureRandom.hex(16)
|
|
||||||
user.save!
|
|
||||||
end
|
|
||||||
cookies.permanent[:_t] = { :value => user.auth_token, :httponly => true }
|
|
||||||
end
|
|
||||||
|
|
||||||
# This is odd, but it seems that in Rails `render json: obj` is about
|
# This is odd, but it seems that in Rails `render json: obj` is about
|
||||||
# 20% slower than calling MultiJSON.dump ourselves. I'm not sure why
|
# 20% slower than calling MultiJSON.dump ourselves. I'm not sure why
|
||||||
# Rails doesn't call MultiJson.dump when you pass it json: obj but
|
# Rails doesn't call MultiJson.dump when you pass it json: obj but
|
||||||
|
|
|
@ -291,6 +291,13 @@ class User < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def update_ip_address!(new_ip_address)
|
||||||
|
if (ip_address != new_ip_address) and new_ip_address.present?
|
||||||
|
@current_user.ip_address = new_ip_address
|
||||||
|
@current_user.update_column(:ip_address, new_ip_address)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def update_last_seen!
|
def update_last_seen!
|
||||||
now = DateTime.now
|
now = DateTime.now
|
||||||
now_date = now.to_date
|
now_date = now.to_date
|
||||||
|
|
|
@ -2,13 +2,22 @@ module CurrentUser
|
||||||
|
|
||||||
def self.lookup_from_env(env)
|
def self.lookup_from_env(env)
|
||||||
request = Rack::Request.new(env)
|
request = Rack::Request.new(env)
|
||||||
auth_token = request.cookies[:_t]
|
lookup_from_auth_token(request.cookies["_t"])
|
||||||
user = nil
|
end
|
||||||
|
|
||||||
|
def self.lookup_from_auth_token(auth_token)
|
||||||
if auth_token && auth_token.length == 32
|
if auth_token && auth_token.length == 32
|
||||||
user = User.where(auth_token: auth_token).first
|
User.where(auth_token: auth_token).first
|
||||||
end
|
end
|
||||||
|
end
|
||||||
return user
|
|
||||||
|
def log_on_user(user)
|
||||||
|
session[:current_user_id] = user.id
|
||||||
|
unless user.auth_token
|
||||||
|
user.auth_token = SecureRandom.hex(16)
|
||||||
|
user.save!
|
||||||
|
end
|
||||||
|
cookies.permanent[:_t] = { :value => user.auth_token, :httponly => true }
|
||||||
end
|
end
|
||||||
|
|
||||||
def current_user
|
def current_user
|
||||||
|
@ -16,11 +25,8 @@ module CurrentUser
|
||||||
|
|
||||||
if session[:current_user_id].blank?
|
if session[:current_user_id].blank?
|
||||||
# maybe we have a cookie?
|
# maybe we have a cookie?
|
||||||
auth_token = cookies[:_t]
|
@current_user = CurrentUser.lookup_from_auth_token(cookies["_t"])
|
||||||
if auth_token && auth_token.length == 32
|
session[:current_user_id] = @current_user.id if @current_user
|
||||||
@current_user = User.where(auth_token: auth_token).first
|
|
||||||
session[:current_user_id] = @current_user.id if @current_user
|
|
||||||
end
|
|
||||||
else
|
else
|
||||||
@current_user ||= User.where(id: session[:current_user_id]).first
|
@current_user ||= User.where(id: session[:current_user_id]).first
|
||||||
end
|
end
|
||||||
|
@ -32,10 +38,7 @@ module CurrentUser
|
||||||
@not_logged_in = session[:current_user_id].blank?
|
@not_logged_in = session[:current_user_id].blank?
|
||||||
if @current_user
|
if @current_user
|
||||||
@current_user.update_last_seen!
|
@current_user.update_last_seen!
|
||||||
if (@current_user.ip_address != request.remote_ip) and request.remote_ip.present?
|
@current_user.update_ip_address!(request.remote_ip)
|
||||||
@current_user.ip_address = request.remote_ip
|
|
||||||
@current_user.update_column(:ip_address, request.remote_ip)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
@current_user
|
@current_user
|
||||||
end
|
end
|
||||||
|
|
15
spec/components/current_user_spec.rb
Normal file
15
spec/components/current_user_spec.rb
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
require 'spec_helper'
|
||||||
|
require_dependency 'current_user'
|
||||||
|
|
||||||
|
describe CurrentUser do
|
||||||
|
it "allows us to lookup a user from our environment" do
|
||||||
|
token = EmailToken.generate_token
|
||||||
|
user = Fabricate.build(:user)
|
||||||
|
User.expects(:where).returns([user])
|
||||||
|
CurrentUser.lookup_from_env("HTTP_COOKIE" => "_t=#{token};").should == user
|
||||||
|
end
|
||||||
|
|
||||||
|
it "allows us to lookup a user from our app" do
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
Loading…
Reference in New Issue
Block a user