From 107339ea9e1d76456736b9fcfdc3f30e0fa425db Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Thu, 1 Dec 2016 11:11:10 +0800 Subject: [PATCH] Add an `:keep_session` option for `verify_rucaptcha?` method to giva a way for let you keep session on verify, if true, RuCaptcha will not delete the captcha code session after validation. --- CHANGELOG.md | 5 +++++ Gemfile | 1 - Gemfile.lock | 4 +--- README.md | 2 ++ lib/rucaptcha/controller_helpers.rb | 20 ++++++++++++++++++-- lib/rucaptcha/version.rb | 2 +- spec/controller_helpers_spec.rb | 12 ++++++++++++ 7 files changed, 39 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 86e195a..3f24c48 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +1.2.0 +----- + +- Add an `:keep_session` option for `verify_rucaptcha?` method to giva a way for let you keep session on verify, if true, RuCaptcha will not delete the captcha code session after validation. + 1.1.4 ----- diff --git a/Gemfile b/Gemfile index b30ae89..36df038 100644 --- a/Gemfile +++ b/Gemfile @@ -5,5 +5,4 @@ gemspec gem 'rake' gem 'rails' gem 'rspec' -gem 'mini_magick' gem 'dalli' \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock index 60c3c44..cd11400 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - rucaptcha (1.1.4) + rucaptcha (1.2.0) railties (>= 3.2) GEM @@ -62,7 +62,6 @@ GEM mime-types (3.1) mime-types-data (~> 3.2015) mime-types-data (3.2016.0521) - mini_magick (4.3.6) mini_portile2 (2.1.0) minitest (5.9.1) nio4r (1.2.1) @@ -134,7 +133,6 @@ PLATFORMS DEPENDENCIES dalli - mini_magick rails rake rspec diff --git a/README.md b/README.md index f9d7a63..4dae17e 100644 --- a/README.md +++ b/README.md @@ -99,6 +99,8 @@ class AccountController < ApplicationController end ``` +> TIP: Sometime you may need keep last verified captcha code in session on `verify_rucaptcha?` method call, you can use `keep_session: true`. For example: ` Verify_rucaptcha? (@user, keep_session: true) `. + View `app/views/account/new.html.erb` ```erb diff --git a/lib/rucaptcha/controller_helpers.rb b/lib/rucaptcha/controller_helpers.rb index 8b00eb1..92cf1be 100644 --- a/lib/rucaptcha/controller_helpers.rb +++ b/lib/rucaptcha/controller_helpers.rb @@ -6,10 +6,12 @@ module RuCaptcha helper_method :verify_rucaptcha? end + # session key of rucaptcha def rucaptcha_sesion_key_key ['rucaptcha-session', session.id].join(':') end + # Generate a new Captcha def generate_rucaptcha code = RuCaptcha::Captcha.random_chars session_val = { @@ -20,10 +22,24 @@ module RuCaptcha RuCaptcha::Captcha.create(code) end - def verify_rucaptcha?(resource = nil) + # Verify captcha code + # + # params: + # resource - [optional] a ActiveModel object, if given will add validation error message to object. + # :keep_session - if true, RuCaptcha will not delete the captcha code session. + # + # exmaples: + # + # verify_rucaptcha? + # verify_rucaptcha?(user, keep_session: true) + # verify_rucaptcha?(nil, keep_session: true) + # + def verify_rucaptcha?(resource = nil, opts = {}) + opts ||= {} + store_info = RuCaptcha.cache.read(rucaptcha_sesion_key_key) # make sure move used key - RuCaptcha.cache.delete(rucaptcha_sesion_key_key) + RuCaptcha.cache.delete(rucaptcha_sesion_key_key) unless opts[:keep_session] # Make sure session exist if store_info.blank? diff --git a/lib/rucaptcha/version.rb b/lib/rucaptcha/version.rb index 2f270d2..c05046e 100644 --- a/lib/rucaptcha/version.rb +++ b/lib/rucaptcha/version.rb @@ -1,3 +1,3 @@ module RuCaptcha - VERSION = '1.1.4' + VERSION = '1.2.0' end diff --git a/spec/controller_helpers_spec.rb b/spec/controller_helpers_spec.rb index 0ec6ee8..fd43266 100644 --- a/spec/controller_helpers_spec.rb +++ b/spec/controller_helpers_spec.rb @@ -74,6 +74,18 @@ describe RuCaptcha do simple.params[:_rucaptcha] = 'AbcD' expect(simple.verify_rucaptcha?).to eq(true) end + + it 'should keep session when given :keep_session' do + RuCaptcha.cache.write(simple.rucaptcha_sesion_key_key, { + time: Time.now.to_i, + code: 'abcd' + }) + simple.params[:_rucaptcha] = 'abcd' + expect(simple.verify_rucaptcha?(nil, keep_session: true)).to eq(true) + expect(simple.custom_session).not_to eq nil + expect(simple.verify_rucaptcha?).to eq(true) + expect(simple.verify_rucaptcha?).to eq(false) + end end describe 'Incorrect chars' do