将设计身份validation纳入现有的用户结构?

我有一个function齐全的身份validation系统,其用户表有超过50列。 它很简单,但它使用salt进行散列加密,使用电子邮件而不是用户名,并且还有两种不同类型的用户和管理员。

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

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 

为了便于参考,这里是迁移的Devise字段:

  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 Devise迁移字段,一切都会好或我还需要做其他事情吗?

编辑:

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

由于某种原因,堆栈跟踪显示电子邮件显然没有被其他变量输入…虽然种子文件中的其他内容都显示在实际查询中,但电子邮件由于某种原因“它是明确定义的

我记得当我们做类似事情时遇到的两个主要考虑因素是:

数据库迁移 – 我们编写了单独的add_column and rename_column语句,而不是使用t.database_authenticatable帮助add_column and rename_column ,因此我们没有遇到您看到的任何重复的列或索引错误,因此我们可以重用我们的salt&在Devise中散列密码,而无需修改gem的工作方式。

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

我希望这足以让你开始。

我会删除你的架构中的:default =>“”电子邮件。 默认情况下,devise会对电子邮件设置唯一约束,因此您不希望默认为空字符串

我去年将authLogic(我认为)的应用程序切换到Devise,我的课程是: – 用户表可以保留 – 重命名列到设计标准,我确信这不是必需的,但与其他身份validation方法不同,我没有找到一个lib映射文件,我可以在其中添加不同的db字段名称,就像我使用其他身份validation方法一样。

那实际上是关于它的。 我真的很惊讶我需要做的很少。 我认为我的哈希实际上仍然有效,或者我按照指示中的指示改为常规。

我使用带有Devise的’admin’标志路由,所以从当前使用的任何东西做一个sql转换就可以做到。

我也会摆脱默认的“”位,我有一个生产应用程序(不知道它使用什么身份validation,我认为一些base64的东西),看起来像:t.string“EMAIL”,:limit => 64,: null => false t.string“PASSWORD”,:limit => 64,:null => false