Module: RuCaptcha::ControllerHelpers
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/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
17 18 19 20 21 22 23 24 25 |
# File 'lib/rucaptcha/controller_helpers.rb', line 17 def generate_rucaptcha res = RuCaptcha.generate() session_val = { code: res[0], time: Time.now.to_i } RuCaptcha.cache.write(rucaptcha_sesion_key_key, session_val, expires_in: RuCaptcha.config.expires_in) res[1] end |
#rucaptcha_sesion_key_key ⇒ Object
session key of rucaptcha
10 11 12 13 14 |
# File 'lib/rucaptcha/controller_helpers.rb', line 10 def rucaptcha_sesion_key_key session_id = session.respond_to?(:id) ? session.id : session[:session_id] warning_when_session_invalid if session_id.blank? ['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. :captcha - if given, the value of it will be used to verify the captcha,
if do not give or blank, the value of params[:_rucaptcha] will be used to verify the captcha
exmaples:
verify_rucaptcha?
verify_rucaptcha?(user, keep_session: true)
verify_rucaptcha?(nil, keep_session: true)
verify_rucaptcha?(nil, captcha: params[:user][:captcha])
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 67 68 69 70 |
# File 'lib/rucaptcha/controller_helpers.rb', line 42 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) 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]) > RuCaptcha.config.expires_in return add_rucaptcha_validation_error end # Make sure parama have captcha captcha = (opts[: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 |