2013-02-05 14:16:51 -05:00
|
|
|
module CurrentUser
|
|
|
|
|
2013-02-15 03:23:40 -05:00
|
|
|
def self.lookup_from_env(env)
|
|
|
|
request = Rack::Request.new(env)
|
2013-02-20 17:24:19 -05:00
|
|
|
auth_token = request.cookies[:_t]
|
2013-02-15 03:23:40 -05:00
|
|
|
user = nil
|
|
|
|
if auth_token && auth_token.length == 32
|
|
|
|
user = User.where(auth_token: auth_token).first
|
|
|
|
end
|
|
|
|
|
|
|
|
return user
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
def current_user
|
|
|
|
return @current_user if @current_user || @not_logged_in
|
|
|
|
|
|
|
|
if session[:current_user_id].blank?
|
|
|
|
# maybe we have a cookie?
|
2013-02-20 17:24:19 -05:00
|
|
|
auth_token = cookies.signed[:_t]
|
2013-02-05 14:16:51 -05:00
|
|
|
if auth_token && auth_token.length == 32
|
|
|
|
@current_user = User.where(auth_token: auth_token).first
|
|
|
|
session[:current_user_id] = @current_user.id if @current_user
|
|
|
|
end
|
|
|
|
else
|
|
|
|
@current_user ||= User.where(id: session[:current_user_id]).first
|
|
|
|
end
|
|
|
|
|
|
|
|
if @current_user && @current_user.is_banned?
|
|
|
|
@current_user = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
@not_logged_in = session[:current_user_id].blank?
|
|
|
|
if @current_user
|
|
|
|
@current_user.update_last_seen!
|
2013-02-11 16:01:04 -05:00
|
|
|
if (@current_user.ip_address != request.remote_ip) and request.remote_ip.present?
|
2013-02-05 14:16:51 -05:00
|
|
|
@current_user.ip_address = request.remote_ip
|
2013-02-11 15:47:28 -05:00
|
|
|
@current_user.update_column(:ip_address, request.remote_ip)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
@current_user
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|