2013-04-03 107 views
4

我試圖在由Devise郵件發送的確認頁面中顯示用戶密碼。確認頁面是默認設計確認頁面顯示用戶密碼頁

Welcome [email protected]! 

You can confirm your account email through the link below: 

Confirm my account 

不過,我希望有

Welcome [email protected]! 

Your password is currently DASADSADS 

You can confirm your account email through the link below: 

Confirm my account 

如何訪問用戶對象的看法?我是否需要用自定義函數覆蓋郵件控制器?如果是這樣,我該如何告訴當前郵件程序的做法(嘗試查看文檔,但找不到任何線索)?

我注意到@email和@resource在視圖中使用。我可以使用其中的任何一種訪問當前密碼的非哈希形式嗎?

請注意,我用user.find(1).send_confirmation_instructions

+1

http://i.imgur.com/3w3XvGU.jpg – 2013-04-03 19:04:32

回答

6

雖然可以這樣做,但我會謹慎的非常強烈反對這樣做。哈希密碼專門用於密碼不能輕易重新創建。將原始密碼傳遞給用戶將導致它以純文本的形式發回,這種做法會導致整個目的的失敗。另外,用戶不應該知道他們的密碼(他們確實已經輸入了兩次)?!?

要做到這一點,您需要在註冊create操作中捕獲原始(未經哈希)密碼並在該點發送電子郵件(傳遞密碼)。您可以通過重寫sign_up方法做到這一點 - 你可以在初始化做到這一點:

class Devise::RegistrationsController < DeviseController 
    def sign_up(resource_name, resource) 
    sign_in(resource_name, resource) 
    resource.unhashed_password = resource_params[:password] 
    resource.send_confirmation_instructions 
    end 
end 

或者,你可以從Devise::RegistrationsController得到一個新的控制器,並把這個重寫代碼中有(推薦的方法 - 話又說回來,但是,這整個操作不是真的推薦)。你需要爲這個添加unhashed_password訪問工作:

class User < ActiveRecord::Base 
    attr_accessor :unhashed_password 
end 

然後你就可以更新您的確認視圖(在app/views/devise/mailer/confirmation_instructions.html.erb)包含本:

<p>Your password is currently <%= @resource.unhashed_password %></p> 
+0

謝謝你的迴應是我一直在尋找的。但是,我的情況稍有不同(我應該詳細闡述)。用戶已經在調用user.find(1).send_confirmation_instructions的同一控制器中使用User.new(...)創建。所以,我在那個視圖中使用了非哈希密碼,無論如何,我可以將它從自定義控制器傳遞給資源對象? – user1431282 2013-04-12 19:07:55

+1

@ user1431282:在離開「創建」操作後,瀏覽器會發送一個重定向到另一個路徑。一旦發生這種情況,就不可能重新獲取未經加密的密碼,因爲它不存儲在任何地方,也不能被反向設計。因此,工作流程中唯一可以完成的地方在於實際設置密碼(並且可以訪問非哈希版本)的相同動作,該動作是'create'動作的一部分。 – PinnyM 2013-04-14 02:52:48

1

手動發送這封電子郵件設計保存在加密的形式密碼:您可以通過解密,

生成新的遷移:

$ rails g migration AddLegacyPasswordToUser legacy_password:boolean 
     invoke active_record 
     create db/migrate/20120508083355_add_legacy_password_to_users.rb 
$ rake db:migrate 

使用legacy_password方法以下代碼您可以解密您的密碼:

class User < ActiveRecord::Base 

... 

    def valid_password?(password) 
    if self.legacy_password? 
     # Use Devise's secure_compare to avoid timing attacks 
     if Devise.secure_compare(self.encrypted_password, User.legacy_password(password)) 

     self.password = password 
     self.password_confirmation = password 
     self.legacy_password = false 
     self.save! 

     else 
     return false 
     end 
    end 

    super(password) 
    end 

    # Put your legacy password hashing method here 
    def self.legacy_password(password) 
    return Digest::MD5.hexdigest("#{password}-salty-herring"); 
    end 
end 
0

你可以只使用要求。 request_parameters [:user] [:password]在創建或更新操作中獲取純文本密碼。

+0

這是一條評論,不是答案 – 2013-10-18 06:55:16