从Authlogic迁移到Devise [英] Migrating from Authlogic to Devise
问题描述
我以前在我的网站上实现了Authlogic的授权。现在我想转而使用Devise,而我想知道有没有人有这方面的经验。也许有人看到这个主题的博文?
谢谢。
我自己最近从Authlogic转到Devise,也没有找到任何文章。然而,在简单的情况下,一旦你丢弃了所有的user_session和其他authlogic相关的代码,主要工作就是将你的旧用户表转换成预期的设计格式。
我的旧表格如下所示:
列|类型|修饰符
------------------- + ------------------------- - + ------------------------------------------------ ----
id |整数| not null default nextval('users_id_seq':: regclass)
login |字符变化(256)| not null
密码|字符变化(64)| not null
created_at |时间戳与时区| not null
updated_at |时间戳与时区| not null
persistence_token |字符变化(255)| not null
索引:
users_pkeyPRIMARY KEY,btree(id)
index_users_on_persistence_tokenUNIQUE,btree(persistence_token)
users_login_keyUNIQUE,btree(login)
,我确定该表必须至少包含以下的devise信息(启用了许多可选功能):
id |整数| not null default nextval('contribut_id_seq':: regclass)
email |字符变化(255)|不为空默认''::字符变化
encrypted_password |字符变化(128)| not null default'':: character changes
password_salt |字符变化(255)| not null default'':: character changes
confirmation_token |字符变化(255)|
confirm_at |没有时区的时间戳|
confirmation_sent_at |没有时区的时间戳|
reset_password_token |字符变化(255)|
remember_token |字符变化(255)|
remember_created_at |没有时区的时间戳|
sign_in_count |整数|默认值0
current_sign_in_at |没有时区的时间戳|
last_sign_in_at |没有时区的时间戳|
current_sign_in_ip |字符变化(255)|
last_sign_in_ip |字符变化(255)|
failed_attempts |整数|默认值0
unlock_token |字符变化(255)|
locked_at |没有时区的时间戳|
created_at |没有时区的时间戳|
updated_at |没有时区的时间戳|
所以我在迁移类中定义了一个未装载的activerecord类
class ConversionUser< ActiveRecord :: Base
set_table_nameusers
end
然后这里是up迁移代码我最终使用(与PostgreSQL):
add_column:users,:email,:string,,limit => 255
执行UPDATE users SET email = login ||'@ somedomain.net'
executeALTER 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,:confirmation_at,:timestamp
add_column:users,:confirmation_sent_at,:timestamp
executeUPDATE users SET confirmation_at = created_at,confirmation_sent_at = created_at
add_column:用户,: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 =>请注意,在这里,我已经将纯密码列转换为加密的加密列,用于Devise - - 如果您使用Authlogic加密密码,那么您可能只想重命名列(如有必要),并在 config / initializers / devise.rb $ c中选择正确的加密器模块$ c>。
为了参考,我的User模型中的devise子句如下所示:
devise:database_authenticatable,:可注册,可恢复
:可记忆,可追踪,可验证,可确认,可锁定
:可超时,:authentication_keys => [:login]
请注意,覆盖:authentication_keys
这样,使用户登录他们的登录而不是他们的电子邮件地址需要我修改一些设计视图: rails generate devise:views
,然后编辑文件。
希望这有所帮助。祝你好运!
I've previously implemented Authlogic for authorization on my site. Now however I wish to switch over to using Devise instead, and I'm wondering if anyone has any experience with this. Perhaps anyone's seen a blog post on the subject?
Thank you.
解决方案 I myself switched from Authlogic to Devise recently and also didn't find any articles. However, in the simple case, once you've thrown away all of your user_session and other authlogic-related code, the main piece of work is converting your old users table to the format expected by devise.
My old table looked like this:
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)
and I determined that the table would have to contain at least the following info for devise (with many optional features enabled):
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 |
So I defined an unadorned activerecord class in the migration class
class ConversionUser < ActiveRecord::Base
set_table_name "users"
end
and then here's the "up" migration code I ended up using (with 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
Note that here I've converted a plain password column into a bcrypt-encrypted column for Devise -- if you've used encrypted passwords with Authlogic, then you'll probably want to just rename the column (if necessary) and choose the correct encryptor module in config/initializers/devise.rb
.
For reference, the "devise" clause in my User model looks like this:
devise :database_authenticatable, :registerable, :recoverable,
:rememberable, :trackable, :validatable, :confirmable, :lockable,
:timeoutable, :authentication_keys => [ :login ]
Note that overriding :authentication_keys
like this so that users sign in with their login rather than their email address required me to modify some of the devise views: rails generate devise:views
, then edit the files.
Hope this helps a bit. Good luck!
这篇关于从Authlogic迁移到Devise的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!