]> git.openstreetmap.org Git - rails.git/commitdiff
Handle expired confirmation tokens
authorJohn Firebaugh <john.firebaugh@gmail.com>
Thu, 8 Aug 2013 21:57:39 +0000 (14:57 -0700)
committerJohn Firebaugh <john.firebaugh@gmail.com>
Mon, 12 Aug 2013 20:34:45 +0000 (13:34 -0700)
app/controllers/user_controller.rb
app/models/user_token.rb
config/locales/en.yml
test/functional/user_controller_test.rb

index fdef4ea04de9da617499567b76330001d0c1b3ff..d89d483f2a72ab5f490a469307cff9daf33accaf 100644 (file)
@@ -304,10 +304,14 @@ class UserController < ApplicationController
   end
 
   def confirm
   end
 
   def confirm
-    if request.post? && (token = UserToken.find_by_token(params[:confirm_string]))
-      if token.user.active?
+    if request.post?
+      token = UserToken.find_by_token(params[:confirm_string])
+      if token && token.user.active?
         flash[:error] = t('user.confirm.already active')
         redirect_to :action => 'login'
         flash[:error] = t('user.confirm.already active')
         redirect_to :action => 'login'
+      elsif !token || token.expired?
+        flash[:error] = t('user.confirm.unknown token')
+        redirect_to :action => 'confirm'
       else
         user = token.user
         user.status = "active"
       else
         user = token.user
         user.status = "active"
index 9a754d3442c5deeec13b6b35be7e6e59013dc772..3060b33ea17830012d0b65d864af56aa4dcbc989 100644 (file)
@@ -5,6 +5,10 @@ class UserToken < ActiveRecord::Base
 
   after_initialize :set_defaults
 
 
   after_initialize :set_defaults
 
+  def expired?
+    expiry < Time.now
+  end
+
 private
 
   def set_defaults
 private
 
   def set_defaults
index f3f4ac8dcf24085246eef5baff772f9f3ab7a4f4..9709b87781ccd27c05ff775237d5db3bc3dffc0f 100644 (file)
@@ -1880,7 +1880,7 @@ en:
       press confirm button: "Press the confirm button below to activate your account."
       button: Confirm
       already active: "This account has already been confirmed."
       press confirm button: "Press the confirm button below to activate your account."
       button: Confirm
       already active: "This account has already been confirmed."
-      unknown token: "That token doesn't seem to exist."
+      unknown token: "That confirmation code has expired or does not exist."
       reconfirm_html: "If you need us to resend the confirmation email, <a href=\"%{reconfirm}\">click here</a>."
     confirm_resend:
       success: "We've sent a new confirmation note to %{email} and as soon as you confirm your account you'll be able to get mapping.<br /><br />If you use an antispam system which sends confirmation requests then please make sure you whitelist webmaster@openstreetmap.org as we are unable to reply to any confirmation requests."
       reconfirm_html: "If you need us to resend the confirmation email, <a href=\"%{reconfirm}\">click here</a>."
     confirm_resend:
       success: "We've sent a new confirmation note to %{email} and as soon as you confirm your account you'll be able to get mapping.<br /><br />If you use an antispam system which sends confirmation requests then please make sure you whitelist webmaster@openstreetmap.org as we are unable to reply to any confirmation requests."
index 8b223882940bef2d30df1b55ca413a37ee64f1cd..75fd34f0f1457851eb8264bbf73a3e36de933103 100644 (file)
@@ -319,6 +319,30 @@ class UserControllerTest < ActionController::TestCase
     assert_select "form > fieldset > div.form-row > div.field_with_errors > input#user_display_name"
   end
 
     assert_select "form > fieldset > div.form-row > div.field_with_errors > input#user_display_name"
   end
 
+  def test_user_confirm_expired_token
+    user = users(:inactive_user)
+    token = user.tokens.new
+    token.expiry = 1.day.ago
+    token.save!
+
+    @request.cookies["_osm_session"] = user.display_name
+    post :confirm, :confirm_string => token.token
+
+    assert_redirected_to :action => 'confirm'
+    assert_match /expired/, flash[:error]
+  end
+
+  def test_user_already_confirmed
+    user = users(:normal_user)
+    token = user.tokens.create
+
+    @request.cookies["_osm_session"] = user.display_name
+    post :confirm, :confirm_string => token.token
+
+    assert_redirected_to :action => 'login'
+    assert_match /confirmed/, flash[:error]
+  end
+
   def test_user_terms_new_user
     get :terms, {}, { "new_user" => User.new }
     assert_response :success
   def test_user_terms_new_user
     get :terms, {}, { "new_user" => User.new }
     assert_response :success