DEV: s/\$redis/Discourse\.redis (#8431)

This commit also adds a rubocop rule to prevent global variables.
This commit is contained in:
Joffrey JAFFEUX
2019-12-03 10:05:53 +01:00
committed by GitHub
parent 9eccfb7b52
commit 0d3d2c43a0
118 changed files with 378 additions and 362 deletions

View File

@@ -768,14 +768,14 @@ class ApplicationController < ActionController::Base
if !SiteSetting.login_required? || (current_user rescue false)
key = "page_not_found_topics"
if @topics_partial = $redis.get(key)
if @topics_partial = Discourse.redis.get(key)
@topics_partial = @topics_partial.html_safe
else
category_topic_ids = Category.pluck(:topic_id).compact
@top_viewed = TopicQuery.new(nil, except_topic_ids: category_topic_ids).list_top_for("monthly").topics.first(10)
@recent = Topic.includes(:category).where.not(id: category_topic_ids).recent(10)
@topics_partial = render_to_string partial: '/exceptions/not_found_topics', formats: [:html]
$redis.setex(key, 10.minutes, @topics_partial)
Discourse.redis.setex(key, 10.minutes, @topics_partial)
end
end

View File

@@ -9,7 +9,7 @@ class ForumsController < ActionController::Base
after_action :add_readonly_header
def status
if $shutdown
if $shutdown # rubocop:disable Style/GlobalVars
render plain: "shutting down", status: 500
else
render plain: "ok"

View File

@@ -409,15 +409,15 @@ class SessionController < ApplicationController
end
def one_time_password
@otp_username = otp_username = $redis.get "otp_#{params[:token]}"
@otp_username = otp_username = Discourse.redis.get "otp_#{params[:token]}"
if otp_username && user = User.find_by_username(otp_username)
if current_user&.username == otp_username
$redis.del "otp_#{params[:token]}"
Discourse.redis.del "otp_#{params[:token]}"
return redirect_to path("/")
elsif request.post?
log_on_user(user)
$redis.del "otp_#{params[:token]}"
Discourse.redis.del "otp_#{params[:token]}"
return redirect_to path("/")
else
# Display the form

View File

@@ -205,7 +205,7 @@ class UserApiKeysController < ApplicationController
raise Discourse::InvalidAccess unless UserApiKey.allowed_scopes.superset?(Set.new(["one_time_password"]))
otp = SecureRandom.hex
$redis.setex "otp_#{otp}", 10.minutes, username
Discourse.redis.setex "otp_#{otp}", 10.minutes, username
Base64.encode64(public_key.public_encrypt(otp))
end

View File

@@ -21,7 +21,7 @@ class Users::AssociateAccountsController < ApplicationController
# Presents a confirmation screen to the user. Accessed via GET, with no CSRF checks
def connect
auth = get_auth_hash
$redis.del "#{REDIS_PREFIX}_#{current_user&.id}_#{params[:token]}"
Discourse.redis.del "#{REDIS_PREFIX}_#{current_user&.id}_#{params[:token]}"
provider_name = auth.provider
authenticator = Discourse.enabled_authenticators.find { |a| a.name == provider_name }
@@ -37,7 +37,7 @@ class Users::AssociateAccountsController < ApplicationController
def get_auth_hash
token = params[:token]
json = $redis.get "#{REDIS_PREFIX}_#{current_user&.id}_#{token}"
json = Discourse.redis.get "#{REDIS_PREFIX}_#{current_user&.id}_#{token}"
raise Discourse::NotFound if json.nil?
OmniAuth::AuthHash.new(JSON.parse(json))

View File

@@ -31,7 +31,7 @@ class Users::OmniauthCallbacksController < ApplicationController
if session.delete(:auth_reconnect) && authenticator.can_connect_existing_user? && current_user
# Save to redis, with a secret token, then redirect to confirmation screen
token = SecureRandom.hex
$redis.setex "#{Users::AssociateAccountsController::REDIS_PREFIX}_#{current_user.id}_#{token}", 10.minutes, auth.to_json
Discourse.redis.setex "#{Users::AssociateAccountsController::REDIS_PREFIX}_#{current_user.id}_#{token}", 10.minutes, auth.to_json
return redirect_to Discourse.base_uri("/associate/#{token}")
else
@auth_result = authenticator.after_authenticate(auth)

View File

@@ -119,8 +119,8 @@ class WebhooksController < ActionController::Base
# prevent replay attacks
key = "mailgun_token_#{token}"
return false unless $redis.setnx(key, 1)
$redis.expire(key, 10.minutes)
return false unless Discourse.redis.setnx(key, 1)
Discourse.redis.expire(key, 10.minutes)
# ensure timestamp isn't too far from current time
return false if (Time.at(timestamp.to_i) - Time.now).abs > 12.hours.to_i