如何在Rails Devise中自动生成密码? [英] How to auto-generate passwords in Rails Devise?

查看:255
本文介绍了如何在Rails Devise中自动生成密码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试Devise如何与我的一个项目进行用户身份验证。有一个用户要求,他们的管理员应该能够不时地生成一批用户名和用户密码,然后管理员将向用户发送新的用户名和密码。

I am trying out how Devise works with one of my projects for user authentication. There is a user requirement that their admin should be able to generate a batch of username and user's password from time to time, and then the admin will email the new username and password to his users.

假设管理员在MySQL数据库上有直接SQL的知识,Devise可以识别生成的用户名/密码?谢谢!

Assume the admin has the knowledge of direct SQL on the MySQL database, how can the generated usernames/passwords recognized by Devise? Thanks!

推荐答案

使用 Devise.friendly_token 方法:

password_length = 6
password = Devise.friendly_token.first(password_length)
User.create!(:email => 'someone@something.com', :password => password, :password_confirmation => password)

FYI: Devise.friendly_token 返回一个20个字符的令牌。在上面的例子中,我们通过使用 password_length 字符Rails提供的/String.html#method-i-firstrel =noreferrer> String#first 方法。

FYI: Devise.friendly_token returns a 20 character token. In the example above, we're chopping off the first password_length characters of the generated token by using the String#first method that Rails provides.

这篇关于如何在Rails Devise中自动生成密码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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