结合设计验证到一个已经存在的用户结构? [英] Incorporating Devise Authentication into an already existing user structure?

查看:214
本文介绍了结合设计验证到一个已经存在的用户结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有五十列的用户表一个全功能的认证系统。这很简单,但它确实哈希加密盐,使用的用户名,而不是电子邮件,并有两个单独类型的用户与管理员为好。

I have a fully functional authentication system with a user table that has over fifty columns. It's simple but it does hash encryption with salt, uses email instead of usernames, and has two separate kinds of users with an admin as well.

我期待整合设计验证到我的应用程序来加强多余的部分,如电子邮件验证,忘记密码,还记得我令牌等...我只是想看看是否有人有他们已经任何意见或问题结合设计成一个已经存在的用户结构时遇到的。在我的用户模型的基本字段是:

I'm looking to incorporate Devise authentication into my application to beef up the extra parts like email validation, forgetting passwords, remember me tokens, etc... I just wanted to see if anyone has any advice or problems they've encountered when incorporating Devise into an already existing user structure. The essential fields in my user model are:

  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

另外,为了参考,这里是从迁移的设计领域:

For reference sake, here's the Devise fields from the migration:

  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

这最终变成现实,这些领域在架构:

That eventually turn into these actual fields in the schema:

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,和盐放在5制定移民领域,一切都会好的或者我需要做点别的?

What do you guys recommend? Do I just remove email, hashed_password, and salt from my migration and put in the 5 Devise migration fields and everything will be OK or do I need to do something else?

编辑:

我已经开始这种尝试自己和已经碰到了一些问题。我加了色器件迁移领域我上面给我的现有的用户模型显示,现在,当我跑我的种子文件,它给了我这个错误PostgreSQL的:

I've started to attempt this myself and have already run into some problems. I added the devise migration fields I showed above to my existing user model, and now when I run my seeds file it gives me this Postgresql error:

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"
}

User模型:

User model:

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

堆栈跟踪显示电子邮件显然没有被送入的变量,其余为某种原因......虽然一切在种子文件中的实际查询显示出来,电子邮件是「一些原因即使它明确defined.auth

The stack trace shows that the email apparently isn't being fed in with the rest of the variables for some reason... though everything else in the seed file shows up in the actual query, the email is '' for some reason even though it's explicitly defined.auth

推荐答案

这两个主要考虑我记得我们面对的时候我们做了类似的事情是:

The two main considerations I recall we faced when we did a similar thing were:

数据库迁移 - 因此而不是使用 t.database_authenticatable 帮手,我们写了个人 add_column和rename_column 语句,我们没有遇到任何重复的列或索引错误,你已经看到了,所以这样我们就可以重新使用我们的盐放;在设计散列口令,而无需修改宝石是如何工作的。

Database Migrations - rather than using the t.database_authenticatable helpers, we wrote individual add_column and rename_column statements, so that we didn’t run into any duplicate column or index errors that you’ve see, and so so that we could reuse our salt & hashed passwords within Devise without having to modify how the gem works.

第二,大,考虑的是,我们所使用的哈希算法是不一样的设计提供的,所以我们不得不编写自己的加密类的子类,设计::加密机:: Base的,并实现使用我们自己的逻辑摘要功能。最后,我们配置设计在适当的配置/初始化文件, config.encryptor =指定它使用这种加密:our_own_algorithm

The second, and larger, consideration, was that the hashing algorithm we used was not the same as any that Devise provided, and so we had to write our own encryptor class as subclass of Devise::Encryptors::Base, and implement the digest function using our own logic. Finally, we configured Devise to use this encryptor by specifying it in the appropriate config/initializer file with config.encryptor = :our_own_algorithm

我希望这给你足够让你开始。

I hope this gives you enough to get you started.

这篇关于结合设计验证到一个已经存在的用户结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆