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:
parent
e58c051632
commit
107339ea9e
|
@ -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
Gemfile
1
Gemfile
|
@ -5,5 +5,4 @@ gemspec
|
|||
gem 'rake'
|
||||
gem 'rails'
|
||||
gem 'rspec'
|
||||
gem 'mini_magick'
|
||||
gem 'dalli'
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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?
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
module RuCaptcha
|
||||
VERSION = '1.1.4'
|
||||
VERSION = '1.2.0'
|
||||
end
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue