2017-05-17 07:20:55 -04:00
|
|
|
module Gitlab
|
|
|
|
module PerformanceBar
|
2017-07-04 10:15:24 -04:00
|
|
|
ALLOWED_USER_IDS_KEY = 'performance_bar_allowed_user_ids'.freeze
|
|
|
|
# The time (in seconds) after which a set of allowed user IDs is expired
|
|
|
|
# automatically.
|
|
|
|
ALLOWED_USER_IDS_TIME_TO_LIVE = 10.minutes.to_i
|
|
|
|
|
2017-06-21 10:59:13 -04:00
|
|
|
def self.enabled?(current_user = nil)
|
|
|
|
Feature.enabled?(:gitlab_performance_bar, current_user)
|
|
|
|
end
|
|
|
|
|
2017-06-30 11:34:06 -04:00
|
|
|
def self.allowed_user?(user)
|
2017-07-04 10:15:24 -04:00
|
|
|
return false unless allowed_group_name
|
2017-06-21 10:59:13 -04:00
|
|
|
|
2017-07-04 10:15:24 -04:00
|
|
|
allowed_user_ids.include?(user.id)
|
2017-06-21 10:59:13 -04:00
|
|
|
end
|
|
|
|
|
2017-07-04 10:15:24 -04:00
|
|
|
def self.allowed_group_name
|
|
|
|
Gitlab.config.performance_bar.allowed_group
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.allowed_user_ids
|
|
|
|
Gitlab::Redis.with do |redis|
|
|
|
|
if redis.exists(cache_key)
|
|
|
|
redis.smembers(cache_key).map(&:to_i)
|
|
|
|
else
|
|
|
|
group = Group.find_by_full_path(allowed_group_name)
|
|
|
|
# Redis#sadd doesn't accept an empty array, but we still want to use
|
|
|
|
# Redis to let us know that no users are allowed, so we set the
|
|
|
|
# array to [-1] in this case.
|
|
|
|
user_ids =
|
|
|
|
if group
|
|
|
|
GroupMembersFinder.new(group).execute
|
|
|
|
.pluck(:user_id).presence || [-1]
|
|
|
|
else
|
|
|
|
[-1]
|
|
|
|
end
|
|
|
|
|
|
|
|
redis.multi do
|
|
|
|
redis.sadd(cache_key, user_ids)
|
|
|
|
redis.expire(cache_key, ALLOWED_USER_IDS_TIME_TO_LIVE)
|
|
|
|
end
|
2017-06-21 10:59:13 -04:00
|
|
|
|
2017-07-04 10:15:24 -04:00
|
|
|
user_ids
|
2017-06-28 13:18:16 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-04 10:15:24 -04:00
|
|
|
def self.cache_key
|
|
|
|
"#{ALLOWED_USER_IDS_KEY}:#{allowed_group_name}"
|
2017-05-17 07:20:55 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|