Module: ConciseRuCaptcha::ControllerHelpers
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/concise_rucaptcha/controller_helpers.rb
Instance Method Summary collapse
-
#generate_rucaptcha ⇒ Object
Generate a new Captcha.
-
#rucaptcha_sesion_key_key ⇒ Object
session key of rucaptcha.
-
#verify_rucaptcha?(resource = nil, opts = {}) ⇒ Boolean
Verify captcha code.
Instance Method Details
#generate_rucaptcha ⇒ Object
Generate a new Captcha
16 17 18 19 20 21 22 23 24 |
# File 'lib/concise_rucaptcha/controller_helpers.rb', line 16 def generate_rucaptcha res = ConciseRuCaptcha.generate() session_val = { code: res[0], time: Time.now.to_i } ConciseRuCaptcha.cache.write(rucaptcha_sesion_key_key, session_val, expires_in: ConciseRuCaptcha.config.expires_in) res[1] end |
#rucaptcha_sesion_key_key ⇒ Object
session key of rucaptcha
10 11 12 13 |
# File 'lib/concise_rucaptcha/controller_helpers.rb', line 10 def rucaptcha_sesion_key_key session_id = session.respond_to?(:id) ? session.id : session[:session_id] ['rucaptcha-session', session_id].join(':') end |
#verify_rucaptcha?(resource = nil, opts = {}) ⇒ Boolean
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)
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/concise_rucaptcha/controller_helpers.rb', line 38 def verify_rucaptcha?(resource = nil, opts = {}) opts ||= {} store_info = ConciseRuCaptcha.cache.read(rucaptcha_sesion_key_key) # make sure move used key ConciseRuCaptcha.cache.delete(rucaptcha_sesion_key_key) unless opts[:keep_session] # Make sure session exist if store_info.blank? return add_rucaptcha_validation_error end # Make sure not expire if (Time.now.to_i - store_info[:time]) > ConciseRuCaptcha.config.expires_in return add_rucaptcha_validation_error end # Make sure parama have captcha captcha = (params[:_rucaptcha] || '').downcase.strip if captcha.blank? return add_rucaptcha_validation_error end if captcha != store_info[:code] return add_rucaptcha_validation_error end true end |