2015-09-04 47 views
4

用戶登錄後(ENV['warden'].authenticate!'d)一次,warden如何知道?除了rack.session價值之外,我無法在cookie中找到任何內容 - 我無法從文檔或代碼中找出這些。監督員如何從機架會話中獲取當前用戶?

+0

你能詳細說明一下這個問題嗎? 'env ['rack.session']'包含這個特定用戶的會話,並且包含一個'warden.user.key'或者其他東西... – mhutter

+0

@mhutter但是服務器上存儲的是哪裏?這似乎是在這裏統一(https://github.com/rack/rack/blob/master/lib/rack/session/cookie.rb),但我無法找到它的存儲位置。似乎有兩個選項:Memcached和Pooled,但找不到哪一個是默認的,哪個是在什麼時候使用的。我知道它在服務器關閉後仍然存在,所以memcached? – Cenoc

+0

你確定它會一直重新啓動嗎?默認是'Pool',它將數據存儲在'Hash'中:https://github.com/rack/rack/blob/master/lib/rack/session/pool.rb#L33 – mhutter

回答

0

在色器件/ LIB /色器件/控制器/ helpers.rb:35

def devise_group(group_name, opts={}) 
     mappings = "[#{ opts[:contains].map { |m| ":#{m}" }.join(',') }]" 

     class_eval <<-METHODS, __FILE__, __LINE__ + 1 
     def authenticate_#{group_name}!(favourite=nil, opts={}) 
      unless #{group_name}_signed_in? 
      mappings = #{mappings} 
      mappings.unshift mappings.delete(favourite.to_sym) if favourite 
      mappings.each do |mapping| 
       opts[:scope] = mapping 
       warden.authenticate!(opts) if !devise_controller? || opts.delete(:force) 
      end 
      end 
     end 
     def #{group_name}_signed_in? 
      #{mappings}.any? do |mapping| 
      warden.authenticate?(scope: mapping) 
      end 
     end 
     def current_#{group_name}(favourite=nil) 
      mappings = #{mappings} 
      mappings.unshift mappings.delete(favourite.to_sym) if favourite 
      mappings.each do |mapping| 
      current = warden.authenticate(scope: mapping) 
      return current if current 
      end 
      nil 
     end 
     def current_#{group_name.to_s.pluralize} 
      #{mappings}.map do |mapping| 
      warden.authenticate(scope: mapping) 
      end.compact 
     end 
     helper_method "current_#{group_name}", "current_#{group_name.to_s.pluralize}", "#{group_name}_signed_in?" 
     METHODS 
    end 

https://github.com/plataformatec/devise/blob/master/lib/devise/controllers/helpers.rb

闡釋: 在服務器開始時devise_group方法運行動態地創建輔助方法(CURRENT_USER例如),這是用於實際保存用戶的執事012xx的包裝,其內容是warden.authenticate方法內容是

def warden 
    request.env['warden'] 
    end 

也是在同一個文件中聲明

+1

如果你添加了一些關於你想用這段代碼片段傳達的信息將會很有幫助 –