将现有用户模型迁移到 Devise
我正在尝试将旧应用程序迁移到 Rails 3 并更改身份验证以使用 Devise。我已经创建了模型和迁移并导入了所有用户数据。
我不打算迁移密码,因为我们不想继续使用现有方案,但我希望能够为用户提供一种简单的体验。
理想情况下,我想捕获登录错误,然后使用旧字段检查密码,如果匹配,则用它更新设计密码。
我可以看到 Warden 给了我一个可以捕获错误的回调,所以我希望我可以捕获登录错误。
但是,由于所有密码(在 Devise 中)都是空白,因此我收到与哈希相关的错误,因为 crypto_password 字段为空。
有没有办法可以使用随机密码更新所有用户帐户?
我在 Devise::Models::DatabaseAuthenticatable 中看到有一个方法 'password=' 但如果我调用它,例如在应用程序的 Rails 控制台中:
User.find(1).password=('new')
=> "new"
我只会得到相同的纯文本字符串('new')并且保存用户记录后,这不会填充 crypto_password 字段。
我已经四处寻找但似乎无法找到它。任何建议非常感谢!
I'm trying to migrate a legacy app to Rails 3 and change the authentication to use Devise. I've created the model and migrations and imported all the user data.
I don't plan to migrate the passwords over as the existing scheme is not one we'd like to use going forward, but I want to be able to present users with a simple experience.
Ideally I'd like to catch a login error and then check the password with the legacy field and then update the Devise password with it if it matches.
I can see that Warden gives me a callback that can trap errors so I expect I can trap a login error.
However because all the passwords (in Devise) are blank I get errors relating to the hash as the encrypted_password fields are empty.
Is there a way I can update all the user accounts with a random password?
I've seen in Devise::Models::DatabaseAuthenticatable that there is a method 'password=' but if I call that, e.g. in rails console for the app:
User.find(1).password=('new')
=> "new"
I just get the same plain text string back ('new') and saving the user record post this doesn't populate the encrypted_password field.
I've searched around but can't seem to be able to find it. Any suggestions much appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,以防万一其他人像我一样在过去 24 小时内头脑发热,以下是设置密码的方法:
真的很简单:)
Ok just in case anyone else is as cloth headed as I have been the last 24 hours, here's how you set the password:
Simple really :)