Rearrange thread_with_api_token around_filters so current_user is
authorTom Clegg <tom@curoverse.com>
Fri, 7 Feb 2014 00:39:48 +0000 (16:39 -0800)
committerTom Clegg <tom@curoverse.com>
Fri, 7 Feb 2014 00:39:48 +0000 (16:39 -0800)
available even when using thread_with_optional_api_token.

This makes notifications show up on users#home when accessed via root
url.

apps/workbench/app/controllers/application_controller.rb
apps/workbench/app/controllers/sessions_controller.rb
apps/workbench/app/controllers/users_controller.rb

index 02103c08bc9eefe661d8e5b2c0e26d3f8589b15c..100c5ea0c20daef798233e3cbfb099f96a6463e1 100644 (file)
@@ -2,7 +2,8 @@ class ApplicationController < ActionController::Base
   respond_to :html, :json, :js
   protect_from_forgery
   around_filter :thread_clear
-  around_filter :thread_with_api_token, :except => [:render_exception, :render_not_found]
+  around_filter :thread_with_mandatory_api_token, :except => [:render_exception, :render_not_found]
+  around_filter :thread_with_optional_api_token
   before_filter :find_object_by_uuid, :except => [:index, :render_exception, :render_not_found]
   before_filter :check_user_agreements, :except => [:render_exception, :render_not_found]
   before_filter :check_user_notifications, :except => [:render_exception, :render_not_found]
@@ -236,12 +237,25 @@ class ApplicationController < ActionController::Base
     end
   end
 
-  def thread_with_optional_api_token 
-    thread_with_api_token(true) do 
+  def thread_with_mandatory_api_token
+    thread_with_api_token do
       yield
     end
   end
 
+  # This runs after thread_with_mandatory_api_token in the filter chain.
+  def thread_with_optional_api_token
+    if Thread.current[:arvados_api_token]
+      # We are already inside thread_with_mandatory_api_token.
+      yield
+    else
+      # We skipped thread_with_mandatory_api_token. Use the optional version.
+      thread_with_api_token(true) do 
+        yield
+      end
+    end
+  end
+
   def verify_api_token
     begin
       Link.where(uuid: 'just-verifying-my-api-token')
@@ -298,7 +312,7 @@ class ApplicationController < ActionController::Base
   }
 
   @@notification_tests.push lambda { |controller, current_user|
-    AuthorizedKey.limit(1).where(authorized_user_uuid: current_user.uuid).each do   
+    AuthorizedKey.limit(1).where(authorized_user_uuid: current_user.uuid).each do
       return nil
     end
     return lambda { |view|
@@ -307,7 +321,7 @@ class ApplicationController < ActionController::Base
   }
 
   @@notification_tests.push lambda { |controller, current_user|
-    Job.limit(1).where(created_by: current_user.uuid).each do   
+    Job.limit(1).where(created_by: current_user.uuid).each do
       return nil
     end
     return lambda { |view|
@@ -316,7 +330,7 @@ class ApplicationController < ActionController::Base
   }
 
   @@notification_tests.push lambda { |controller, current_user|
-    Collection.limit(1).where(created_by: current_user.uuid).each do   
+    Collection.limit(1).where(created_by: current_user.uuid).each do
       return nil
     end
     return lambda { |view|
@@ -325,7 +339,7 @@ class ApplicationController < ActionController::Base
   }
 
   @@notification_tests.push lambda { |controller, current_user|
-    PipelineInstance.limit(1).where(created_by: current_user.uuid).each do   
+    PipelineInstance.limit(1).where(created_by: current_user.uuid).each do
       return nil
     end
     return lambda { |view|
index 7967111fff748717a0a47c8a2643c8722007587d..488c67c3c2e8b51cfb8990b9148d7b3b4d0a7f13 100644 (file)
@@ -1,5 +1,6 @@
 class SessionsController < ApplicationController
-  skip_around_filter :thread_with_api_token, :only => [:destroy, :index]
+  skip_around_filter :thread_with_mandatory_api_token, :only => [:destroy, :index]
+  skip_around_filter :thread_with_optional_api_token, :only => [:destroy, :index]
   skip_before_filter :find_object_by_uuid, :only => [:destroy, :index]
   def destroy
     session.clear
index f0aaae6ce32cdc1275b5a7f4f02959753aecbdde..3ccaa525cee853e43e9cd1f963419638152a53b0 100644 (file)
@@ -1,7 +1,6 @@
 class UsersController < ApplicationController
   skip_before_filter :find_object_by_uuid, :only => :welcome
-  skip_around_filter :thread_with_api_token, :only => :welcome
-  around_filter :thread_with_optional_api_token, :only => :welcome
+  skip_around_filter :thread_with_mandatory_api_token, :only => :welcome
 
   def welcome
     if current_user