From 732234503921c96a1bc91f439deef312fe644aee Mon Sep 17 00:00:00 2001
From: Neil Lalonde <neillalonde@gmail.com>
Date: Wed, 26 Feb 2014 17:53:44 -0500
Subject: [PATCH] FIX: when shown 403 error page and logging in, it will take
 you to the same page

---
 app/assets/javascripts/discourse/models/post_stream.js      | 6 +++++-
 .../javascripts/discourse/templates/topic.js.handlebars     | 4 +++-
 app/controllers/application_controller.rb                   | 6 +++---
 config/locales/client.en.yml                                | 1 +
 4 files changed, 12 insertions(+), 5 deletions(-)

diff --git a/app/assets/javascripts/discourse/models/post_stream.js b/app/assets/javascripts/discourse/models/post_stream.js
index d2182a371..c66f1a82d 100644
--- a/app/assets/javascripts/discourse/models/post_stream.js
+++ b/app/assets/javascripts/discourse/models/post_stream.js
@@ -726,7 +726,11 @@ Discourse.PostStream = Em.Object.extend({
     // If the result is 403 it means invalid access
     if (status === 403) {
       topic.set('errorTitle', I18n.t('topic.invalid_access.title'));
-      topic.set('message', I18n.t('topic.invalid_access.description'));
+      if (Discourse.User.current()) {
+        topic.set('message', I18n.t('topic.invalid_access.description'));
+      } else {
+        topic.set('message', I18n.t('topic.invalid_access.login_required'));
+      }
       return;
     }
 
diff --git a/app/assets/javascripts/discourse/templates/topic.js.handlebars b/app/assets/javascripts/discourse/templates/topic.js.handlebars
index fbf19bce0..5077e3376 100644
--- a/app/assets/javascripts/discourse/templates/topic.js.handlebars
+++ b/app/assets/javascripts/discourse/templates/topic.js.handlebars
@@ -123,9 +123,11 @@
       {{#if message}}
         <div class="message">
           <h2>{{message}}</h2>
+          {{#unless currentUser}}
+            <button {{action showLogin}} class='btn btn-primary btn-small'>{{i18n log_in}}</button>
+          {{/unless}}
         </div>
       {{/if}}
-
     </div>
   {{else}}
     <div class='container'>
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb
index 5fc1f2ead..09b16e99a 100644
--- a/app/controllers/application_controller.rb
+++ b/app/controllers/application_controller.rb
@@ -94,21 +94,21 @@ class ApplicationController < ActionController::Base
   end
 
   rescue_from Discourse::InvalidAccess do
-    rescue_discourse_actions("[error: 'invalid access']", 403) # TODO: this breaks json responses
+    rescue_discourse_actions("[error: 'invalid access']", 403, true) # TODO: this breaks json responses
   end
 
   rescue_from Discourse::ReadOnly do
     render status: 405, json: failed_json.merge(message: I18n.t("read_only_mode_enabled"))
   end
 
-  def rescue_discourse_actions(message, error)
+  def rescue_discourse_actions(message, error, include_ember=false)
     if request.format && request.format.json?
       # TODO: this doesn't make sense. Stuffing an html page into a json response will cause
       #       $.parseJSON to fail in the browser. Also returning text like "[error: 'invalid access']"
       #       from the above rescue_from blocks will fail because that isn't valid json.
       render status: error, layout: false, text: (error == 404) ? build_not_found_page(error) : message
     else
-      render text: build_not_found_page(error, 'no_js')
+      render text: build_not_found_page(error, include_ember ? 'application' : 'no_js')
     end
   end
 
diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml
index eaf236b88..14dc7e130 100644
--- a/config/locales/client.en.yml
+++ b/config/locales/client.en.yml
@@ -679,6 +679,7 @@ en:
       invalid_access:
         title: "Topic is private"
         description: "Sorry, you don't have access to that topic!"
+        login_required: "You need to log in to see that topic."
       server_error:
         title: "Topic failed to load"
         description: "Sorry, we couldn't load that topic, possibly due to a connection problem. Please try again. If the problem persists, let us know."