1 class ApplicationController < ActionController::Base
3 around_filter :thread_with_api_token, :except => [:render_exception, :render_not_found]
4 before_filter :find_object_by_uuid, :except => [:index, :render_exception, :render_not_found]
6 unless Rails.application.config.consider_all_requests_local
8 :with => :render_exception
9 rescue_from ActiveRecord::RecordNotFound,
10 :with => :render_not_found
11 rescue_from ActionController::RoutingError,
12 :with => :render_not_found
13 rescue_from ActionController::UnknownController,
14 :with => :render_not_found
15 rescue_from ActionController::UnknownAction,
16 :with => :render_not_found
19 def unprocessable(message=nil)
21 @errors << message if message
22 render_error status: 422
25 def render_error(opts)
27 f.html { render opts.merge(controller: 'application', action: 'error') }
28 f.json { render opts.merge(json: {success: false, errors: @errors}) }
32 def render_exception(e)
33 logger.error e.inspect
34 logger.error e.backtrace.collect { |x| x + "\n" }.join('') if e.backtrace
35 if @object and @object.errors and @object.errors.full_messages
36 @errors = @object.errors.full_messages
40 self.render_error status: 422
43 def render_not_found(e=ActionController::RoutingError.new("Path not found"))
44 logger.error e.inspect
45 @errors = ["Path not found"]
46 self.render_error status: 404
51 @objects ||= model_class.all
53 f.json { render json: @objects }
60 return render_not_found("object not found")
63 f.json { render json: @object }
69 if Thread.current[:arvados_api_token]
70 @current_user ||= User.current
72 logger.error "No API token in Thread"
80 controller_name.classify.constantize
83 def find_object_by_uuid
84 if params[:id] and params[:id].match /\D/
85 params[:uuid] = params.delete :id
87 @object = model_class.where(uuid: params[:uuid]).first
90 def thread_with_api_token
92 try_redirect_to_login = true
94 try_redirect_to_login = false
95 Thread.current[:arvados_api_token] = params[:api_token]
96 # Before copying the token into session[], do a simple API
97 # call to verify its authenticity.
99 session[:arvados_api_token] = params[:api_token]
100 if !request.format.json? and request.method == 'GET'
101 # Repeat this request with api_token in the (new) session
102 # cookie instead of the query string. This prevents API
103 # tokens from appearing in (and being inadvisedly copied
104 # and pasted from) browser Location bars.
105 redirect_to request.fullpath.sub(%r{([&\?]api_token=)[^&\?]*}, '')
110 @errors = ['Invalid API token']
111 self.render_error status: 401
113 elsif session[:arvados_api_token]
114 # In this case, the token must have already verified at some
115 # point, but it might have been revoked since. We'll try
116 # using it, and catch the exception if it doesn't work.
117 try_redirect_to_login = false
118 Thread.current[:arvados_api_token] = session[:arvados_api_token]
121 rescue ArvadosApiClient::NotLoggedInException
122 try_redirect_to_login = true
125 if try_redirect_to_login
128 redirect_to $arvados_api_client.arvados_login_url(return_to: request.url)
131 @errors = ['No API token supplied -- can\'t really do anything.']
132 self.render_error status: 422
137 # Remove token in case this Thread is used for anything else.
138 Thread.current[:arvados_api_token] = nil
144 Link.where(uuid: 'just-verifying-my-api-token')
146 rescue ArvadosApiClient::NotLoggedInException
151 def ensure_current_user_is_admin
152 unless current_user and current_user.is_admin
153 @errors = ['Permission denied']
154 self.render_error status: 401