update_attributes clears password

62 views
Skip to first unread message

jsnark

unread,
Jul 30, 2012, 1:14:24 PM7/30/12
to rubyonra...@googlegroups.com
I have a database table that contains encrypted passwords along with other information relating to users.  When I do an update_attributes operation on a row in the table with a hash that does not contain a password, the password gets reset to the empty string.  How can I stop this?

Michael Pavling

unread,
Jul 30, 2012, 2:39:22 PM7/30/12
to rubyonra...@googlegroups.com


On 30 Jul 2012 18:14, "jsnark" <s...@monmouth.com> wrote:
>
> I have a database table that contains encrypted passwords along with other information relating to users.  When I do an update_attributes operation on a row in the table with a hash that does not contain a password, the password gets reset to the empty string.  How can I stop this?

Remove the password key/value pair from the params hash before the update-attributes call if the password value is blank.

jsnark

unread,
Jul 30, 2012, 3:23:07 PM7/30/12
to rubyonra...@googlegroups.com


On Monday, July 30, 2012 2:39:22 PM UTC-4, pavling wrote:


On 30 Jul 2012 18:14, "jsnark" wrote:
>
> I have a database table that contains encrypted passwords along with other information relating to users.  When I do an update_attributes operation on a row in the table with a hash that does not contain a password, the password gets reset to the empty string.  How can I stop this?

Remove the password key/value pair from the params hash before the update-attributes call if the password value is blank.


The hash does not contain a password key/value pair.  In spite of this, the password is set to the empty string.

  Parameters: {"utf8"=>"✓", "authenticity_token"=>"AdPOoGvw9LXnkLEen9NzXo/yhwESO6hRxnICD2eK4Rk=", "user"=>{"role_id"=>"3"}, "commit"=>"Update User", "id"=>"1"}

Colin Law

unread,
Jul 30, 2012, 4:03:39 PM7/30/12
to rubyonra...@googlegroups.com
Have you got any callbacks in the model (before_save for example)?
What does the log (log/development.log) show for the action? It
should show the sql.

If you still can't see it post the action code, the model (strip out
any irrelevant methods) and the log (just for the action).

Colin

Colin

>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To unsubscribe from this group, send email to
> rubyonrails-ta...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/rubyonrails-talk/-/jC2XnDqxCzwJ.
>
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Michael Pavling

unread,
Jul 30, 2012, 4:07:20 PM7/30/12
to rubyonra...@googlegroups.com
On 30 July 2012 20:23, jsnark <s...@monmouth.com> wrote:
> The hash does not contain a password key/value pair. In spite of this, the
> password is set to the empty string.

What filters run in the model? What observers are operating?
It would be worth setting a breakpoint at your update_attribute line
and following through from there to see what happens to your password
attribute.

jsnark

unread,
Jul 30, 2012, 5:39:39 PM7/30/12
to rubyonra...@googlegroups.com


On Monday, July 30, 2012 4:07:20 PM UTC-4, pavling wrote:
On 30 July 2012 20:23, jsnark wrote:
> The hash does not contain a password key/value pair.  In spite of this, the
> password is set to the empty string.

What filters run in the model? What observers are operating?
It would be worth setting a breakpoint at your update_attribute line
and following through from there to see what happens to your password
attribute.

I understand the problem now, but I do not see the solution.  The model has a before_save filter that is causing the password to be reset.  How do I stop this on an update?

require 'authenticators/sql_encrypted'

class User < ActiveRecord::Base

  belongs_to :role
  belongs_to :person

  include CASServer::Authenticators::SQLEncrypted::EncryptedPassword

  attr_accessor :password

  validates :password, :confirmation => true,
    :length => { :within => 7..20 },
    :format => { :with => /^.*(?=.{7,20})(?=.*[a-zA-Z])(?=.*[0-9])(?=.*[`~!@#\$%^&*-_=+|;':",.\/<>?]).*$/ },
    :presence => true,
    :if => :password_required?

  validates_uniqueness_of :username

  before_save :encrypt_password

  protected

  def password_required?
    encrypted_password.blank? || password.present?
  end

  def encrypt_password
    self.encrypted_password = encrypt(self.password)
  end

end

Michael Pavling

unread,
Jul 30, 2012, 6:40:51 PM7/30/12
to rubyonra...@googlegroups.com
On 30 July 2012 22:39, jsnark <s...@monmouth.com> wrote:
> I understand the problem now, but I do not see the solution. The model has
> a before_save filter that is causing the password to be reset. How do I
> stop this on an update?

The same way I said before - only run it if the password has been populated:

def encrypt_password
self.encrypted_password = encrypt(self.password) unless
self.password.blank?
end

But you will probably need to add same validation to ensure there is
an encrypted_password - otherwise it would be possible to create
accounts with blank passwords...

jsnark

unread,
Jul 31, 2012, 9:11:24 AM7/31/12
to rubyonra...@googlegroups.com


On Monday, July 30, 2012 6:40:51 PM UTC-4, pavling wrote:

Thank you.

Jason Fleetwood-Boldt

unread,
Jul 31, 2012, 10:14:21 AM7/31/12
to rubyonra...@googlegroups.com


Also note that before_save (and pretty much all the callbacks) take :if and :unless parameters, like so

before_save do_something, :if => Proc.new {|model| model.some_boolean_attr_or_method }



--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/RIfvsT32vIsJ.
Reply all
Reply to author
Forward
0 new messages