从Authlogic移植到设计
我以前在我的网站上实施了Authlogic进行授权。但是现在我想转而使用Devise,并且我想知道是否有人有这方面的经验。也许任何人都看过关于这个主题的博客文章?从Authlogic移植到设计
谢谢。
我自己最近从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
,然后编辑文件。
希望这会有所帮助。祝你好运!
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伟大的工作。
有没有办法让用户在迁移后登录? – Gady 2015-04-14 14:27:14
如果您收到加密错误,请尝试在devise.rb
配置改变这些:
config.stretches = 20
config.encryptor = :authlogic_sha512
完整的文档从authlogic迁移到设计。 https://sunilsharma3639.wordpress.com/2014/06/11/rails-authentication-authlogic-to-devise/ – sunil 2015-08-05 16:04:38