lato 0.1.59 → 0.1.60
Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7c9eb96e4d8465b539ddee7df568a3b9c37bdfb55e9b8b24ad3c5969a53c0e32
|
4
|
+
data.tar.gz: b31a028a5f3dbdf18ba8f846bb0636ee77e967a1b96ebd858fef93cd882ec0d1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d150983fdebfba187569b988f957ca111030bc5617a64338cb90f13a9a4c635ab6fd10b88ada8f396615adb7d13cbf1bcef38decd9453962eb39bb16524864fb
|
7
|
+
data.tar.gz: 8ddff51d4154bf41ee332bb6b8cd8eefcb7ad2c3d70040b2eebaeedd97fe4bdd602188750b36e6febaed02c8d225d385b455e62e21e8ffdd2ab5836d2f312950
|
@@ -30,6 +30,22 @@ module Lato
|
|
30
30
|
false
|
31
31
|
end
|
32
32
|
|
33
|
+
def limit_requests(limit = 10, time_window = 10.minutes)
|
34
|
+
cache_key = "Lato::Sessionable/limit_requests/#{controller_name}/#{action_name}/#{request.remote_ip}"
|
35
|
+
attempts = Rails.cache.read(cache_key) || 0
|
36
|
+
|
37
|
+
attempts += 1
|
38
|
+
Rails.cache.write(cache_key, attempts, expires_in: time_window)
|
39
|
+
return true unless attempts >= limit
|
40
|
+
|
41
|
+
respond_to do |format|
|
42
|
+
format.html { render plain: "Too many requests, please wait #{time_window.to_i / 60} minutes to retry.", status: :too_many_requests }
|
43
|
+
format.json { render json: {}, status: :too_many_requests }
|
44
|
+
end
|
45
|
+
|
46
|
+
false
|
47
|
+
end
|
48
|
+
|
33
49
|
def session_create(user_id)
|
34
50
|
cookies.encrypted[:lato_session] = { value: Lato::Session.generate_session_per_user(user_id), expires: Lato.config.session_lifetime.from_now }
|
35
51
|
@session = Lato::Session.new(cookies.encrypted[:lato_session])
|
@@ -35,22 +35,6 @@ module Lato
|
|
35
35
|
I18n.locale = @session.user.locale || I18n.default_locale
|
36
36
|
end
|
37
37
|
|
38
|
-
# This method limit the number of requests for a specific action.
|
39
|
-
# Usage: before_action :limit_requests, only: %i[:action_name]
|
40
|
-
def limit_requests(limit = 10, time_window = 10.minutes)
|
41
|
-
cache_key = "Lato::ApplicationController.limit_requests/#{controller_name}/#{action_name}/#{request.remote_ip}"
|
42
|
-
attempts = Rails.cache.read(cache_key) || 0
|
43
|
-
|
44
|
-
attempts += 1
|
45
|
-
Rails.cache.write(cache_key, attempts, expires_in: time_window)
|
46
|
-
return unless attempts >= limit
|
47
|
-
|
48
|
-
respond_to do |format|
|
49
|
-
format.html { render plain: "Too many requests, please wait #{time_window.to_i / 60} minutes to retry.", status: :too_many_requests }
|
50
|
-
format.json { render json: {}, status: :too_many_requests }
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
38
|
def respond_to_with_not_found
|
55
39
|
respond_to do |format|
|
56
40
|
format.html { render plain: '', status: :not_found }
|
data/lib/lato/version.rb
CHANGED