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.
我正在尝试Devise如何使用我的一个用户身份验证项目。用户要求他们的管理员应该能够不时生成一批用户名和用户密码,然后管理员会将新的用户名和密码通过电子邮件发送给他的用户。
Assume the admin has the knowledge of direct SQL on the MySQL database, how can the generated usernames/passwords recognized by Devise? Thanks!
假设管理员具有MySQL数据库上直接SQL的知识,如何通过Devise识别生成的用户名/密码?谢谢!
3 个解决方案
#1
74
Use the Devise.friendly_token
method:
使用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
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.
仅供参考:Devise.friendly_token返回一个20个字符的标记。在上面的示例中,我们使用Rails提供的String#first方法切断生成的令牌的第一个password_length字符。
#2
6
One option would be to use the Devise.generate_token. I.e.
一种选择是使用Devise.generate_token。即
password = User.generate_token('password')
User.create!(:email => 'someone@something.com', :password => password, :password_confirmation => password)
This option has not been available in Devise for quite a while. Please refer to the other answer (friendly_token).
这个选项在Devise中已经有很长一段时间了。请参考其他答案(friendly_token)。
#3
0
(quick caveat: I'm a rails newb)
(快速警告:我是一个新手)
I tried the generate_token but it doesn't do what you think (look at the docs)
我尝试了generate_token,但它没有你想的那样(看看文档)
(I'm using rails 3.0.5, and devise 1.1.7)
(我使用rails 3.0.5,并设计1.1.7)
What I found is that Devise will generate all that stuff for you in the background when you do:
我发现当你做的时候,Devise会在后台为你生成所有东西:
User.create!(:email => "me@example.com", :password => "password")
Devise should create the encrypted_password, and salt for you. (pop open a console and try it out there)
设计应该为您创建encrypted_password和salt。 (弹出一个控制台并在那里试试)
#1
74
Use the Devise.friendly_token
method:
使用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
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.
仅供参考:Devise.friendly_token返回一个20个字符的标记。在上面的示例中,我们使用Rails提供的String#first方法切断生成的令牌的第一个password_length字符。
#2
6
One option would be to use the Devise.generate_token. I.e.
一种选择是使用Devise.generate_token。即
password = User.generate_token('password')
User.create!(:email => 'someone@something.com', :password => password, :password_confirmation => password)
This option has not been available in Devise for quite a while. Please refer to the other answer (friendly_token).
这个选项在Devise中已经有很长一段时间了。请参考其他答案(friendly_token)。
#3
0
(quick caveat: I'm a rails newb)
(快速警告:我是一个新手)
I tried the generate_token but it doesn't do what you think (look at the docs)
我尝试了generate_token,但它没有你想的那样(看看文档)
(I'm using rails 3.0.5, and devise 1.1.7)
(我使用rails 3.0.5,并设计1.1.7)
What I found is that Devise will generate all that stuff for you in the background when you do:
我发现当你做的时候,Devise会在后台为你生成所有东西:
User.create!(:email => "me@example.com", :password => "password")
Devise should create the encrypted_password, and salt for you. (pop open a console and try it out there)
设计应该为您创建encrypted_password和salt。 (弹出一个控制台并在那里试试)