1 class ApplicationController < ActionController::Base
2 include ArvadosApiClientHelper
3 include ApplicationHelper
5 respond_to :html, :json, :js
8 ERROR_ACTIONS = [:render_error, :render_not_found]
10 around_filter :thread_clear
11 around_filter :set_thread_api_token
12 # Methods that don't require login should
13 # skip_around_filter :require_thread_api_token
14 around_filter :require_thread_api_token, except: ERROR_ACTIONS
15 before_filter :set_cache_buster
16 before_filter :accept_uuid_as_id_param, except: ERROR_ACTIONS
17 before_filter :check_user_agreements, except: ERROR_ACTIONS
18 before_filter :check_user_profile, except: ERROR_ACTIONS
19 before_filter :check_user_notifications, except: ERROR_ACTIONS
20 before_filter :load_filters_and_paging_params, except: ERROR_ACTIONS
21 before_filter :find_object_by_uuid, except: [:create, :index, :choose] + ERROR_ACTIONS
25 rescue_from(ActiveRecord::RecordNotFound,
26 ActionController::RoutingError,
27 ActionController::UnknownController,
28 AbstractController::ActionNotFound,
29 with: :render_not_found)
30 rescue_from(Exception,
31 ActionController::UrlGenerationError,
32 with: :render_exception)
36 response.headers["Cache-Control"] = "no-cache, no-store, max-age=0, must-revalidate"
37 response.headers["Pragma"] = "no-cache"
38 response.headers["Expires"] = "Fri, 01 Jan 1990 00:00:00 GMT"
41 def unprocessable(message=nil)
44 @errors << message if message
45 render_error status: 422
48 def render_error(opts={})
51 # json must come before html here, so it gets used as the
52 # default format when js is requested by the client. This lets
53 # ajax:error callback parse the response correctly, even though
55 f.json { render opts.merge(json: {success: false, errors: @errors}) }
56 f.html { render({action: 'error'}.merge(opts)) }
60 def render_exception(e)
61 logger.error e.inspect
62 logger.error e.backtrace.collect { |x| x + "\n" }.join('') if e.backtrace
63 err_opts = {status: 422}
64 if e.is_a?(ArvadosApiClient::ApiError)
65 err_opts.merge!(action: 'api_error', locals: {api_error: e})
66 @errors = e.api_response[:errors]
67 elsif @object.andand.errors.andand.full_messages.andand.any?
68 @errors = @object.errors.full_messages
72 # Make user information available on the error page, falling back to the
73 # session cache if the API server is unavailable.
75 load_api_token(session[:arvados_api_token])
76 rescue ArvadosApiClient::ApiError
77 unless session[:user].nil?
79 Thread.current[:user] = User.new(session[:user])
80 rescue ArvadosApiClient::ApiError
81 # This can happen if User's columns are unavailable. Nothing to do.
85 # Preload projects trees for the template. If that's not doable, set empty
86 # trees so error page rendering can proceed. (It's easier to rescue the
87 # exception here than in a template.)
88 unless current_user.nil?
91 rescue ArvadosApiClient::ApiError
92 # Fall back to the default-setting code later.
95 @my_project_tree ||= []
96 @shared_project_tree ||= []
97 render_error(err_opts)
100 def render_not_found(e=ActionController::RoutingError.new("Path not found"))
101 logger.error e.inspect
102 @errors = ["Path not found"]
103 set_thread_api_token do
104 self.render_error(action: '404', status: 404)
108 def load_filters_and_paging_params
109 @order = params[:order] || 'created_at desc'
110 @order = [@order] unless @order.is_a? Array
114 @limit = params[:limit].to_i
119 @offset = params[:offset].to_i
124 filters = params[:filters]
125 if filters.is_a? String
126 filters = Oj.load filters
127 elsif filters.is_a? Array
128 filters = filters.collect do |filter|
129 if filter.is_a? String
130 # Accept filters[]=["foo","=","bar"]
133 # Accept filters=[["foo","=","bar"]]
138 # After this, params[:filters] can be trusted to be an array of arrays:
139 params[:filters] = filters
144 def find_objects_for_index
145 @objects ||= model_class
146 @objects = @objects.filter(@filters).limit(@limit).offset(@offset)
151 f.json { render json: @objects }
154 render_pane params[:tab_pane]
163 helper_method :render_pane
164 def render_pane tab_pane, opts={}
166 partial: 'show_' + tab_pane.downcase,
168 comparable: self.respond_to?(:compare),
171 }.merge(opts[:locals] || {})
174 render_to_string render_opts
181 find_objects_for_index if !@objects
185 helper_method :next_page_offset
186 def next_page_offset objects=nil
190 if objects.respond_to?(:result_offset) and
191 objects.respond_to?(:result_limit) and
192 objects.respond_to?(:items_available)
193 next_offset = objects.result_offset + objects.result_limit
194 if next_offset < objects.items_available
202 helper_method :next_page_href
203 def next_page_href with_params={}
205 url_for with_params.merge(offset: next_page_offset)
211 return render_not_found("object not found")
215 extra_attrs = { href: url_for(action: :show, id: @object) }
216 @object.textile_attributes.each do |textile_attr|
217 extra_attrs.merge!({ "#{textile_attr}Textile" => view_context.render_markup(@object.attributes[textile_attr]) })
219 render json: @object.attributes.merge(extra_attrs)
222 if params['tab_pane']
223 render_pane(if params['tab_pane'].is_a? Hash then params['tab_pane']["name"] else params['tab_pane'] end)
224 elsif request.method.in? ['GET', 'HEAD']
227 redirect_to params[:return_to] || @object
235 params[:limit] ||= 40
239 find_objects_for_index if !@objects
241 content: render_to_string(partial: "choose_rows.html",
243 next_page_href: next_page_href(partial: params[:partial])
248 find_objects_for_index if !@objects
249 render partial: 'choose', locals: {multiple: params[:multiple]}
256 return render_not_found("object not found")
261 @object = model_class.new
265 @updates ||= params[@object.resource_param_name.to_sym]
266 @updates.keys.each do |attr|
267 if @object.send(attr).is_a? Hash
268 if @updates[attr].is_a? String
269 @updates[attr] = Oj.load @updates[attr]
271 if params[:merge] || params["merge_#{attr}".to_sym]
272 # Merge provided Hash with current Hash, instead of
274 @updates[attr] = @object.send(attr).with_indifferent_access.
275 deep_merge(@updates[attr].with_indifferent_access)
279 if @object.update_attributes @updates
282 self.render_error status: 422
287 @new_resource_attrs ||= params[model_class.to_s.underscore.singularize]
288 @new_resource_attrs ||= {}
289 @new_resource_attrs.reject! { |k,v| k.to_s == 'uuid' }
290 @object ||= model_class.new @new_resource_attrs, params["options"]
293 f.json { render json: @object.attributes.merge(href: url_for(action: :show, id: @object)) }
300 self.render_error status: 422
304 # Clone the given object, merging any attribute values supplied as
305 # with a create action.
307 @new_resource_attrs ||= params[model_class.to_s.underscore.singularize]
308 @new_resource_attrs ||= {}
309 @object = @object.dup
310 @object.update_attributes @new_resource_attrs
311 if not @new_resource_attrs[:name] and @object.respond_to? :name
312 if @object.name and @object.name != ''
313 @object.name = "Copy of #{@object.name}"
325 f.json { render json: @object }
327 redirect_to(params[:return_to] || :back)
332 self.render_error status: 422
337 Thread.current[:user]
341 controller_name.classify.constantize
344 def breadcrumb_page_name
345 (@breadcrumb_page_name ||
346 (@object.friendly_link_name if @object.respond_to? :friendly_link_name) ||
355 %w(Attributes Advanced)
360 def strip_token_from_path(path)
361 path.sub(/([\?&;])api_token=[^&;]*[&;]?/, '\1')
364 def redirect_to_login
367 if request.method.in? ['GET', 'HEAD']
368 redirect_to arvados_api_client.arvados_login_url(return_to: strip_token_from_path(request.url))
370 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."
375 @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.']
376 self.render_error status: 422
379 false # For convenience to return from callbacks
382 def using_specific_api_token(api_token)
384 [:arvados_api_token, :user].each do |key|
385 start_values[key] = Thread.current[key]
387 load_api_token(api_token)
391 start_values.each_key { |key| Thread.current[key] = start_values[key] }
396 def accept_uuid_as_id_param
397 if params[:id] and params[:id].match /\D/
398 params[:uuid] = params.delete :id
402 def find_object_by_uuid
406 elsif not params[:uuid].is_a?(String)
407 @object = model_class.where(uuid: params[:uuid]).first
408 elsif params[:uuid].empty?
410 elsif (model_class != Link and
411 resource_class_for_uuid(params[:uuid]) == Link)
412 @name_link = Link.find(params[:uuid])
413 @object = model_class.find(@name_link.head_uuid)
415 @object = model_class.find(params[:uuid])
417 rescue ArvadosApiClient::NotFoundException, RuntimeError => error
418 if error.is_a?(RuntimeError) and (error.message !~ /^argument to find\(/)
421 render_not_found(error)
428 Rails.cache.delete_matched(/^request_#{Thread.current.object_id}_/)
430 Rails.cache.delete_matched(/^request_#{Thread.current.object_id}_/)
433 # Set up the thread with the given API token and associated user object.
434 def load_api_token(new_token)
435 Thread.current[:arvados_api_token] = new_token
437 Thread.current[:user] = nil
439 Thread.current[:user] = User.current
443 # If there's a valid api_token parameter, set up the session with that
444 # user's information. Return true if the method redirects the request
445 # (usually a post-login redirect); false otherwise.
446 def setup_user_session
447 return false unless params[:api_token]
448 Thread.current[:arvados_api_token] = params[:api_token]
451 rescue ArvadosApiClient::NotLoggedInException
452 false # We may redirect to login, or not, based on the current action.
454 session[:arvados_api_token] = params[:api_token]
455 # If we later have trouble contacting the API server, we still want
456 # to be able to render basic user information in the UI--see
457 # render_exception above. We store that in the session here. This is
458 # not intended to be used as a general-purpose cache. See #2891.
462 first_name: user.first_name,
463 last_name: user.last_name,
464 is_active: user.is_active,
465 is_admin: user.is_admin,
469 if !request.format.json? and request.method.in? ['GET', 'HEAD']
470 # Repeat this request with api_token in the (new) session
471 # cookie instead of the query string. This prevents API
472 # tokens from appearing in (and being inadvisedly copied
473 # and pasted from) browser Location bars.
474 redirect_to strip_token_from_path(request.fullpath)
480 Thread.current[:arvados_api_token] = nil
484 # Save the session API token in thread-local storage, and yield.
485 # This method also takes care of session setup if the request
486 # provides a valid api_token parameter.
487 # If a token is unavailable or expired, the block is still run, with
489 def set_thread_api_token
490 if Thread.current[:arvados_api_token]
491 yield # An API token has already been found - pass it through.
493 elsif setup_user_session
494 return # A new session was set up and received a response.
498 load_api_token(session[:arvados_api_token])
500 rescue ArvadosApiClient::NotLoggedInException
501 # If we got this error with a token, it must've expired.
502 # Retry the request without a token.
503 unless Thread.current[:arvados_api_token].nil?
508 # Remove token in case this Thread is used for anything else.
513 # Redirect to login/welcome if client provided expired API token (or none at all)
514 def require_thread_api_token
515 if Thread.current[:arvados_api_token]
517 elsif session[:arvados_api_token]
518 # Expired session. Clear it before refreshing login so that,
519 # if this login procedure fails, we end up showing the "please
520 # log in" page instead of getting stuck in a redirect loop.
521 session.delete :arvados_api_token
524 redirect_to welcome_users_path(return_to: request.fullpath)
528 def ensure_current_user_is_admin
529 unless current_user and current_user.is_admin
530 @errors = ['Permission denied']
531 self.render_error status: 401
535 helper_method :unsigned_user_agreements
536 def unsigned_user_agreements
537 @signed_ua_uuids ||= UserAgreement.signatures.map &:head_uuid
538 @unsigned_user_agreements ||= UserAgreement.all.map do |ua|
539 if not @signed_ua_uuids.index ua.uuid
540 Collection.find(ua.uuid)
545 def check_user_agreements
546 if current_user && !current_user.is_active
547 if not current_user.is_invited
548 return redirect_to inactive_users_path(return_to: request.fullpath)
550 if unsigned_user_agreements.empty?
551 # No agreements to sign. Perhaps we just need to ask?
552 current_user.activate
553 if !current_user.is_active
554 logger.warn "#{current_user.uuid.inspect}: " +
555 "No user agreements to sign, but activate failed!"
558 if !current_user.is_active
559 redirect_to user_agreements_path(return_to: request.fullpath)
565 def check_user_profile
566 if request.method.downcase != 'get' || params[:partial] ||
567 params[:tab_pane] || params[:action_method] ||
568 params[:action] == 'setup_popup'
572 if missing_required_profile?
573 redirect_to profile_user_path(current_user.uuid, return_to: request.fullpath)
578 helper_method :missing_required_profile?
579 def missing_required_profile?
580 missing_required = false
582 profile_config = Rails.configuration.user_profile_form_fields
583 if current_user && profile_config
584 current_user_profile = current_user.prefs[:profile]
585 profile_config.kind_of?(Array) && profile_config.andand.each do |entry|
587 if !current_user_profile ||
588 !current_user_profile[entry['key'].to_sym] ||
589 current_user_profile[entry['key'].to_sym].empty?
590 missing_required = true
601 return Rails.configuration.arvados_theme
604 @@notification_tests = []
606 @@notification_tests.push lambda { |controller, current_user|
607 AuthorizedKey.limit(1).where(authorized_user_uuid: current_user.uuid).each do
610 return lambda { |view|
611 view.render partial: 'notifications/ssh_key_notification'
615 @@notification_tests.push lambda { |controller, current_user|
616 Collection.limit(1).where(created_by: current_user.uuid).each do
619 return lambda { |view|
620 view.render partial: 'notifications/collections_notification'
624 @@notification_tests.push lambda { |controller, current_user|
625 PipelineInstance.limit(1).where(created_by: current_user.uuid).each do
628 return lambda { |view|
629 view.render partial: 'notifications/pipelines_notification'
633 def check_user_notifications
634 return if params['tab_pane']
636 @notification_count = 0
639 if current_user.andand.is_active
640 @showallalerts = false
641 @@notification_tests.each do |t|
642 a = t.call(self, current_user)
644 @notification_count += 1
645 @notifications.push a
650 if @notification_count == 0
651 @notification_count = ''
655 helper_method :all_projects
657 @all_projects ||= Group.
658 filter([['group_class','=','project']]).order('name')
661 helper_method :my_projects
663 return @my_projects if @my_projects
666 all_projects.each do |g|
667 root_of[g.uuid] = g.owner_uuid
673 root_of = root_of.each_with_object({}) do |(child, parent), h|
675 h[child] = root_of[parent]
682 @my_projects = @my_projects.select do |g|
683 root_of[g.uuid] == current_user.uuid
687 helper_method :projects_shared_with_me
688 def projects_shared_with_me
689 my_project_uuids = my_projects.collect &:uuid
690 all_projects.reject { |x| x.uuid.in? my_project_uuids }
693 helper_method :recent_jobs_and_pipelines
694 def recent_jobs_and_pipelines
696 PipelineInstance.limit(10)).
698 (x.finished_at || x.started_at rescue nil) || x.modified_at || x.created_at
702 helper_method :running_pipelines
703 def running_pipelines
704 pi = PipelineInstance.order(["started_at asc", "created_at asc"]).filter([["state", "in", ["RunningOnServer", "RunningOnClient"]]])
707 pl.components.each do |k,v|
708 if v.is_a? Hash and v[:job]
709 jobs[v[:job][:uuid]] = {}
715 Job.filter([["uuid", "in", jobs.keys]]).each do |j|
720 pl.components.each do |k,v|
721 if v.is_a? Hash and v[:job]
722 v[:job] = jobs[v[:job][:uuid]]
731 helper_method :finished_pipelines
732 def finished_pipelines lim
733 PipelineInstance.limit(lim).order(["finished_at desc"]).filter([["state", "in", ["Complete", "Failed", "Paused"]], ["finished_at", "!=", nil]])
736 helper_method :recent_collections
737 def recent_collections lim
738 c = Collection.limit(lim).order(["modified_at desc"]).filter([["owner_uuid", "is_a", "arvados#group"]])
740 Group.filter([["uuid", "in", c.map(&:owner_uuid)]]).each do |g|
743 {collections: c, owners: own}
746 helper_method :my_project_tree
752 helper_method :shared_project_tree
753 def shared_project_tree
758 def build_project_trees
759 return if @my_project_tree and @shared_project_tree
760 parent_of = {current_user.uuid => 'me'}
761 all_projects.each do |ob|
762 parent_of[ob.uuid] = ob.owner_uuid
764 children_of = {false => [], 'me' => [current_user]}
765 all_projects.each do |ob|
766 if ob.owner_uuid != current_user.uuid and
767 not parent_of.has_key? ob.owner_uuid
768 parent_of[ob.uuid] = false
770 children_of[parent_of[ob.uuid]] ||= []
771 children_of[parent_of[ob.uuid]] << ob
773 buildtree = lambda do |children_of, root_uuid=false|
775 children_of[root_uuid].andand.each do |ob|
776 tree[ob] = buildtree.call(children_of, ob.uuid)
780 sorted_paths = lambda do |tree, depth=0|
782 tree.keys.sort_by { |ob|
783 ob.is_a?(String) ? ob : ob.friendly_link_name
785 paths << {object: ob, depth: depth}
786 paths += sorted_paths.call tree[ob], depth+1
791 sorted_paths.call buildtree.call(children_of, 'me')
792 @shared_project_tree =
793 sorted_paths.call({'Projects shared with me' =>
794 buildtree.call(children_of, false)})
797 helper_method :get_object
799 if @get_object.nil? and @objects
800 @get_object = @objects.each_with_object({}) do |object, h|
801 h[object.uuid] = object
808 helper_method :project_breadcrumbs
809 def project_breadcrumbs
811 current = @name_link || @object
813 if current.is_a?(Group) and current.group_class == 'project'
814 crumbs.prepend current
816 if current.is_a? Link
817 current = Group.find?(current.tail_uuid)
819 current = Group.find?(current.owner_uuid)
825 helper_method :current_project_uuid
826 def current_project_uuid
827 if @object.is_a? Group and @object.group_class == 'project'
829 elsif @name_link.andand.tail_uuid
831 elsif @object and resource_class_for_uuid(@object.owner_uuid) == Group
838 # helper method to get links for given object or uuid
839 helper_method :links_for_object
840 def links_for_object object_or_uuid
841 raise ArgumentError, 'No input argument' unless object_or_uuid
842 preload_links_for_objects([object_or_uuid])
843 uuid = object_or_uuid.is_a?(String) ? object_or_uuid : object_or_uuid.uuid
844 @all_links_for[uuid] ||= []
847 # helper method to preload links for given objects and uuids
848 helper_method :preload_links_for_objects
849 def preload_links_for_objects objects_and_uuids
850 @all_links_for ||= {}
852 raise ArgumentError, 'Argument is not an array' unless objects_and_uuids.is_a? Array
853 return @all_links_for if objects_and_uuids.empty?
855 uuids = objects_and_uuids.collect { |x| x.is_a?(String) ? x : x.uuid }
857 # if already preloaded for all of these uuids, return
858 if not uuids.select { |x| @all_links_for[x].nil? }.any?
859 return @all_links_for
863 @all_links_for[x] = []
866 # TODO: make sure we get every page of results from API server
867 Link.filter([['head_uuid', 'in', uuids]]).each do |link|
868 @all_links_for[link.head_uuid] << link
873 # helper method to get a certain number of objects of a specific type
874 # this can be used to replace any uses of: "dataclass.limit(n)"
875 helper_method :get_n_objects_of_class
876 def get_n_objects_of_class dataclass, size
877 @objects_map_for ||= {}
879 raise ArgumentError, 'Argument is not a data class' unless dataclass.is_a? Class and dataclass < ArvadosBase
880 raise ArgumentError, 'Argument is not a valid limit size' unless (size && size>0)
882 # if the objects_map_for has a value for this dataclass, and the
883 # size used to retrieve those objects is equal, return it
884 size_key = "#{dataclass.name}_size"
885 if @objects_map_for[dataclass.name] && @objects_map_for[size_key] &&
886 (@objects_map_for[size_key] == size)
887 return @objects_map_for[dataclass.name]
890 @objects_map_for[size_key] = size
891 @objects_map_for[dataclass.name] = dataclass.limit(size)
894 # helper method to get collections for the given uuid
895 helper_method :collections_for_object
896 def collections_for_object uuid
897 raise ArgumentError, 'No input argument' unless uuid
898 preload_collections_for_objects([uuid])
899 @all_collections_for[uuid] ||= []
902 # helper method to preload collections for the given uuids
903 helper_method :preload_collections_for_objects
904 def preload_collections_for_objects uuids
905 @all_collections_for ||= {}
907 raise ArgumentError, 'Argument is not an array' unless uuids.is_a? Array
908 return @all_collections_for if uuids.empty?
910 # if already preloaded for all of these uuids, return
911 if not uuids.select { |x| @all_collections_for[x].nil? }.any?
912 return @all_collections_for
916 @all_collections_for[x] = []
919 # TODO: make sure we get every page of results from API server
920 Collection.where(uuid: uuids).each do |collection|
921 @all_collections_for[collection.uuid] << collection
926 # helper method to get log collections for the given log
927 helper_method :log_collections_for_object
928 def log_collections_for_object log
929 raise ArgumentError, 'No input argument' unless log
931 preload_log_collections_for_objects([log])
934 fixup = /([a-f0-9]{32}\+\d+)(\+?.*)/.match(log)
935 if fixup && fixup.size>1
939 @all_log_collections_for[uuid] ||= []
942 # helper method to preload collections for the given uuids
943 helper_method :preload_log_collections_for_objects
944 def preload_log_collections_for_objects logs
945 @all_log_collections_for ||= {}
947 raise ArgumentError, 'Argument is not an array' unless logs.is_a? Array
948 return @all_log_collections_for if logs.empty?
952 fixup = /([a-f0-9]{32}\+\d+)(\+?.*)/.match(log)
953 if fixup && fixup.size>1
960 # if already preloaded for all of these uuids, return
961 if not uuids.select { |x| @all_log_collections_for[x].nil? }.any?
962 return @all_log_collections_for
966 @all_log_collections_for[x] = []
969 # TODO: make sure we get every page of results from API server
970 Collection.where(uuid: uuids).each do |collection|
971 @all_log_collections_for[collection.uuid] << collection
973 @all_log_collections_for
976 # helper method to get object of a given dataclass and uuid
977 helper_method :object_for_dataclass
978 def object_for_dataclass dataclass, uuid
979 raise ArgumentError, 'No input argument dataclass' unless (dataclass && uuid)
980 preload_objects_for_dataclass(dataclass, [uuid])
984 # helper method to preload objects for given dataclass and uuids
985 helper_method :preload_objects_for_dataclass
986 def preload_objects_for_dataclass dataclass, uuids
989 raise ArgumentError, 'Argument is not a data class' unless dataclass.is_a? Class
990 raise ArgumentError, 'Argument is not an array' unless uuids.is_a? Array
992 return @objects_for if uuids.empty?
994 # if already preloaded for all of these uuids, return
995 if not uuids.select { |x| @objects_for[x].nil? }.any?
999 dataclass.where(uuid: uuids).each do |obj|
1000 @objects_for[obj.uuid] = obj
1005 def wiselinks_layout