Changing password is not working right.

Hi I am trying to change a password from my DB, adding a new user works.. login in as the new user also works.. to change the user password does not works right.. I basically using a bit of the login code with the new user code.. what I am trying to do..( I MUST use unix crypt no way I can change this unfortunately) 1. I need to check for the email and password and I add this to variables of course the password I need to encrypted from plain text before I check this..( I also do this when login in.. and works) 2. after that is true then I go ahead to try to change the password..

the issue? I seem to change it but after that I cant log in anymore.. so somehow the .crypt(salt) that I am using for login and creating the user is not working here.. Im a noob so please let me know what can this be.

I also cant seem to see anything on the development.log :frowning: how can I see what is going on between the form and rails? with no eyes I cant troubleshoot.

----------------snip----------------- def change_password

        old_password = params[:crypt_old].crypt(params[:crypt_old])         new_password = params[:crypt_new].crypt(params[:crypt_new])         username = params[:email]         auth2 = Users.find(:first, :conditions => [ "email = '%s' AND crypt = '%s'",username, old_password ])         if auth2         auth2.update_attribute(:crypt, new_password)         redirect_to :action => 'si'         else         redirect_to :action => 'no'         end    end end

------------- snip ------------------

<div class="depot-form">

<%= error_messages_for 'login' %>

<fieldset> <legend>Enter your email, your old and new password</legend> <% form_tag :action => 'change_password' do %>     <p>         <label for="email">Email</label><br/>         <%= text_field_tag :email %>     </p>

  <p>         <label for="crypt_old">Old Password</label><br/>         <%= password_field_tag :crypt_old %>    </p>

<p>         <label for="crypt_new">New Password</label><br/>         <%= password_field_tag :crypt_new %>    </p>

<%= submit_tag "Login" %> <% end %> </fieldset> </div>

Hi--

See my comments interleaved and at the end.

1. I need to check for the email and password and I add this to variables of course the password I need to encrypted from plain text before I check this..( I also do this when login in.. and works) 2. after that is true then I go ahead to try to change the password..

the issue? I seem to change it but after that I cant log in anymore.. so somehow the .crypt(salt) that I am using for login and creating the user is not working here.. Im a noob so please let me know what can this be.

I also cant seem to see anything on the development.log :frowning: how can I see what is going on between the form and rails? with no eyes I cant troubleshoot.

Use the logger to see what's going on.

----------------snip----------------- def change_password        old_password = params[:crypt_old].crypt(params[:crypt_old])

          # See if the value is what we expect           logger.debug "old password:\n" + old_password.inspect

       new_password = params[:crypt_new].crypt(params[:crypt_new])

          # See if the new password is what we think we should see           logger.debug "new password:\n" + new_password.inspect

       username = params[:email]        auth2 = Users.find(:first, :conditions => [ "email = '%s' AND crypt = '%s'",username, old_password ])        if auth2        auth2.update_attribute(:crypt, new_password)        redirect_to :action => 'si'        else        redirect_to :action => 'no'        end   end end

------------- snip ------------------

<div class="depot-form">

<%= error_messages_for 'login' %>

<fieldset> <legend>Enter your email, your old and new password</legend> <% form_tag :action => 'change_password' do %>    <p>        <label for="email">Email</label><br/>        <%= text_field_tag :email %>    </p>

<p>        <label for="crypt_old">Old Password</label><br/>        <%= password_field_tag :crypt_old %>   </p>

<p>        <label for="crypt_new">New Password</label><br/>        <%= password_field_tag :crypt_new %>   </p>

<%= submit_tag "Login" %> <% end %> </fieldset> </div>

I would expect params[:crypt_new] and params[:crypt_old] to contain simple strings. That suggests that your code that references params[:crypt_new].params[:crypt_new] may not be working as you expect.

The reason I used the "inspect" method in the logger.debug call is in case the object has more information to reveal than a typical to_s would reveal.

Hope this gets you going.

  

Hi, I added this lines, thanks for the tips. hmm but after I run it I still don't see anything on the log.. I can only see in the log when I use the console manually ./script/console and do queries that way to test my code. but from the controller nothing at all :frowning:

params[:crypt_new].params[:crypt_new]

Im confused, were do I have a params.params? I do have a .crypt(salt) at params[:crypt_new].crypt(params[:crypt_new])

basically is using its own password as salt. this is how I have it on login/create etc and seems to work.

Thanks so much

Hi, I added this lines, thanks for the tips. hmm but after I run it I still don't see anything on the log.. I can only see in the log when I use the console manually ./script/console and do queries that way to test my code. but from the controller nothing at all :frowning:

params[:crypt_new].params[:crypt_new]

OK, you started your server with something like script/server, right? Open another window, cd to the directory of your rails application and (assuming you are running *nix) just type:

tail -f log/development.log

And all the logging messages will scroll by as you hit your site with a Web browser.

Im confused, were do I have a params.params? I do have a .crypt(salt) at params[:crypt_new].crypt(params[:crypt_new])

The code is what you posted. I don't know what it's supposed to do. Look over what's happening with debug logs and see where that leads you.

Hope this helps.

OK, you started your server with something like script/server, right? Open another window, cd to the directory of your rails application and (assuming you are running *nix) just type:

tail -f log/development.log

And all the logging messages will scroll by as you hit your site with a Web browser.

Hi, I am running it under apache, hmmm maybe is showing on the apache logs? let me check

Chris F.

You're running directly under Apache? As in mod_ruby? Or CGI? I would suggest switching to mongrel right away. It will speed up your development process immensely and you'll find it hosts Rails more neatly than does Apache's mod_ruby and it's orders of magnitude faster than CGI.

All you have to do is:

gem install mongrel

--then--

cd /path/to/your/app mongrel_rails mongrel::start

You're running directly under Apache? As in mod_ruby? Or CGI? I would suggest switching to mongrel right away. It will speed up your development process immensely and you'll find it hosts Rails more neatly than does Apache's mod_ruby and it's orders of magnitude faster than CGI.

All you have to do is:

gem install mongrel

--then--

cd /path/to/your/app mongrel_rails mongrel::start

-

Hi, hey thanks, yes I was even thinking in running ngix. the thing is that we dont want to run 2 different webservers on this server, we need apache to run most of our php sites,( am a php developer getting into the ruby world now hehe) BTWthe Apache logs dint give me the results of those two lines I added.. :frowning: I really wish I could debug this.. is probably something very simple to fix. am I doing correct the sentence to update the objet on the db? my manual debug always ends up there.

Thanks

ohh forgot to add Im running it under cgi.

rek2 escribió:

Run it under mongrel on your development machine. When you get it working, deploy it to your server under mongrel and proxy from apache using something like this:

<VirtualHost *:80>     ServerName www.yourapplication.com     DocumentRoot /var/www/rails/yourapplication/current/public     ProxyRequests off     ProxyPass / http://localhost:8021/     ProxyPassReverse / http://localhost: 8021     ProxyPreserveHost on </VirtualHost>

Your mongrel would be started *on your production server* using something like:

mongrel_rails mongrel::start -e production -p 8021 -d

And that creates a mongrel listening on port 8021, which you've told Apache to proxy requests to. Presto an Apache/mongrel solution. When you get more traffic, look into using mod_proxy_balancer, but it sounds like right now you don't have this working on your development system, correct?

Run it under mongrel on your development machine. When you get it working, deploy it to your server under mongrel and proxy from apache using something like this:

<VirtualHost *:80>     ServerName www.yourapplication.com     DocumentRoot /var/www/rails/yourapplication/current/public     ProxyRequests off     ProxyPass / http://localhost:8021/     ProxyPassReverse / http://localhost: 8021     ProxyPreserveHost on </VirtualHost>

Your mongrel would be started *on your production server* using something like:

mongrel_rails mongrel::start -e production -p 8021 -d

And that creates a mongrel listening on port 8021, which you've told Apache to proxy requests to. Presto an Apache/mongrel solution. When you get more traffic, look into using mod_proxy_balancer, but it sounds like right now you don't have this working on your development system, correct?

Hi, thanks for the tip, well first the good news.. I good my app to work!! what I did is start the webserver that comes with rails, this *do* was login fine.. so I saw the issue right away... the problem was that I have another model that I use for the editing of the user settings and there I told encrypt to encrypt that part ... now I thought that this model and this other controller will be different .. so basically I was encrypting two times the password this is why after I changed the password I coulnt log in with the new or neither old password ... :slight_smile: login always saves my live no matter what I do. About production/development.. this is another part I don't understand.. I am doing my development on the production server ;-)(me runs to hide./...) hehe this is on a subdirectory on his own subdomain(using apache with vhosts) so under ruby on rails I have everything as development on the settings since I was following tutorials to set it up.. my question how do I move it to development? for DB what I did is I did a mysql dump of the *real* data and dumped into a development DB on the same server.. and this is the one I am using with rails.. I will have to point rails to the real live DB.(that is indentical) so how do I do this? I have not found anything similar to my case.

Thanks again!

Same way you would do it if you were writing PHP. Use mysqldump, gzip it, and copy it to your local machine. Unzip it and load it into you newly created local mysql database and point your local database.yml at it.

Same way you would do it if you were writing PHP. Use mysqldump, gzip
it, and copy it to your local machine. Unzip it and load it into you
newly created local mysql database and point your local database.yml
at it.    Hi, but is the same mysql server, only different database.. I don't need to dump anything I alredy did that to create a copy.. so then should I just pointed to the live one? so basically server: same server db name: changethis to the right one.? .....

my confusiong comes because Rails have 3 entries on the DB, development testing production

how do I tell rails to use production? as it is now.. I have the same settings on all 3.. but I am using production since there is no testing or production db by that name.

Thanks again.

If you really want to develop on a production box (a bad idea in general), then copy the database, change the entry for development to point to the copy of the database and party on it. I would strongly encourage you to put entries for test and production that don't point to live data -- at least until you've completely tested your app.

If you really want to develop on a production box (a bad idea in
general),

I know I know..

then copy the database, change the entry for development to
point to the copy of the database and party on it.

hmm if I have to change the development entry to point to the live one.. then why is there a production entry? I was hoping so I can keep the development DB put my life one under production and somehow migrate to production every time I do a change on development...

anyway to do this? that way I can still test if I have to add new tables to one and if it works move to production.

I found something similar to what I want.. but that does not mean that my code is pointing to production..

      unning migrations for production and test databases

If you would like to specify what rails environment to use for the migration, use the RAILS_ENV shell variable.

For example:

$ export RAILS_ENV=production $ rake db:migrate

$ export RAILS_ENV=test $ rake db:migrate

$ export RAILS_ENV=development $ rake db:migrate

something like this will be perfect if I can tell rails.. ok point to dev or point to production..

ReK2 escribió:

I think you may be misunderstanding how database.yml is supposed to work. You have any number of configurations, each corresponding to how you've set RAILS_ENV. Normally, these are development, test, and production.

Development points to a database you plan to use to test things out without affecting the live production environment. Test points to a database that your tests will use. This is important because you should be testing your code and those tests will run against a test database. I cannot overemphasize: DO NOT LET TEST POINT AT YOUR OTHER DATABASES. Sorry for yelling, but the first thing the test task does is initialize the database so you lose whatever was there before -- not what you want in dev or production.

Finally, there is production and that's the one where your very important data is stored. I simply don't fill in any values for production until I'm close to running an app in production mode. If there's a chance to make a mistake, I want to avoid that and the cost of recreating production data is too high to mess with.

You might do well to pick up Agile Web Development With Ruby on Rails by Thomas, et. al.

I think you may be misunderstanding how database.yml is supposed to work. You have any number of configurations, each corresponding to how you've set RAILS_ENV. Normally, these are development, test, and production.

Development points to a database you plan to use to test things out without affecting the live production environment. Test points to a database that your tests will use. This is important because you should be testing your code and those tests will run against a test database. I cannot overemphasize: DO NOT LET TEST POINT AT YOUR OTHER DATABASES. Sorry for yelling, but the first thing the test task does is initialize the database so you lose whatever was there before -- not what you want in dev or production.

Finally, there is production and that's the one where your very important data is stored. I simply don't fill in any values for production until I'm close to running an app in production mode. If there's a chance to make a mistake, I want to avoid that and the cost of recreating production data is too high to mess with.

Yes, and that is how I have it.. my application as of now is running 100% as I want it to.. this is why I started to think on production.. I was using all this time development environment.. now I added to the production muy production db.. and I will like to know how to switch from one to the other.. etc.. and yes I did order the book is on his way it will take 2 days more :frowning:

Thanks.