From 64321f5c46422bb55d258376c6277e91e5824da2 Mon Sep 17 00:00:00 2001 From: lukemelia Date: Tue, 17 Jul 2007 04:47:35 +0000 Subject: [PATCH] Create a named route for the preferences page and use it. Rename "crypt_token" to "generate_token". git-svn-id: http://www.rousette.org.uk/svn/tracks-repos/trunk@569 a4c988fc-2ded-0310-b66e-134b36920a42 --- tracks/app/controllers/users_controller.rb | 12 ++++++------ tracks/app/models/user.rb | 15 ++++++++------- tracks/app/views/layouts/standard.rhtml | 2 +- tracks/app/views/users/change_auth_type.rhtml | 2 +- tracks/app/views/users/change_password.rhtml | 2 +- tracks/config/routes.rb | 2 ++ tracks/test/functional/users_controller_test.rb | 2 +- tracks/test/unit/user_test.rb | 4 ++-- 8 files changed, 22 insertions(+), 19 deletions(-) diff --git a/tracks/app/controllers/users_controller.rb b/tracks/app/controllers/users_controller.rb index 09222959..9d750597 100644 --- a/tracks/app/controllers/users_controller.rb +++ b/tracks/app/controllers/users_controller.rb @@ -142,7 +142,7 @@ class UsersController < ApplicationController def update_password @user.change_password(params[:updateuser][:password], params[:updateuser][:password_confirmation]) notify :notice, "Password updated." - redirect_to :controller => 'preferences' + redirect_to preferences_path rescue Exception => error notify :error, error.message redirect_to :action => 'change_password' @@ -171,7 +171,7 @@ class UsersController < ApplicationController @user.auth_type = params[:user][:auth_type] if @user.save notify :notice, "Authentication type updated." - redirect_to :controller => 'preferences' + redirect_to preferences_path else notify :warning, "There was a problem updating your authentication type: #{ @user.errors.full_messages.join(', ')}" redirect_to :action => 'change_auth_type' @@ -207,7 +207,7 @@ class UsersController < ApplicationController else notify :warning, "You have successfully verified #{openid_url} as your identity but there was a problem saving your authentication preferences." end - redirect_to :controller => 'preferences', :action => 'index' + redirect_to preferences_path when OpenID::CANCEL notify :warning, "Verification cancelled." @@ -220,10 +220,10 @@ class UsersController < ApplicationController def refresh_token - @user.crypt_token - @user.save + @user.generate_token + @user.save! notify :notice, "New token successfully generated" - redirect_to :controller => 'preferences', :action => 'index' + redirect_to preferences_path end private diff --git a/tracks/app/models/user.rb b/tracks/app/models/user.rb index 947aa5c5..057ed0ff 100644 --- a/tracks/app/models/user.rb +++ b/tracks/app/models/user.rb @@ -92,7 +92,7 @@ class User < ActiveRecord::Base validates_uniqueness_of :login, :on => :create validates_presence_of :open_id_url, :if => Proc.new{|user| user.auth_type == 'open_id'} - before_create :crypt_password, :crypt_token + before_create :crypt_password, :generate_token before_update :crypt_password def validate @@ -151,6 +151,11 @@ class User < ActiveRecord::Base time.to_date end + def generate_token + new_token = Digest::SHA1.hexdigest "#{Time.now.to_i}#{rand}" + write_attribute("token", new_token) + end + def remember_token? remember_token_expires_at && Time.now.utc < remember_token_expires_at end @@ -170,12 +175,8 @@ class User < ActiveRecord::Base protected - def self.sha1(pass) - Digest::SHA1.hexdigest("#{Tracks::Config.salt}--#{pass}--") - end - - def crypt_token - write_attribute("token", self.class.sha1(login + Time.now.to_i.to_s + rand.to_s)) + def self.sha1(s) + Digest::SHA1.hexdigest("#{Tracks::Config.salt}--#{s}--") end def crypt_password diff --git a/tracks/app/views/layouts/standard.rhtml b/tracks/app/views/layouts/standard.rhtml index b7183a2e..028f0c81 100644 --- a/tracks/app/views/layouts/standard.rhtml +++ b/tracks/app/views/layouts/standard.rhtml @@ -55,7 +55,7 @@ window.onload=function(){
  • <%= navigation_link( "Tickler", tickler_path, :title => "Tickler" ) %>
  • <%= navigation_link( "Done", done_path, {:accesskey=>"d", :title=>"Completed"} ) %>
  • <%= navigation_link( "Notes", notes_path, {:accesskey => "o", :title => "Show all notes"} ) %>
  • -
  • <%= navigation_link( "Preferences", {:controller => "preferences", :action => "index"}, {:accesskey => "u", :title => "Show my preferences"} ) %>
  • +
  • <%= navigation_link( "Preferences", preferences_path, {:accesskey => "u", :title => "Show my preferences"} ) %>
  • <%= navigation_link( "Import/Export", {:controller => "data", :action => "index"}, {:accesskey => "i", :title => "Import and export data"} ) %>
  • <% if @user.is_admin? -%>
  • <%= navigation_link("Admin", users_path, {:accesskey => "a", :title => "Add or delete users"} ) %>
  • diff --git a/tracks/app/views/users/change_auth_type.rhtml b/tracks/app/views/users/change_auth_type.rhtml index d9ec6e4b..88122cf6 100644 --- a/tracks/app/views/users/change_auth_type.rhtml +++ b/tracks/app/views/users/change_auth_type.rhtml @@ -9,7 +9,7 @@ <% form_tag :action => 'update_auth_type' do %>
    <%= select('user', 'auth_type', Tracks::Config.auth_schemes.collect {|p| [ p, p ] }) %>
    -
    <%= submit_tag 'Change Authentication Type' %> <%= link_to 'Cancel', :controller => 'preferences' %>
    +
    <%= submit_tag 'Change Authentication Type' %> <%= link_to 'Cancel', preferences_path %>
    <%= observe_field( :user_auth_type, :function => "$('open_id').style.display = value == 'open_id' ? 'block' : 'none'") %> diff --git a/tracks/app/views/users/change_password.rhtml b/tracks/app/views/users/change_password.rhtml index 3d43cb1e..bd092291 100644 --- a/tracks/app/views/users/change_password.rhtml +++ b/tracks/app/views/users/change_password.rhtml @@ -17,7 +17,7 @@ <%= password_field "updateuser", "password_confirmation", :size => 40 %> - <%= link_to 'Cancel', :controller => 'preferences' %> + <%= link_to 'Cancel', preferences_path %> <%= submit_tag 'Change password' %> diff --git a/tracks/config/routes.rb b/tracks/config/routes.rb index ec198109..92070765 100644 --- a/tracks/config/routes.rb +++ b/tracks/config/routes.rb @@ -51,6 +51,8 @@ ActionController::Routing::Routes.draw do |map| # Feed Routes map.connect 'feeds', :controller => 'feedlist', :action => 'index' + + map.preferences 'preferences', :controller => 'preferences', :action => 'index' # Install the default route as the lowest priority. map.connect ':controller/:action/:id' diff --git a/tracks/test/functional/users_controller_test.rb b/tracks/test/functional/users_controller_test.rb index 77429849..bec5355a 100644 --- a/tracks/test/functional/users_controller_test.rb +++ b/tracks/test/functional/users_controller_test.rb @@ -55,7 +55,7 @@ class UsersControllerTest < Test::Rails::TestCase assert_response :success assert_equal assigns['page_title'], "TRACKS::Change password" post :update_password, :updateuser => {:password => 'newpassword', :password_confirmation => 'newpassword'} - assert_redirected_to :controller => 'preferences' + assert_redirected_to preferences_path @updated_user = User.find(users(:admin_user).id) assert_equal @updated_user.crypted_password, Digest::SHA1.hexdigest("#{Tracks::Config.salt}--newpassword--") assert_equal "Password updated.", flash[:notice] diff --git a/tracks/test/unit/user_test.rb b/tracks/test/unit/user_test.rb index db1e6a25..7f7848e1 100644 --- a/tracks/test/unit/user_test.rb +++ b/tracks/test/unit/user_test.rb @@ -178,9 +178,9 @@ class UserTest < Test::Rails::TestCase assert User.no_users_yet? end - def test_crypt_token_updates_token + def test_generate_token_updates_token assert_value_changed @admin_user, :token do - @admin_user.send :crypt_token + @admin_user.send :generate_token end end