From f117e281589e2c81fdb7b8813788c47e19ec5c8d Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Mon, 2 Oct 2017 18:44:11 +0800 Subject: [PATCH] FIX: Redirect to login for anon user. https://meta.discourse.org/t/link-to-upgrade-shows-500-error/70993/7 --- .../docker_manager/admin_controller.rb | 40 +++++++++---------- .../docker_manager/application_controller.rb | 4 +- spec/requests/admin_controller_spec.rb | 33 +++++++++++++++ 3 files changed, 55 insertions(+), 22 deletions(-) create mode 100644 spec/requests/admin_controller_spec.rb diff --git a/app/controllers/docker_manager/admin_controller.rb b/app/controllers/docker_manager/admin_controller.rb index 6001d86..2a3f50f 100644 --- a/app/controllers/docker_manager/admin_controller.rb +++ b/app/controllers/docker_manager/admin_controller.rb @@ -20,27 +20,27 @@ module DockerManager if (version < expected_version) || (ruby_version < expected_ruby_version) - render text: < -

You are running an old version of the Discourse image.

-

-Upgrades via the web UI are disabled until you run the latest image. -

-

-To do so log in to your server using SSH and run: -

+ render text: <<~HTML + +

You are running an old version of the Discourse image.

+

+ Upgrades via the web UI are disabled until you run the latest image. +

+

+ To do so log in to your server using SSH and run: +

-
-cd /var/discourse
-git pull
-./launcher rebuild app
-
-

-More info on our support site -

- - -HTML +
+        cd /var/discourse
+        git pull
+        ./launcher rebuild app
+        
+

+ More info on our support site +

+ + + HTML else render end diff --git a/app/controllers/docker_manager/application_controller.rb b/app/controllers/docker_manager/application_controller.rb index 54c9fd0..98b39ad 100644 --- a/app/controllers/docker_manager/application_controller.rb +++ b/app/controllers/docker_manager/application_controller.rb @@ -1,6 +1,5 @@ module DockerManager class ApplicationController < ActionController::Base - helper DockerManager::ApplicationHelper include CurrentUser @@ -17,7 +16,8 @@ module DockerManager protected def ensure_admin - raise Discourse::InvalidAccess.new unless current_user && current_user.admin? + return redirect_to '/login' if !current_user + return render(plain: I18n.t('invalid_access'), status: 404) if !current_user.admin? end end diff --git a/spec/requests/admin_controller_spec.rb b/spec/requests/admin_controller_spec.rb new file mode 100644 index 0000000..1531746 --- /dev/null +++ b/spec/requests/admin_controller_spec.rb @@ -0,0 +1,33 @@ +require 'rails_helper' + +RSpec.describe DockerManager::AdminController do + describe 'anonymous user' do + it 'should redirect to login page' do + get '/admin/upgrade' + + expect(response.status).to eq(302) + expect(response).to redirect_to('/login') + end + end + + describe 'when user is not an admin' do + it 'should redirect to login page' do + sign_in(Fabricate(:user)) + + get '/admin/upgrade' + + expect(response.status).to eq(404) + expect(response.body).to eq(I18n.t('invalid_access')) + end + end + + describe 'when user is an admin' do + it 'should return the right response' do + sign_in(Fabricate(:admin)) + + get '/admin/upgrade' + + expect(response.status).to eq(200) + end + end +end