Class: Gitlab::Auth::UniqueIpsLimiter
- Inherits:
-
Object
- Object
- Gitlab::Auth::UniqueIpsLimiter
- Defined in:
- lib/gitlab/auth/unique_ips_limiter.rb
Constant Summary collapse
- USER_UNIQUE_IPS_PREFIX =
'user_unique_ips'
Class Method Summary collapse
- .config ⇒ Object
- .limit_user!(user = nil) ⇒ Object
- .limit_user_id!(user_id) ⇒ Object
- .update_and_return_ips_count(user_id, ip) ⇒ Object
Class Method Details
.config ⇒ Object
24 25 26 |
# File 'lib/gitlab/auth/unique_ips_limiter.rb', line 24 def config Gitlab::CurrentSettings.current_application_settings end |
.limit_user!(user = nil) ⇒ Object
18 19 20 21 22 |
# File 'lib/gitlab/auth/unique_ips_limiter.rb', line 18 def limit_user!(user = nil) user ||= yield if block_given? limit_user_id!(user.id) unless user.nil? user end |
.limit_user_id!(user_id) ⇒ Object
9 10 11 12 13 14 15 16 |
# File 'lib/gitlab/auth/unique_ips_limiter.rb', line 9 def limit_user_id!(user_id) if config.unique_ips_limit_enabled ip = RequestContext.instance.client_ip unique_ips = update_and_return_ips_count(user_id, ip) raise TooManyIps.new(user_id, ip, unique_ips) if unique_ips > config.unique_ips_limit_per_user end end |
.update_and_return_ips_count(user_id, ip) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/gitlab/auth/unique_ips_limiter.rb', line 28 def update_and_return_ips_count(user_id, ip) time = Time.now.utc.to_i key = "#{USER_UNIQUE_IPS_PREFIX}:#{user_id}" Gitlab::Redis::SharedState.with do |redis| redis.multi do |r| r.zadd(key, time, ip.to_s) r.zremrangebyscore(key, 0, time - config.unique_ips_limit_time_window) r.zcard(key) end.last end end |