Ruby on Rails Friday, July 27, 2012

I am not exactly following the tutorial. I have customized things according to my requirements.

Regards
Sumit Srivastava

The power of imagination makes us infinite...


On 27 July 2012 15:12, sumit srivastava <sumit.theinvincible@gmail.com> wrote:
This is the complete model. I am trying to encrypt the password.


require 'digest/sha1'

class User < ActiveRecord::Base
  attr_accessible :mailid, :name, :password, :username
      validates:name, :presence=>true
    validates:mailid, :presence=>true
      validates:username, :presence=>true
      validates:password, :presence=>true
      validates_uniqueness_of :username, :mailid
      validates_format_of :mailid, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i, :message => "Invalid email" 
     
      attr_accessor :passwordtext
  has_many :contacts
 
  def self.hashedpass(login, pass)
    u=find(:first, :conditions=>["login = ?", login])
    return nil if u.nil?
    return u if User.encrypt(pass, u.salt)==u.hashed_password
    nil
  end 
 
  def passwordtext=(pass)

    #debugger
    @password1=pass
    self.salt = SecureRandom.hex(10) if !self.salt?
    self.password = User.encrypt(@password1, self.salt)
  end
 
  def self.encrypt(pass, salt)
   Digest::SHA1.hexdigest(pass+salt)
  end
end


Regards
Sumit Srivastava

The power of imagination makes us infinite...



On 27 July 2012 12:46, Michael Pavling <pavling@gmail.com> wrote:
On 27 July 2012 08:07, Michael Pavling <pavling@gmail.com> wrote:
> Which (if any) of those lines is line 26 in your UserController? What
> does the User.encrypt method look like?

Ignore me... I can't even read your error message properly myself.

>> I used the following action to encrypt the password.
>>>
>>>   def password
>>>     #debugger
>>>     @password1=pass
>>>     self.salt = SecureRandom.hex(10) if !self.salt?
>>>     self.password = User.encrypt(@password1, self.salt)
>>>   end
>>

Right... so where does the value of "pass" come from? Is there a
method that returns it?

In the (six year old) tutorial you're following, the method is:

  def password=(pass)
    @password=pass
    self.salt = User.random_string(10) if !self.salt?
    self.hashed_password = User.encrypt(@password, self.salt)
  end

but you've changed it to not take any attributes, and to update
"self.password" rather than "self.hashed_password". It would probably
help a little to post a bit more (all) of your model. Also, I'm
curious why, if you're following a tutorial, would you change large
chunks of the functionality? If you implement it exactly as described,
does it work?

--
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 rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




--
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 rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments:

Post a Comment