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.

This commit is contained in:
Jason Lee 2016-12-01 11:11:10 +08:00
parent e58c051632
commit 107339ea9e
7 changed files with 39 additions and 7 deletions

View File

@ -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 1.1.4
----- -----

View File

@ -5,5 +5,4 @@ gemspec
gem 'rake' gem 'rake'
gem 'rails' gem 'rails'
gem 'rspec' gem 'rspec'
gem 'mini_magick'
gem 'dalli' gem 'dalli'

View File

@ -1,7 +1,7 @@
PATH PATH
remote: . remote: .
specs: specs:
rucaptcha (1.1.4) rucaptcha (1.2.0)
railties (>= 3.2) railties (>= 3.2)
GEM GEM
@ -62,7 +62,6 @@ GEM
mime-types (3.1) mime-types (3.1)
mime-types-data (~> 3.2015) mime-types-data (~> 3.2015)
mime-types-data (3.2016.0521) mime-types-data (3.2016.0521)
mini_magick (4.3.6)
mini_portile2 (2.1.0) mini_portile2 (2.1.0)
minitest (5.9.1) minitest (5.9.1)
nio4r (1.2.1) nio4r (1.2.1)
@ -134,7 +133,6 @@ PLATFORMS
DEPENDENCIES DEPENDENCIES
dalli dalli
mini_magick
rails rails
rake rake
rspec rspec

View File

@ -99,6 +99,8 @@ class AccountController < ApplicationController
end 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` View `app/views/account/new.html.erb`
```erb ```erb

View File

@ -6,10 +6,12 @@ module RuCaptcha
helper_method :verify_rucaptcha? helper_method :verify_rucaptcha?
end end
# session key of rucaptcha
def rucaptcha_sesion_key_key def rucaptcha_sesion_key_key
['rucaptcha-session', session.id].join(':') ['rucaptcha-session', session.id].join(':')
end end
# Generate a new Captcha
def generate_rucaptcha def generate_rucaptcha
code = RuCaptcha::Captcha.random_chars code = RuCaptcha::Captcha.random_chars
session_val = { session_val = {
@ -20,10 +22,24 @@ module RuCaptcha
RuCaptcha::Captcha.create(code) RuCaptcha::Captcha.create(code)
end 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) store_info = RuCaptcha.cache.read(rucaptcha_sesion_key_key)
# make sure move used 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 # Make sure session exist
if store_info.blank? if store_info.blank?

View File

@ -1,3 +1,3 @@
module RuCaptcha module RuCaptcha
VERSION = '1.1.4' VERSION = '1.2.0'
end end

View File

@ -74,6 +74,18 @@ describe RuCaptcha do
simple.params[:_rucaptcha] = 'AbcD' simple.params[:_rucaptcha] = 'AbcD'
expect(simple.verify_rucaptcha?).to eq(true) expect(simple.verify_rucaptcha?).to eq(true)
end 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 end
describe 'Incorrect chars' do describe 'Incorrect chars' do