]> git.openstreetmap.org Git - rails.git/commitdiff
Require a valid session token to resend a confirmation
authorTom Hughes <tom@compton.nu>
Mon, 20 Jul 2015 20:32:34 +0000 (21:32 +0100)
committerTom Hughes <tom@compton.nu>
Mon, 20 Jul 2015 20:32:34 +0000 (21:32 +0100)
Make user#confirm_resend require a valid token in the session
that matches the requested user, and ensure trying to login as
an unconfirmed user sets such a token.

Fixes #1010

app/controllers/user_controller.rb
test/controllers/user_controller_test.rb

index 8a5fa3fe1b0bb62fb27166d791cce513e77039d0..0c251492783f2b0dfbf444734e5f7b4b8aa69aea 100644 (file)
@@ -318,16 +318,19 @@ class UserController < ApplicationController
     else
       user = User.find_by_display_name(params[:display_name])
 
     else
       user = User.find_by_display_name(params[:display_name])
 
-      redirect_to root_path if !user || user.active?
+      redirect_to root_path if user.nil? || user.active?
     end
   end
 
   def confirm_resend
     end
   end
 
   def confirm_resend
-    if user = User.find_by_display_name(params[:display_name])
-      Notifier.signup_confirm(user, user.tokens.create).deliver_now
-      flash[:notice] = t "user.confirm_resend.success", :email => "your email" # user.email
-    else
+    user = User.find_by_display_name(params[:display_name])
+    token = UserToken.find_by_token(session[:token])
+
+    if user.nil? || token.nil? || token.user != user
       flash[:error] = t "user.confirm_resend.failure", :name => params[:display_name]
       flash[:error] = t "user.confirm_resend.failure", :name => params[:display_name]
+    else
+      Notifier.signup_confirm(user, user.tokens.create).deliver_now
+      flash[:notice] = t "user.confirm_resend.success", :email => user.email
     end
 
     redirect_to :action => "login"
     end
 
     redirect_to :action => "login"
@@ -631,6 +634,8 @@ class UserController < ApplicationController
   ##
   #
   def unconfirmed_login(user)
   ##
   #
   def unconfirmed_login(user)
+    session[:token] = user.tokens.create.token
+
     redirect_to :action => "confirm", :display_name => user.display_name
 
     session.delete(:remember_me)
     redirect_to :action => "confirm", :display_name => user.display_name
 
     session.delete(:remember_me)
index 03238ea28261324eb406ec5b48264d04abde9ea4..37b8a7f30efaf39987a87b2e4b5957e59ae6fa67 100644 (file)
@@ -474,6 +474,8 @@ class UserControllerTest < ActionController::TestCase
   end
 
   def test_confirm_resend_success
   end
 
   def test_confirm_resend_success
+    session[:token] = users(:inactive_user).tokens.create.token
+
     assert_difference "ActionMailer::Base.deliveries.size", 1 do
       get :confirm_resend, :display_name => users(:inactive_user).display_name
     end
     assert_difference "ActionMailer::Base.deliveries.size", 1 do
       get :confirm_resend, :display_name => users(:inactive_user).display_name
     end
@@ -489,7 +491,17 @@ class UserControllerTest < ActionController::TestCase
     ActionMailer::Base.deliveries.clear
   end
 
     ActionMailer::Base.deliveries.clear
   end
 
-  def test_confirm_resend_failure
+  def test_confirm_resend_no_token
+    assert_no_difference "ActionMailer::Base.deliveries.size" do
+      get :confirm_resend, :display_name => users(:inactive_user).display_name
+    end
+
+    assert_response :redirect
+    assert_redirected_to login_path
+    assert_match "User Inactive User not found.", flash[:error]
+  end
+
+  def test_confirm_resend_unknown_user
     assert_no_difference "ActionMailer::Base.deliveries.size" do
       get :confirm_resend, :display_name => "No Such User"
     end
     assert_no_difference "ActionMailer::Base.deliveries.size" do
       get :confirm_resend, :display_name => "No Such User"
     end