代码之家  ›  专栏  ›  技术社区  ›  Kevin

将designe身份验证合并到现有的用户结构中?

  •  7
  • Kevin  · 技术社区  · 14 年前

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

    我正在寻找合并到我的应用程序设计认证,以加强额外的部分,如电子邮件验证,忘记密码,记住我的令牌,等等。。。我只是想看看是否有人有任何建议或他们遇到的问题时,将设计纳入一个已经存在的用户结构。我的用户模型中的基本字段是:

      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
    

    为了便于参考,以下是迁移中的design字段:

      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"
    

    你们推荐什么?我只是从我的迁移中删除电子邮件、散列密码和salt,然后放入5个designe迁移字段,一切都会好起来,还是我需要做其他事情?

    编辑:

    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
    

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

    3 回复  |  直到 13 年前
        1
  •  2
  •   Graham Savage    14 年前

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

    数据库迁移—而不是使用 t.database_authenticatable 帮手们,我们写了 add_column and rename_column 语句,这样我们就不会遇到任何重复的列或索引错误,这样我们就可以在designe中重用salt&hash密码,而不必修改gem的工作方式。

    第二个更重要的考虑因素是,我们使用的哈希算法与design提供的算法不同,因此我们必须将自己的加密程序类作为 Devise::Encryptors::Base ,并使用我们自己的逻辑实现摘要函数。最后,我们通过在适当的config/initializer文件中用 config.encryptor = :our_own_algorithm

    我希望这足够让你开始。

        2
  •  0
  •   Nader    14 年前

    我会去掉电子邮件模式中的:default=>“”。designe在默认情况下对email设置了一个惟一的约束,所以您不希望默认值为空字符串

        3
  •  0
  •   Michael Durrant    14 年前

    去年我从authLogic(我想)换了一个应用程序来设计,我的经验是: -用户表可以保留 -重命名列以设计标准,我相信这应该不是必需的,但与其他身份验证方法不同,我没有找到一个lib映射文件,我可以在其中添加不同的db字段名,就像我对其他身份验证方法所做的那样。

    实际上就是这样。我真的很惊讶我所需要做的是如此之少。我想我的哈希实际上仍然有效,或者我按照指令中的指示改成了例程。

    我也会去掉默认的“”位,我有一个生产应用程序(不确定它使用什么身份验证,我想是base64之类的东西),它看起来像: t、 字符串“PASSWORD”,:limit=>64,:null=>false