7

我有一个功能齐全的身份验证系统,其中包含一个包含五十多列的用户表。这很简单,但它使用盐进行哈希加密,使用电子邮件而不是用户名,并且还有两种不同类型的用户和管理员。

我希望将设计身份验证合并到我的应用程序中,以加强额外的部分,例如电子邮件验证、忘记密码、记住我的令牌等……我只是想看看是否有人在合并时遇到任何建议或问题设计成已经存在的用户结构。我的用户模型中的基本字段是:

  t.string    :first_name, :null => false
  t.string    :last_name, :null => false
  t.string    :email, :null => false
  t.string    :hashed_password
  t.string    :salt
  t.boolean   :is_userA, :default => false
  t.boolean   :is_userB, :default => false
  t.boolean   :is_admin, :default => false
  t.boolean :active, :default => true
  t.timestamps

作为参考,这里是迁移中的设计字段:

  t.database_authenticatable :null => false
  t.confirmable
  t.recoverable
  t.rememberable
  t.trackable

  add_index "users", ["confirmation_token"], :name => "index_users_on_confirmation_token", :unique => true
  add_index "users", ["email"], :name => "index_users_on_email", :unique => true
  add_index "users", ["reset_password_token"], :name => "index_users_on_reset_password_token", :unique => true

最终变成架构中的这些实际字段:

t.string   "email",                               :default => "", :null => false
t.string   "encrypted_password",   :limit => 128, :default => "", :null => false
t.string   "password_salt",                       :default => "", :null => false
t.string   "confirmation_token"
t.datetime "confirmed_at"
t.datetime "confirmation_sent_at"
t.string   "reset_password_token"
t.string   "remember_token"
t.datetime "remember_created_at"
t.integer  "sign_in_count",                       :default => 0
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string   "current_sign_in_ip"
t.string   "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"

大家有什么推荐的?我是否只是从我的迁移中删除电子邮件、hashed_pa​​ssword 和 salt,然后放入 5 个设计迁移字段,一切都会好起来的,还是我需要做其他事情?

编辑:

我自己已经开始尝试这个并且已经遇到了一些问题。我将上面显示的设计迁移字段添加到我现有的用户模型中,现在当我运行我的种子文件时,它给了我这个 Postgresql 错误:

ERROR: duplicate key value violates unique constraint "index_users_on_email"

我的种子文件:

initial_usersA = User.create!(
[
{
    :first_name => "John", 
    :last_name => "Doe",
    :email => "johndoe@gmail.com",
    :is_userA => true,
    :is_userB => false,
            :is_admin => true,
    :password => "password",
    :password_confirmation => "password"
},
{
    :first_name => "Jane", 
    :last_name => "Smith",
    :email => "janesmith@gmail.com",
    :is_userA => true,
    :is_userB => false,
            :is_admin => true,
    :password => "password",
    :password_confirmation => "password"
}

用户型号:

devise :registerable, :authenticatable, :recoverable,
     :rememberable, :trackable, :validatable
attr_accessor :password_confirmation, :email, :password

堆栈跟踪显示,由于某种原因,电子邮件显然没有与其余变量一起输入......尽管种子文件中的所有其他内容都显示在实际查询中,但由于某种原因,电子邮件是 ''它是明确定义的.auth

4

3 回答 3

2

我记得我们在做类似事情时面临的两个主要考虑因素是:

t.database_authenticatable数据库迁移 -我们编写了单独的语句,而不是使用帮助程序,add_column and rename_column这样我们就不会遇到您看到的任何重复的列或索引错误,这样我们就可以在 Devise 中重用我们的盐和哈希密码而无需修改 gem 的工作方式。

第二个也是更大的考虑是,我们使用的散列算法与 Devise 提供的不一样,因此我们必须编写自己的加密器类作为 的子类Devise::Encryptors::Base,并使用我们自己的逻辑实现摘要函数。最后,我们通过在适当的 config/initializer 文件中指定它来配置 Devise 以使用此加密器config.encryptor = :our_own_algorithm

我希望这足以让你开始。

于 2011-04-16T07:33:09.637 回答
0

去年我将一个应用程序从 authLogic(我认为)切换到了 Devise,我的课程是: - 用户表可以保留 - 将列重命名为 Devise 标准,我确信这不应该是必要的,但与其他身份验证方法不同,我没有找到一个 lib 映射文件,我可以在其中添加不同的数据库字段名称,就像我使用其他身份验证方法所做的那样。

实际上就是这样。我真的很惊讶我需要做的事情这么少。我认为我的哈希实际上仍然有效,或者我按照说明更改为常规。

我将“admin”标志路由与 Devise 一起使用,因此从当前使用的任何内容进行 sql 转换都可以做到这一点。

我还将摆脱默认的“”位,我有一个生产应用程序(不确定它使用什么身份验证,我认为是一些 base64 的东西),它看起来像: t.string "EMAIL", :limit => 64, : null => false t.string "PASSWORD", :limit => 64, :null => false

于 2011-04-22T17:10:13.013 回答
0

我会在您的电子邮件架构中摆脱 :default => "" 。devise 默认情况下对电子邮件设置唯一约束,因此您不希望空字符串的默认值

于 2010-07-28T17:40:57.567 回答