gpt4 book ai didi

ruby-on-rails - 从 Authlogic 迁移到 Devise

转载 作者:行者123 更新时间:2023-12-02 06:01:45 25 4
gpt4 key购买 nike

我之前已经在我的网站上实现了 Authlogic 进行授权。然而现在我希望改用 Devise,我想知道是否有人有这方面的经验。也许有人看过有关该主题的博客文章?

谢谢。

最佳答案

我自己最近从 Authlogic 切换到 Devise,也没有找到任何文章。但是,在简单的情况下,一旦您丢弃了所有 user_session 和其他与 authlogic 相关的代码,主要工作就是将旧用户表转换为设计所需的格式。

我的旧 table 看起来像这样:

      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

请注意,这里我已将纯密码列转换为 Devise 的 bcrypt 加密列 - 如果您在 Authlogic 中使用了加密密码,那么您可能只想重命名该列(如有必要)并在 config/initializers/devise.rb 中选择正确的加密器模块。

作为引用,我的用户模型中的“devise”子句如下所示:

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

请注意,像这样覆盖 :authentication_keys 以便用户使用他们的登录名而不是电子邮件地址登录需要我修改一些设计 View :railsgenerate devise:views,然后编辑文件。

希望这能有所帮助。祝你好运!

关于ruby-on-rails - 从 Authlogic 迁移到 Devise,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3840072/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com