From be7d72feb02965caa1688a593975c608a02528fe Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Sun, 24 Apr 2016 20:20:01 +0800 Subject: [PATCH] Add error message to resource when captcha code expired. close #22 --- CHANGELOG.md | 5 +++++ Gemfile.lock | 2 +- lib/rucaptcha/controller_helpers.rb | 14 ++++++++------ lib/rucaptcha/version.rb | 2 +- 4 files changed, 15 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 641e05a..98c70b4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +0.4.1 +----- + +- Add error message to resource when captcha code expired. + 0.4.0 ----- diff --git a/Gemfile.lock b/Gemfile.lock index b0fb3cb..2d355e1 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - rucaptcha (0.4.0) + rucaptcha (0.4.1) posix-spawn (>= 0.3.0) GEM diff --git a/lib/rucaptcha/controller_helpers.rb b/lib/rucaptcha/controller_helpers.rb index daf530e..25ebd0c 100644 --- a/lib/rucaptcha/controller_helpers.rb +++ b/lib/rucaptcha/controller_helpers.rb @@ -15,17 +15,19 @@ module RuCaptcha def verify_rucaptcha?(resource = nil) rucaptcha_at = session[:_rucaptcha_at].to_i + captcha = params[:_rucaptcha].downcase.strip + # Captcha chars in Session expire in 2 minutes - if (Time.now.to_i - rucaptcha_at) > RuCaptcha.config.expires_in - return false + valid = false + if (Time.now.to_i - rucaptcha_at) <= RuCaptcha.config.expires_in + valid = captcha.present? && captcha == session[:_rucaptcha] end - right = params[:_rucaptcha].present? && session[:_rucaptcha].present? && - params[:_rucaptcha].downcase.strip == session[:_rucaptcha] if resource && resource.respond_to?(:errors) - resource.errors.add(:base, t('rucaptcha.invalid')) unless right + resource.errors.add(:base, t('rucaptcha.invalid')) unless valid end - right + + valid end end end diff --git a/lib/rucaptcha/version.rb b/lib/rucaptcha/version.rb index 66781cf..42531f1 100644 --- a/lib/rucaptcha/version.rb +++ b/lib/rucaptcha/version.rb @@ -1,3 +1,3 @@ module RuCaptcha - VERSION = '0.4.0' + VERSION = '0.4.1' end