1 class ApplicationController < ActionController::Base
3 around_filter :thread_clear
4 around_filter :thread_with_api_token, :except => [:render_exception, :render_not_found]
5 before_filter :find_object_by_uuid, :except => [:index, :render_exception, :render_not_found]
9 :with => :render_exception
10 rescue_from ActiveRecord::RecordNotFound,
11 :with => :render_not_found
12 rescue_from ActionController::RoutingError,
13 :with => :render_not_found
14 rescue_from ActionController::UnknownController,
15 :with => :render_not_found
16 rescue_from ActionController::UnknownAction,
17 :with => :render_not_found
20 def unprocessable(message=nil)
22 @errors << message if message
23 render_error status: 422
26 def render_error(opts)
28 f.html { render opts.merge(controller: 'application', action: 'error') }
29 f.json { render opts.merge(json: {success: false, errors: @errors}) }
33 def render_exception(e)
34 logger.error e.inspect
35 logger.error e.backtrace.collect { |x| x + "\n" }.join('') if e.backtrace
36 if @object.andand.errors.andand.full_messages.andand.any?
37 @errors = @object.errors.full_messages
41 self.render_error status: 422
44 def render_not_found(e=ActionController::RoutingError.new("Path not found"))
45 logger.error e.inspect
46 @errors = ["Path not found"]
47 self.render_error status: 404
52 @objects ||= model_class.all
54 f.json { render json: @objects }
61 return render_not_found("object not found")
64 f.json { render json: @object }
71 return render_not_found("object not found")
76 @object = model_class.new
80 updates = params[@object.class.to_s.underscore.singularize.to_sym]
81 updates.keys.each do |attr|
82 if @object.send(attr).is_a? Hash and updates[attr].is_a? String
83 updates[attr] = Oj.load updates[attr]
86 if @object.update_attributes updates
89 self.render_error status: 422
94 @object ||= model_class.new params[model_class.to_s.singularize.to_sym]
101 redirect_to(params[:return_to] || :back)
103 self.render_error status: 422
108 if Thread.current[:arvados_api_token]
109 Thread.current[:user] ||= User.current
111 logger.error "No API token in Thread"
117 controller_name.classify.constantize
122 def find_object_by_uuid
123 if params[:id] and params[:id].match /\D/
124 params[:uuid] = params.delete :id
126 @object = model_class.where(uuid: params[:uuid]).first
130 Thread.current[:arvados_api_token] = nil
131 Thread.current[:user] = nil
135 def thread_with_api_token
137 try_redirect_to_login = true
138 if params[:api_token]
139 try_redirect_to_login = false
140 Thread.current[:arvados_api_token] = params[:api_token]
141 # Before copying the token into session[], do a simple API
142 # call to verify its authenticity.
144 session[:arvados_api_token] = params[:api_token]
145 if !request.format.json? and request.method == 'GET'
146 # Repeat this request with api_token in the (new) session
147 # cookie instead of the query string. This prevents API
148 # tokens from appearing in (and being inadvisedly copied
149 # and pasted from) browser Location bars.
150 redirect_to request.fullpath.sub(%r{([&\?]api_token=)[^&\?]*}, '')
155 @errors = ['Invalid API token']
156 self.render_error status: 401
158 elsif session[:arvados_api_token]
159 # In this case, the token must have already verified at some
160 # point, but it might have been revoked since. We'll try
161 # using it, and catch the exception if it doesn't work.
162 try_redirect_to_login = false
163 Thread.current[:arvados_api_token] = session[:arvados_api_token]
166 rescue ArvadosApiClient::NotLoggedInException
167 try_redirect_to_login = true
170 logger.debug "No token received, session is #{session.inspect}"
172 if try_redirect_to_login
175 if request.method == 'GET'
176 redirect_to $arvados_api_client.arvados_login_url(return_to: request.url)
178 flash[:error] = "Either you are not logged in, or your session has timed out. I can't automatically log you in and re-attempt this request."
183 @errors = ['You do not seem to be logged in. You did not supply an API token with this request, and your session (if any) has timed out.']
184 self.render_error status: 422
189 # Remove token in case this Thread is used for anything else.
190 Thread.current[:arvados_api_token] = nil
196 Link.where(uuid: 'just-verifying-my-api-token')
198 rescue ArvadosApiClient::NotLoggedInException
203 def ensure_current_user_is_admin
204 unless current_user and current_user.is_admin
205 @errors = ['Permission denied']
206 self.render_error status: 401