Module: SubmitOnce::ControllerHelper
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/submit_once/controller_helper.rb
Instance Method Summary collapse
-
#check_form_token ⇒ Object
TODO: force or no.
- #check_form_token! ⇒ Object
- #clean_expired_token ⇒ Object
- #gen_form_token ⇒ Object
Instance Method Details
#check_form_token ⇒ Object
TODO: force or no
14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/submit_once/controller_helper.rb', line 14 def check_form_token # TODO: 顺便做超时 token 清理工作 clean_expired_token return false if params[TOKEN_KEY].blank? form_token_key = params[TOKEN_KEY] if session[form_token_key] == params[TOKEN_VALUE] session.delete(form_token_key) true else false end end |
#check_form_token! ⇒ Object
29 30 31 32 33 34 |
# File 'lib/submit_once/controller_helper.rb', line 29 def check_form_token! unless check_form_token # TODO: path, and i18n redirect_to '/', notice: "Couldn't repeat submit form" end end |
#clean_expired_token ⇒ Object
45 46 47 48 49 50 51 52 |
# File 'lib/submit_once/controller_helper.rb', line 45 def clean_expired_token session.each do |key, value| if key.start_with? TOKEN_KEY = Time.zone.at key.sub(TOKEN_KEY, '').to_i session.delete(key) if < 30.minutes.ago end end end |
#gen_form_token ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/submit_once/controller_helper.rb', line 36 def gen_form_token clean_expired_token @__form_token_key ||= "#{TOKEN_KEY}#{Time.now.to_i}" @__form_token ||= (session[@__form_token_key] = Digest::SHA1.hexdigest((Time.now.to_i + rand(0xffffff)).to_s)[0..39]) [@__form_token_key, @__form_token] end |