1 class ApplicationController < ActionController::Base
2 respond_to :html, :json, :js
4 around_filter :thread_clear
5 around_filter :thread_with_mandatory_api_token, :except => [:render_exception, :render_not_found]
6 around_filter :thread_with_optional_api_token
7 before_filter :find_object_by_uuid, :except => [:index, :render_exception, :render_not_found]
8 before_filter :check_user_agreements, :except => [:render_exception, :render_not_found]
9 before_filter :check_user_notifications, :except => [:render_exception, :render_not_found]
13 rescue_from Exception,
14 :with => :render_exception
15 rescue_from ActiveRecord::RecordNotFound,
16 :with => :render_not_found
17 rescue_from ActionController::RoutingError,
18 :with => :render_not_found
19 rescue_from ActionController::UnknownController,
20 :with => :render_not_found
21 rescue_from ::AbstractController::ActionNotFound,
22 :with => :render_not_found
25 def unprocessable(message=nil)
27 @errors << message if message
28 render_error status: 422
31 def render_error(opts)
33 # json must come before html here, so it gets used as the
34 # default format when js is requested by the client. This lets
35 # ajax:error callback parse the response correctly, even though
37 f.json { render opts.merge(json: {success: false, errors: @errors}) }
38 f.html { render opts.merge(controller: 'application', action: 'error') }
42 def render_exception(e)
43 logger.error e.inspect
44 logger.error e.backtrace.collect { |x| x + "\n" }.join('') if e.backtrace
45 if @object.andand.errors.andand.full_messages.andand.any?
46 @errors = @object.errors.full_messages
50 self.render_error status: 422
53 def render_not_found(e=ActionController::RoutingError.new("Path not found"))
54 logger.error e.inspect
55 @errors = ["Path not found"]
56 self.render_error status: 404
60 @objects ||= model_class.limit(200).all
62 f.json { render json: @objects }
70 return render_not_found("object not found")
73 f.json { render json: @object }
75 if request.method == 'GET'
78 redirect_to params[:return_to] || @object
87 return render_not_found("object not found")
92 @object = model_class.new
96 updates = params[@object.class.to_s.underscore.singularize.to_sym]
97 updates.keys.each do |attr|
98 if @object.send(attr).is_a? Hash and updates[attr].is_a? String
99 updates[attr] = Oj.load updates[attr]
102 if @object.update_attributes updates
105 self.render_error status: 422
110 @object ||= model_class.new params[model_class.to_s.underscore.singularize.to_sym]
115 redirect_to(params[:return_to] || @object)
125 redirect_to(params[:return_to] || :back)
130 self.render_error status: 422
135 if Thread.current[:arvados_api_token]
136 Thread.current[:user] ||= User.current
138 logger.error "No API token in Thread"
144 controller_name.classify.constantize
147 def breadcrumb_page_name
148 (@breadcrumb_page_name ||
149 (@object.friendly_link_name if @object.respond_to? :friendly_link_name))
157 %w(Attributes Metadata JSON API)
162 def find_object_by_uuid
163 if params[:id] and params[:id].match /\D/
164 params[:uuid] = params.delete :id
166 if params[:uuid].is_a? String
167 @object = model_class.find(params[:uuid])
169 @object = model_class.where(uuid: params[:uuid]).first
174 Thread.current[:arvados_api_token] = nil
175 Thread.current[:user] = nil
176 Rails.cache.delete_matched(/^request_#{Thread.current.object_id}_/)
178 Rails.cache.delete_matched(/^request_#{Thread.current.object_id}_/)
181 def thread_with_api_token(login_optional = false)
183 try_redirect_to_login = true
184 if params[:api_token]
185 try_redirect_to_login = false
186 Thread.current[:arvados_api_token] = params[:api_token]
187 # Before copying the token into session[], do a simple API
188 # call to verify its authenticity.
190 session[:arvados_api_token] = params[:api_token]
191 if !request.format.json? and request.method == 'GET'
192 # Repeat this request with api_token in the (new) session
193 # cookie instead of the query string. This prevents API
194 # tokens from appearing in (and being inadvisedly copied
195 # and pasted from) browser Location bars.
196 redirect_to request.fullpath.sub(%r{([&\?]api_token=)[^&\?]*}, '')
201 @errors = ['Invalid API token']
202 self.render_error status: 401
204 elsif session[:arvados_api_token]
205 # In this case, the token must have already verified at some
206 # point, but it might have been revoked since. We'll try
207 # using it, and catch the exception if it doesn't work.
208 try_redirect_to_login = false
209 Thread.current[:arvados_api_token] = session[:arvados_api_token]
212 rescue ArvadosApiClient::NotLoggedInException
213 try_redirect_to_login = true
216 logger.debug "No token received, session is #{session.inspect}"
218 if try_redirect_to_login
219 unless login_optional
222 if request.method == 'GET'
223 redirect_to $arvados_api_client.arvados_login_url(return_to: request.url)
225 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."
230 @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.']
231 self.render_error status: 422
235 # login is optional for this route so go on to the regular controller
236 Thread.current[:arvados_api_token] = nil
241 # Remove token in case this Thread is used for anything else.
242 Thread.current[:arvados_api_token] = nil
246 def thread_with_mandatory_api_token
247 thread_with_api_token do
252 # This runs after thread_with_mandatory_api_token in the filter chain.
253 def thread_with_optional_api_token
254 if Thread.current[:arvados_api_token]
255 # We are already inside thread_with_mandatory_api_token.
258 # We skipped thread_with_mandatory_api_token. Use the optional version.
259 thread_with_api_token(true) do
267 Link.where(uuid: 'just-verifying-my-api-token')
269 rescue ArvadosApiClient::NotLoggedInException
274 def ensure_current_user_is_admin
275 unless current_user and current_user.is_admin
276 @errors = ['Permission denied']
277 self.render_error status: 401
281 def check_user_agreements
282 if current_user && !current_user.is_active && current_user.is_invited
283 signatures = UserAgreement.signatures
284 @signed_ua_uuids = UserAgreement.signatures.map &:head_uuid
285 @required_user_agreements = UserAgreement.all.map do |ua|
286 if not @signed_ua_uuids.index ua.uuid
287 Collection.find(ua.uuid)
290 if @required_user_agreements.empty?
291 # No agreements to sign. Perhaps we just need to ask?
292 current_user.activate
293 if !current_user.is_active
294 logger.warn "#{current_user.uuid.inspect}: " +
295 "No user agreements to sign, but activate failed!"
298 if !current_user.is_active
299 render 'user_agreements/index'
306 return Rails.configuration.arvados_theme
309 @@notification_tests = []
311 @@notification_tests.push lambda { |controller, current_user|
312 AuthorizedKey.limit(1).where(authorized_user_uuid: current_user.uuid).each do
315 return lambda { |view|
316 view.render partial: 'notifications/ssh_key_notification'
320 @@notification_tests.push lambda { |controller, current_user|
321 Job.limit(1).where(created_by: current_user.uuid).each do
324 return lambda { |view|
325 view.render partial: 'notifications/jobs_notification'
329 @@notification_tests.push lambda { |controller, current_user|
330 Collection.limit(1).where(created_by: current_user.uuid).each do
333 return lambda { |view|
334 view.render partial: 'notifications/collections_notification'
338 @@notification_tests.push lambda { |controller, current_user|
339 PipelineInstance.limit(1).where(created_by: current_user.uuid).each do
342 return lambda { |view|
343 view.render partial: 'notifications/pipelines_notification'
347 def check_user_notifications
348 @notification_count = 0
352 @showallalerts = false
353 @@notification_tests.each do |t|
354 a = t.call(self, current_user)
356 @notification_count += 1
357 @notifications.push a
362 if @notification_count == 0
363 @notification_count = ''