2010-10-01 47 views
38

我以前在我的網站上實施了Authlogic進行授權。但是現在我想轉而使用Devise,並且我想知道是否有人有這方面的經驗。也許任何人都看過關於這個主題的博客文章?從Authlogic移植到設計

謝謝。

+0

完整的文檔從authlogic遷移到設計。 https://sunilsharma3639.wordpress.com/2014/06/11/rails-authentication-authlogic-to-devise/ – sunil 2015-08-05 16:04:38

回答

48

我自己最近從Authlogic轉到Devise,也沒有找到任何文章。然而,在簡單的情況下,一旦你拋棄了所有的user_session和其他authlogic相關的代碼,主要工作就是將你的舊用戶錶轉換爲devise所期望的格式。

我的舊錶是這樣的:

 Column  |   Type   |      Modifiers      
-------------------+--------------------------+---------------------------------------------------- 
id    | integer     | not null default nextval('users_id_seq'::regclass) 
login    | character varying(256) | not null 
password   | character varying(64) | not null 
created_at  | timestamp with time zone | not null 
updated_at  | timestamp with time zone | not null 
persistence_token | character varying(255) | not null 
Indexes: 
    "users_pkey" PRIMARY KEY, btree (id) 
    "index_users_on_persistence_token" UNIQUE, btree (persistence_token) 
    "users_login_key" UNIQUE, btree (login) 

,我確定,該表必須包含至少設計出以下信息(與許多可選的功能啓用):

id     | integer      | not null default nextval('contributors_id_seq'::regclass) 
email    | character varying(255)  | not null default ''::character varying 
encrypted_password | character varying(128)  | not null default ''::character varying 
password_salt  | character varying(255)  | not null default ''::character varying 
confirmation_token | character varying(255)  | 
confirmed_at   | timestamp without time zone | 
confirmation_sent_at | timestamp without time zone | 
reset_password_token | character varying(255)  | 
remember_token  | character varying(255)  | 
remember_created_at | timestamp without time zone | 
sign_in_count  | integer      | default 0 
current_sign_in_at | timestamp without time zone | 
last_sign_in_at  | timestamp without time zone | 
current_sign_in_ip | character varying(255)  | 
last_sign_in_ip  | character varying(255)  | 
failed_attempts  | integer      | default 0 
unlock_token   | character varying(255)  | 
locked_at   | timestamp without time zone | 
created_at   | timestamp without time zone | 
updated_at   | timestamp without time zone | 

所以我在移植類中定義了一個未經修改的activerecord類

class ConversionUser < ActiveRecord::Base 
    set_table_name "users" 
end 

和ñ這裏的「上」遷移代碼我結束了使用(在PostgreSQL):

add_column :users, :email, :string, :limit => 255 
execute "UPDATE users SET email = login || '@somedomain.net'" 
execute "ALTER TABLE users ALTER email SET NOT NULL" 

add_column :users, :encrypted_password, :string, :limit => 128 
add_column :users, :password_salt, :string, :limit => 255 

require 'devise/encryptors/bcrypt' 
ConversionUser.find(:all).each do |u| 
    password_salt = Devise::Encryptors::Bcrypt.salt(Devise.stretches) 
    u.update_attributes!(:password_salt => password_salt, 
         :encrypted_password => Devise::Encryptors::Bcrypt.digest(u.password, Devise.stretches, password_salt, Devise.pepper)) 
end 

add_column :users, :confirmation_token, :string, :limit => 255 
add_column :users, :confirmed_at, :timestamp 
add_column :users, :confirmation_sent_at, :timestamp 
execute "UPDATE users SET confirmed_at = created_at, confirmation_sent_at = created_at" 
add_column :users, :reset_password_token, :string, :limit => 255 

add_column :users, :remember_token, :string, :limit => 255 
add_column :users, :remember_created_at, :timestamp 
add_column :users, :sign_in_count, :integer, :default => 0 
add_column :users, :current_sign_in_at, :timestamp 
add_column :users, :last_sign_in_at, :timestamp 
add_column :users, :current_sign_in_ip, :string, :limit => 255 
add_column :users, :last_sign_in_ip, :string, :limit => 255 

add_column :users, :failed_attempts, :integer, :default => 0 
add_column :users, :unlock_token, :string, :limit => 255 
add_column :users, :locked_at, :timestamp 

remove_column :users, :password 
remove_column :users, :persistence_token 

add_index :users, :email,    :unique => true 
add_index :users, :confirmation_token, :unique => true 
add_index :users, :reset_password_token, :unique => true 
add_index :users, :unlock_token,   :unique => true 

請注意,在這裏我已經轉換明文口令列到bcrypt加密列設計 - 如果你已經使用使用Authlogic加密密碼,那麼您可能只想重命名該列(如有必要)並在config/initializers/devise.rb中選擇正確的加密器模塊。

供參考,在我的用戶模型中的「制定」的條款是這樣的:

devise :database_authenticatable, :registerable, :recoverable, 
    :rememberable, :trackable, :validatable, :confirmable, :lockable, 
    :timeoutable, :authentication_keys => [ :login ] 

注意重載:authentication_keys這樣使用戶在使用其登錄,而不是他們的電子郵件地址登錄要求我修改一些設計意見:rails generate devise:views,然後編輯文件。

希望這會有所幫助。祝你好運!

+0

優秀的答案!謝謝! – Erik 2010-10-07 07:31:15

+0

正是我在找的東西。謝謝! – jspooner 2010-10-22 03:26:55

21

sanityinc的回答非常棒!非常感謝!

但我有一個不同的版本authlogic的帶有加密密碼,我的最終向上遷移是:

rename_column :users, :crypted_password, :encrypted_password 

add_column :users, :confirmation_token, :string, :limit => 255 
add_column :users, :confirmed_at, :timestamp 
add_column :users, :confirmation_sent_at, :timestamp 
execute "UPDATE users SET confirmed_at = created_at, confirmation_sent_at = created_at" 
add_column :users, :reset_password_token, :string, :limit => 255 

add_column :users, :remember_token, :string, :limit => 255 
add_column :users, :remember_created_at, :timestamp 
rename_column :users, :login_count, :sign_in_count 
rename_column :users, :current_login_at, :current_sign_in_at 
rename_column :users, :last_login_at, :last_sign_in_at 
rename_column :users, :current_login_ip, :current_sign_in_ip 
rename_column :users, :last_login_ip, :last_sign_in_ip 

rename_column :users, :failed_login_count, :failed_attempts 
add_column :users, :unlock_token, :string, :limit => 255 
add_column :users, :locked_at, :timestamp 

remove_column :users, :persistence_token 
remove_column :users, :perishable_token 
remove_column :users, :single_access_token 

add_index :users, :email,    :unique => true 
add_index :users, :confirmation_token, :unique => true 
add_index :users, :reset_password_token, :unique => true 
add_index :users, :unlock_token,   :unique => true 

..它爲MySQL偉大的工作。

+0

有沒有辦法讓用戶在遷移後登錄? – Gady 2015-04-14 14:27:14

6

如果用戶登錄時遇到'無效哈希'錯誤,請在用戶模型中添加:encryptable。 我已經從authlogic遷移到設計。我的數據庫是MySQL。

+0

超級讚賞。我已經通過了上述兩個遷移,這是一個很好的解決方案,節省了一些時間。 – Glenn 2011-12-21 22:15:59

+0

這是我的解決方案,在文檔中沒有提到它。 – pingu 2013-09-22 09:11:52

5

如果您收到加密錯誤,請嘗試在devise.rb配置改變這些:

config.stretches = 20 
config.encryptor = :authlogic_sha512