রেলস কনসোল থেকে পাসওয়ার্ড পুনরায় সেট করুন


92

কোনও অ্যাপ্লিকেশন চালানোর সময় আপনি কীভাবে ইমেল ঠিকানার মাধ্যমে কোনও ব্যবহারকারীকে নির্বাচন করবেন এবং তারপরে rails consoleডিভাইসটির জন্য ম্যানুয়ালি পাসওয়ার্ড সেট করবেন ?

এছাড়াও, আমি ডিভাইস ব্যবহার করার সময় অ্যাকাউন্টগুলির হেরফের সম্পর্কে এই বিষয়ে আরও বিশদটি কভার করতে ডকুমেন্টেশন পর্যালোচনা করতে কোথায় যাব?


Valk: যেখানে () রেল 3 অবধি উপলব্ধ নয় তবে আপনি যেভাবে এটি করেছেন তা বেশ ভাল।
25:58

উত্তর:


140

আপনি বর্ণিত হিসাবে এটি কমবেশি :-)

# use mongoid
class User
  include Mongoid::Document
end


# then
user = User.where(email: 'joe@example.com').first

if user
  user.password = new_password
  user.password_confirmation = new_password
  user.save
end

Update from 6 years later :)

Modern devise allows simpler syntax, no need to set the confirmation field

user.password = new_password; user.save
# or
user.update_attributes(password: new_password)

Ah, hmm. That would work for a standard user, but in this case it's from the admin_users table. What's the appropriate tweak to pull from this table vs the users? Simply setting it to user = AdminUser... did not work.
ylluminate

Umm, I don't know, query AdminUser model? As for me, I always stored all users in the same tables, with 'roles' attribute assigned.
Sergio Tulentsev

You can change name of collection that model refers to with :store_in method. So, to look in admin_users table you'd have to add User.store_in 'admin_users' before that code. (this answer implies using of Mongoid)
Sergio Tulentsev

Attempted User.store_in 'admin_users' however received undefined method. I'm not seemingly able to access the table as I'm just getting a nil back each time. What about querying the entire table and just getting all entries therein initially to test to see if I'm getting into that table initially? (Working in MySQL here, however that shouldn't matter with ActiveRecord.)
ylluminate

:store_in is a part of Mongoid gem. You can get access to low(er)-level ruby driver by calling User.db
Sergio Tulentsev

55
# $ rails console production
u=User.where(:email => 'usermail@gmail.com').first
u.password='userpassword'
u.password_confirmation='userpassword'
u.save!

1
devise is baked in rails so the use of pw confirmation is redundant. User.find_by_email('joe@example.com').update_attributes(:password => 'password')
copremesis

27

If you run the following in the rails console it should do the trick:

User.find_by(email: 'user_email_address').reset_password!('new_password','new_password')

http://www.rubydoc.info/github/plataformatec/devise/Devise/Models/Recoverable


6
Note the exclamation mark is deprecated, it is just : User.find_by(email: 'user_email_address').reset_password('new_password','new_password')
IrishDubGuy

1
Also note that you have to enter a valid password that confirms to the password requirements in your devise configuration.
zwippie

5

You can simply update password field, no need for confirmation password, devise will save it in encrypted form

u = User.find_by_email('user@example.com')
u.update_attribute(:password, '123123')

3

For some reason, (Rails 2.3??)

user = User.where(:email => email).first

didn't work for me, but

user = User.find_by_email('user@example.com')

did it.


The reason for this being that the where(); method was not yet in rails 2.3, we used to use the find(:all, :conditions => conditions) back in the days.
dennis

3

1.Login in to ralis console

$ sudo bundle exec rails console production

2.Then update the administrator's password

irb(main):001:0> user = User.where("username = 'root'")
irb(main):002:0> u = user.first
irb(main):003:0> u.password="root2014@Robin"
=> "root2014@Robin"
irb(main):004:0> u.password_confirmation="root2014@Robin"
=> "root2014@Robin"
irb(main):005:0> u.save
=> true
irb(main):006:0> exit

3.Refresh the login page, use the new password to login, enjoy!

Good Luck!


devise is baked in so the use of pw confirmation is redundant. User.find_by_email('joe@example.com').update_attributes(:password => 'password')
copremesis


0

If your account is locked from too many login attempts, you may also need to do:

user.locked_at = ''
user.failed_attempts = '0'
user.save!
আমাদের সাইট ব্যবহার করে, আপনি স্বীকার করেছেন যে আপনি আমাদের কুকি নীতি এবং গোপনীয়তা নীতিটি পড়েছেন এবং বুঝতে পেরেছেন ।
Licensed under cc by-sa 3.0 with attribution required.