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)
28 @errors << message if message
29 render_error status: 422
32 def render_error(opts)
34 # json must come before html here, so it gets used as the
35 # default format when js is requested by the client. This lets
36 # ajax:error callback parse the response correctly, even though
38 f.json { render opts.merge(json: {success: false, errors: @errors}) }
39 f.html { render opts.merge(controller: 'application', action: 'error') }
43 def render_exception(e)
44 logger.error e.inspect
45 logger.error e.backtrace.collect { |x| x + "\n" }.join('') if e.backtrace
46 if @object.andand.errors.andand.full_messages.andand.any?
47 @errors = @object.errors.full_messages
51 self.render_error status: 422
54 def render_not_found(e=ActionController::RoutingError.new("Path not found"))
55 logger.error e.inspect
56 @errors = ["Path not found"]
57 self.render_error status: 404
62 limit = params[:limit].to_i
68 offset = params[:offset].to_i
73 @objects ||= model_class.limit(limit).offset(offset).all
75 f.json { render json: @objects }
83 return render_not_found("object not found")
86 f.json { render json: @object }
88 if request.method == 'GET'
91 redirect_to params[:return_to] || @object
100 return render_not_found("object not found")
105 @object = model_class.new
109 updates = params[@object.class.to_s.underscore.singularize.to_sym]
110 updates.keys.each do |attr|
111 if @object.send(attr).is_a? Hash and updates[attr].is_a? String
112 updates[attr] = Oj.load updates[attr]
115 if @object.update_attributes updates
118 self.render_error status: 422
123 @object ||= model_class.new params[model_class.to_s.underscore.singularize]
127 f.json { render json: @object }
129 redirect_to(params[:return_to] || @object)
138 f.json { render json: @object }
140 redirect_to(params[:return_to] || :back)
145 self.render_error status: 422
150 if Thread.current[:arvados_api_token]
151 Thread.current[:user] ||= User.current
153 logger.error "No API token in Thread"
159 controller_name.classify.constantize
162 def breadcrumb_page_name
163 (@breadcrumb_page_name ||
164 (@object.friendly_link_name if @object.respond_to? :friendly_link_name))
172 %w(Attributes Metadata JSON API)
177 def find_object_by_uuid
178 if params[:id] and params[:id].match /\D/
179 params[:uuid] = params.delete :id
181 if params[:uuid].is_a? String
182 @object = model_class.find(params[:uuid])
184 @object = model_class.where(uuid: params[:uuid]).first
189 Thread.current[:arvados_api_token] = nil
190 Thread.current[:user] = nil
191 Rails.cache.delete_matched(/^request_#{Thread.current.object_id}_/)
193 Rails.cache.delete_matched(/^request_#{Thread.current.object_id}_/)
196 def thread_with_api_token(login_optional = false)
198 try_redirect_to_login = true
199 if params[:api_token]
200 try_redirect_to_login = false
201 Thread.current[:arvados_api_token] = params[:api_token]
202 # Before copying the token into session[], do a simple API
203 # call to verify its authenticity.
205 session[:arvados_api_token] = params[:api_token]
206 if !request.format.json? and request.method == 'GET'
207 # Repeat this request with api_token in the (new) session
208 # cookie instead of the query string. This prevents API
209 # tokens from appearing in (and being inadvisedly copied
210 # and pasted from) browser Location bars.
211 redirect_to request.fullpath.sub(%r{([&\?]api_token=)[^&\?]*}, '')
216 @errors = ['Invalid API token']
217 self.render_error status: 401
219 elsif session[:arvados_api_token]
220 # In this case, the token must have already verified at some
221 # point, but it might have been revoked since. We'll try
222 # using it, and catch the exception if it doesn't work.
223 try_redirect_to_login = false
224 Thread.current[:arvados_api_token] = session[:arvados_api_token]
227 rescue ArvadosApiClient::NotLoggedInException
228 try_redirect_to_login = true
231 logger.debug "No token received, session is #{session.inspect}"
233 if try_redirect_to_login
234 unless login_optional
237 if request.method == 'GET'
238 redirect_to $arvados_api_client.arvados_login_url(return_to: request.url)
240 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."
245 @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.']
246 self.render_error status: 422
250 # login is optional for this route so go on to the regular controller
251 Thread.current[:arvados_api_token] = nil
256 # Remove token in case this Thread is used for anything else.
257 Thread.current[:arvados_api_token] = nil
261 def thread_with_mandatory_api_token
262 thread_with_api_token do
267 # This runs after thread_with_mandatory_api_token in the filter chain.
268 def thread_with_optional_api_token
269 if Thread.current[:arvados_api_token]
270 # We are already inside thread_with_mandatory_api_token.
273 # We skipped thread_with_mandatory_api_token. Use the optional version.
274 thread_with_api_token(true) do
282 Link.where(uuid: 'just-verifying-my-api-token')
284 rescue ArvadosApiClient::NotLoggedInException
289 def ensure_current_user_is_admin
290 unless current_user and current_user.is_admin
291 @errors = ['Permission denied']
292 self.render_error status: 401
296 def check_user_agreements
297 if current_user && !current_user.is_active && current_user.is_invited
298 signatures = UserAgreement.signatures
299 @signed_ua_uuids = UserAgreement.signatures.map &:head_uuid
300 @required_user_agreements = UserAgreement.all.map do |ua|
301 if not @signed_ua_uuids.index ua.uuid
302 Collection.find(ua.uuid)
305 if @required_user_agreements.empty?
306 # No agreements to sign. Perhaps we just need to ask?
307 current_user.activate
308 if !current_user.is_active
309 logger.warn "#{current_user.uuid.inspect}: " +
310 "No user agreements to sign, but activate failed!"
313 if !current_user.is_active
314 render 'user_agreements/index'
321 return Rails.configuration.arvados_theme
324 @@notification_tests = []
326 @@notification_tests.push lambda { |controller, current_user|
327 AuthorizedKey.limit(1).where(authorized_user_uuid: current_user.uuid).each do
330 return lambda { |view|
331 view.render partial: 'notifications/ssh_key_notification'
335 #@@notification_tests.push lambda { |controller, current_user|
336 # Job.limit(1).where(created_by: current_user.uuid).each do
339 # return lambda { |view|
340 # view.render partial: 'notifications/jobs_notification'
344 @@notification_tests.push lambda { |controller, current_user|
345 Collection.limit(1).where(created_by: current_user.uuid).each do
348 return lambda { |view|
349 view.render partial: 'notifications/collections_notification'
353 @@notification_tests.push lambda { |controller, current_user|
354 PipelineInstance.limit(1).where(created_by: current_user.uuid).each do
357 return lambda { |view|
358 view.render partial: 'notifications/pipelines_notification'
362 def check_user_notifications
363 @notification_count = 0
367 @showallalerts = false
368 @@notification_tests.each do |t|
369 a = t.call(self, current_user)
371 @notification_count += 1
372 @notifications.push a
377 if @notification_count == 0
378 @notification_count = ''