1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
5 class ApplicationController < ActionController::Base
6 include ArvadosApiClientHelper
7 include ApplicationHelper
9 respond_to :html, :json, :js
12 ERROR_ACTIONS = [:render_error, :render_not_found]
14 around_action :thread_clear
15 around_action :set_current_request_id
16 around_action :set_thread_api_token
17 # Methods that don't require login should
18 # skip_around_action :require_thread_api_token
19 around_action :require_thread_api_token, except: ERROR_ACTIONS
20 before_action :ensure_arvados_api_exists, only: [:index, :show]
21 before_action :set_cache_buster
22 before_action :accept_uuid_as_id_param, except: ERROR_ACTIONS
23 before_action :check_user_agreements, except: ERROR_ACTIONS
24 before_action :check_user_profile, except: ERROR_ACTIONS
25 before_action :load_filters_and_paging_params, except: ERROR_ACTIONS
26 before_action :find_object_by_uuid, except: [:create, :index, :choose] + ERROR_ACTIONS
30 rescue_from(ActiveRecord::RecordNotFound,
31 ActionController::RoutingError,
32 AbstractController::ActionNotFound,
33 with: :render_not_found)
34 rescue_from(Exception,
35 ActionController::UrlGenerationError,
36 with: :render_exception)
40 response.headers["Cache-Control"] = "no-cache, no-store, max-age=0, must-revalidate"
41 response.headers["Pragma"] = "no-cache"
42 response.headers["Expires"] = "Fri, 01 Jan 1990 00:00:00 GMT"
45 def unprocessable(message=nil)
48 @errors << message if message
49 render_error status: 422
52 def render_error(opts={})
53 # Helpers can rely on the presence of @errors to know they're
54 # being used in an error page.
58 # json must come before html here, so it gets used as the
59 # default format when js is requested by the client. This lets
60 # ajax:error callback parse the response correctly, even though
62 f.json { render opts.merge(json: {success: false, errors: @errors}) }
63 f.html { render({action: 'error'}.merge(opts)) }
64 f.all { render({action: 'error', formats: 'text'}.merge(opts)) }
68 def render_exception(e)
69 logger.error e.inspect
70 logger.error e.backtrace.collect { |x| x + "\n" }.join('') if e.backtrace
71 err_opts = {status: 422}
72 if e.is_a?(ArvadosApiClient::ApiError)
73 err_opts.merge!(action: 'api_error', locals: {api_error: e})
74 @errors = e.api_response[:errors]
75 elsif @object.andand.errors.andand.full_messages.andand.any?
76 @errors = @object.errors.full_messages
80 # Make user information available on the error page, falling back to the
81 # session cache if the API server is unavailable.
83 load_api_token(session[:arvados_api_token])
84 rescue ArvadosApiClient::ApiError
85 unless session[:user].nil?
87 Thread.current[:user] = User.new(session[:user])
88 rescue ArvadosApiClient::ApiError
89 # This can happen if User's columns are unavailable. Nothing to do.
93 # Preload projects trees for the template. If that's not doable, set empty
94 # trees so error page rendering can proceed. (It's easier to rescue the
95 # exception here than in a template.)
96 unless current_user.nil?
98 my_starred_projects current_user, 'project'
99 build_my_wanted_projects_tree current_user
100 rescue ArvadosApiClient::ApiError
101 # Fall back to the default-setting code later.
104 @starred_projects ||= []
105 @my_wanted_projects_tree ||= []
106 render_error(err_opts)
109 def render_not_found(e=ActionController::RoutingError.new("Path not found"))
110 logger.error e.inspect
111 @errors = ["Path not found"]
112 set_thread_api_token do
113 self.render_error(action: '404', status: 404)
119 # The order can be left empty to allow it to default.
120 # Or it can be a comma separated list of real database column names, one per model.
121 # Column names should always be qualified by a table name and a direction is optional, defaulting to asc
122 # (e.g. "collections.name" or "collections.name desc").
123 # If a column name is specified, that table will be sorted by that column.
124 # If there are objects from different models that will be shown (such as in Pipelines and processes tab),
125 # then a sort column name can optionally be specified for each model, passed as an comma-separated list (e.g. "jobs.script, pipeline_instances.name")
126 # Currently only one sort column name and direction can be specified for each model.
127 def load_filters_and_paging_params
128 if params[:order].blank?
129 @order = 'created_at desc'
130 elsif params[:order].is_a? Array
131 @order = params[:order]
134 @order = JSON.load(params[:order])
136 @order = params[:order].split(',')
139 @order = [@order] unless @order.is_a? Array
143 @limit = params[:limit].to_i
148 @offset = params[:offset].to_i
153 filters = params[:filters]
154 if filters.is_a? String
155 filters = Oj.load filters
156 elsif filters.is_a? Array
157 filters = filters.collect do |filter|
158 if filter.is_a? String
159 # Accept filters[]=["foo","=","bar"]
162 # Accept filters=[["foo","=","bar"]]
167 # After this, params[:filters] can be trusted to be an array of arrays:
168 params[:filters] = filters
173 def find_objects_for_index
174 @objects ||= model_class
175 @objects = @objects.filter(@filters).limit(@limit).offset(@offset)
176 @objects.fetch_multiple_pages(false)
183 @next_page_href = next_page_href(partial: params[:partial], filters: @filters.to_json)
185 content: render_to_string(partial: "show_#{params[:partial]}",
187 next_page_href: @next_page_href
190 render json: @objects
195 render_pane params[:tab_pane]
204 helper_method :render_pane
205 def render_pane tab_pane, opts={}
207 partial: 'show_' + tab_pane.downcase,
209 comparable: self.respond_to?(:compare),
212 }.merge(opts[:locals] || {})
215 render_to_string render_opts
221 def ensure_arvados_api_exists
222 if model_class.is_a?(Class) && model_class < ArvadosBase && !model_class.api_exists?(params['action'].to_sym)
223 @errors = ["#{params['action']} method is not supported for #{params['controller']}"]
224 return render_error(status: 404)
229 @objects = nil if !defined?(@objects)
230 find_objects_for_index if !@objects
234 helper_method :next_page_offset
235 def next_page_offset objects=nil
239 if objects.respond_to?(:result_offset) and
240 objects.respond_to?(:result_limit)
241 next_offset = objects.result_offset + objects.result_limit
242 if objects.respond_to?(:items_available) and (objects.items_available != nil) and (next_offset < objects.items_available)
244 elsif @objects.results.size > 0 and (params[:count] == 'none' or
245 (params[:controller] == 'search' and params[:action] == 'choose'))
246 last_object_class = @objects.last.class
247 if params['last_object_class'].nil? or params['last_object_class'] == last_object_class.to_s
250 @objects.select{|obj| obj.class == last_object_class}.size
258 helper_method :next_page_href
259 def next_page_href with_params={}
261 url_for with_params.merge(offset: next_page_offset)
265 helper_method :next_page_filters
266 def next_page_filters nextpage_operator
267 next_page_filters = @filters.reject do |attr, op, val|
268 (attr == 'created_at' and op == nextpage_operator) or
269 (attr == 'uuid' and op == 'not in')
273 last_created_at = @objects.last.created_at
276 @objects.each do |obj|
277 last_uuids << obj.uuid if obj.created_at.eql?(last_created_at)
280 next_page_filters += [['created_at', nextpage_operator, last_created_at]]
281 next_page_filters += [['uuid', 'not in', last_uuids]]
289 return render_not_found("object not found")
293 extra_attrs = { href: url_for(action: :show, id: @object) }
294 @object.textile_attributes.each do |textile_attr|
295 extra_attrs.merge!({ "#{textile_attr}Textile" => view_context.render_markup(@object.attributes[textile_attr]) })
297 render json: @object.attributes.merge(extra_attrs)
300 if params['tab_pane']
301 render_pane(if params['tab_pane'].is_a? Hash then params['tab_pane']["name"] else params['tab_pane'] end)
302 elsif request.request_method.in? ['GET', 'HEAD']
305 redirect_to (params[:return_to] ||
306 polymorphic_url(@object,
307 anchor: params[:redirect_to_anchor]))
314 def redirect_to uri, *args
316 if not uri.is_a? String
317 uri = polymorphic_url(uri)
319 render json: {href: uri}
326 @objects = nil if !defined?(@objects)
327 params[:limit] ||= 40
331 find_objects_for_index if !@objects
333 content: render_to_string(partial: "choose_rows.html",
335 next_page_href: next_page_href(partial: params[:partial])
340 find_objects_for_index if !@objects
341 render partial: 'choose', locals: {multiple: params[:multiple]}
348 return render_not_found("object not found")
353 @object = model_class.new
357 @updates ||= params[@object.resource_param_name.to_sym]
358 if @updates.is_a? ActionController::Parameters
359 @updates = @updates.to_unsafe_hash
361 @updates.keys.each do |attr|
362 if @object.send(attr).is_a? Hash
363 if @updates[attr].is_a? String
364 @updates[attr] = Oj.load @updates[attr]
366 if params[:merge] || params["merge_#{attr}".to_sym]
367 # Merge provided Hash with current Hash, instead of
369 if @updates[attr].is_a? ActionController::Parameters
370 @updates[attr] = @updates[attr].to_unsafe_hash
372 @updates[attr] = @object.send(attr).with_indifferent_access.
373 deep_merge(@updates[attr].with_indifferent_access)
377 if @object.update_attributes @updates
380 self.render_error status: 422
385 @new_resource_attrs ||= params[model_class.to_s.underscore.singularize]
386 @new_resource_attrs ||= {}
387 @new_resource_attrs.reject! { |k,v| k.to_s == 'uuid' }
388 @object ||= model_class.new @new_resource_attrs, params["options"]
393 render_error status: 422
397 # Clone the given object, merging any attribute values supplied as
398 # with a create action.
400 @new_resource_attrs ||= params[model_class.to_s.underscore.singularize]
401 @new_resource_attrs ||= {}
402 @object = @object.dup
403 @object.update_attributes @new_resource_attrs
404 if not @new_resource_attrs[:name] and @object.respond_to? :name
405 if @object.name and @object.name != ''
406 @object.name = "Copy of #{@object.name}"
418 f.json { render json: @object }
420 redirect_to(params[:return_to] || :back)
425 self.render_error status: 422
430 Thread.current[:user]
434 controller_name.classify.constantize
437 def breadcrumb_page_name
438 (@breadcrumb_page_name ||
439 (@object.friendly_link_name if @object.respond_to? :friendly_link_name) ||
448 %w(Attributes Advanced)
452 @user_is_manager = false
455 if @object.uuid != current_user.andand.uuid
457 @share_links = Link.permissions_for(@object)
458 @user_is_manager = true
459 rescue ArvadosApiClient::AccessForbiddenException,
460 ArvadosApiClient::NotFoundException
466 if not params[:uuids].andand.any?
467 @errors = ["No user/group UUIDs specified to share with."]
468 return render_error(status: 422)
470 results = {"success" => [], "errors" => []}
471 params[:uuids].each do |shared_uuid|
473 Link.create(tail_uuid: shared_uuid, link_class: "permission",
474 name: "can_read", head_uuid: @object.uuid)
475 rescue ArvadosApiClient::ApiError => error
476 error_list = error.api_response.andand[:errors]
477 if error_list.andand.any?
478 results["errors"] += error_list.map { |e| "#{shared_uuid}: #{e}" }
480 error_code = error.api_status || "Bad status"
481 results["errors"] << "#{shared_uuid}: #{error_code} response"
484 results["success"] << shared_uuid
487 if results["errors"].empty?
488 results.delete("errors")
494 f.json { render(json: results, status: status) }
498 helper_method :is_starred
500 links = Link.where(tail_uuid: current_user.uuid,
501 head_uuid: @object.uuid,
502 link_class: 'star').with_count("none")
504 return links.andand.any?
509 helper_method :strip_token_from_path
510 def strip_token_from_path(path)
511 path.sub(/([\?&;])api_token=[^&;]*[&;]?/, '\1')
514 def redirect_to_login
515 if request.xhr? or request.format.json?
516 @errors = ['You are not logged in. Most likely your session has timed out and you need to log in again.']
517 render_error status: 401
518 elsif request.method.in? ['GET', 'HEAD']
519 redirect_to arvados_api_client.arvados_login_url(return_to: strip_token_from_path(request.url))
521 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."
524 false # For convenience to return from callbacks
527 def using_specific_api_token(api_token, opts={})
529 [:arvados_api_token, :user].each do |key|
530 start_values[key] = Thread.current[key]
532 if opts.fetch(:load_user, true)
533 load_api_token(api_token)
535 Thread.current[:arvados_api_token] = api_token
536 Thread.current[:user] = nil
541 start_values.each_key { |key| Thread.current[key] = start_values[key] }
546 def accept_uuid_as_id_param
547 if params[:id] and params[:id].match(/\D/)
548 params[:uuid] = params.delete :id
552 def find_object_by_uuid
556 elsif params[:uuid].nil? or params[:uuid].empty?
558 elsif not params[:uuid].is_a?(String)
559 @object = model_class.where(uuid: params[:uuid]).first
560 elsif (model_class != Link and
561 resource_class_for_uuid(params[:uuid]) == Link)
562 @name_link = Link.find(params[:uuid])
563 @object = model_class.find(@name_link.head_uuid)
565 @object = model_class.find(params[:uuid])
566 load_preloaded_objects [@object]
568 rescue ArvadosApiClient::NotFoundException, ArvadosApiClient::NotLoggedInException, RuntimeError => error
569 if error.is_a?(RuntimeError) and (error.message !~ /^argument to find\(/)
572 render_not_found(error)
579 Rails.cache.delete_matched(/^request_#{Thread.current.object_id}_/)
581 Rails.cache.delete_matched(/^request_#{Thread.current.object_id}_/)
584 def set_current_request_id
585 response.headers['X-Request-Id'] =
586 Thread.current[:request_id] =
587 "req-" + Random::DEFAULT.rand(2**128).to_s(36)[0..19]
589 Thread.current[:request_id] = nil
592 def append_info_to_payload(payload)
594 payload[:request_id] = response.headers['X-Request-Id']
597 # Set up the thread with the given API token and associated user object.
598 def load_api_token(new_token)
599 Thread.current[:arvados_api_token] = new_token
601 Thread.current[:user] = nil
603 Thread.current[:user] = User.current
607 # If there's a valid api_token parameter, set up the session with that
608 # user's information. Return true if the method redirects the request
609 # (usually a post-login redirect); false otherwise.
610 def setup_user_session
611 return false unless params[:api_token]
612 Thread.current[:arvados_api_token] = params[:api_token]
615 rescue ArvadosApiClient::NotLoggedInException
616 false # We may redirect to login, or not, based on the current action.
618 session[:arvados_api_token] = params[:api_token]
619 # If we later have trouble contacting the API server, we still want
620 # to be able to render basic user information in the UI--see
621 # render_exception above. We store that in the session here. This is
622 # not intended to be used as a general-purpose cache. See #2891.
626 first_name: user.first_name,
627 last_name: user.last_name,
628 is_active: user.is_active,
629 is_admin: user.is_admin,
633 if !request.format.json? and request.method.in? ['GET', 'HEAD']
634 # Repeat this request with api_token in the (new) session
635 # cookie instead of the query string. This prevents API
636 # tokens from appearing in (and being inadvisedly copied
637 # and pasted from) browser Location bars.
638 redirect_to strip_token_from_path(request.fullpath)
644 Thread.current[:arvados_api_token] = nil
648 # Save the session API token in thread-local storage, and yield.
649 # This method also takes care of session setup if the request
650 # provides a valid api_token parameter.
651 # If a token is unavailable or expired, the block is still run, with
653 def set_thread_api_token
654 if Thread.current[:arvados_api_token]
655 yield # An API token has already been found - pass it through.
657 elsif setup_user_session
658 return # A new session was set up and received a response.
662 load_api_token(session[:arvados_api_token])
664 rescue ArvadosApiClient::NotLoggedInException
665 # If we got this error with a token, it must've expired.
666 # Retry the request without a token.
667 unless Thread.current[:arvados_api_token].nil?
672 # Remove token in case this Thread is used for anything else.
677 # Redirect to login/welcome if client provided expired API token (or
679 def require_thread_api_token
680 if Thread.current[:arvados_api_token]
682 elsif session[:arvados_api_token]
683 # Expired session. Clear it before refreshing login so that,
684 # if this login procedure fails, we end up showing the "please
685 # log in" page instead of getting stuck in a redirect loop.
686 session.delete :arvados_api_token
689 # If we redirect to the welcome page, the browser will handle
690 # the 302 by itself and the client code will end up rendering
691 # the "welcome" page in some content area where it doesn't make
692 # sense. Instead, we send 401 ("authenticate and try again" or
693 # "display error", depending on how smart the client side is).
694 @errors = ['You are not logged in.']
695 render_error status: 401
697 redirect_to welcome_users_path(return_to: request.fullpath)
701 def ensure_current_user_is_admin
703 @errors = ['Not logged in']
704 render_error status: 401
705 elsif not current_user.is_admin
706 @errors = ['Permission denied']
707 render_error status: 403
711 helper_method :unsigned_user_agreements
712 def unsigned_user_agreements
713 @signed_ua_uuids ||= UserAgreement.signatures.map &:head_uuid
714 @unsigned_user_agreements ||= UserAgreement.all.map do |ua|
715 if not @signed_ua_uuids.index ua.uuid
716 Collection.find(ua.uuid)
721 def check_user_agreements
722 if current_user && !current_user.is_active
723 if not current_user.is_invited
724 return redirect_to inactive_users_path(return_to: request.fullpath)
726 if unsigned_user_agreements.empty?
727 # No agreements to sign. Perhaps we just need to ask?
728 current_user.activate
729 if !current_user.is_active
730 logger.warn "#{current_user.uuid.inspect}: " +
731 "No user agreements to sign, but activate failed!"
734 if !current_user.is_active
735 redirect_to user_agreements_path(return_to: request.fullpath)
741 def check_user_profile
742 return true if !current_user
743 if request.method.downcase != 'get' || params[:partial] ||
744 params[:tab_pane] || params[:action_method] ||
745 params[:action] == 'setup_popup'
749 if missing_required_profile?
750 redirect_to profile_user_path(current_user.uuid, return_to: request.fullpath)
755 helper_method :missing_required_profile?
756 def missing_required_profile?
757 missing_required = false
759 profile_config = Rails.configuration.Workbench.UserProfileFormFields
760 if current_user && !profile_config.empty?
761 current_user_profile = current_user.prefs[:profile]
762 profile_config.each do |k, entry|
764 if !current_user_profile ||
765 !current_user_profile[k] ||
766 current_user_profile[k].empty?
767 missing_required = true
778 return Rails.configuration.Workbench.Theme
781 @@notification_tests = []
783 @@notification_tests.push lambda { |controller, current_user|
784 return nil if Rails.configuration.Services.WebShell.ExternalURL != URI("")
785 AuthorizedKey.limit(1).with_count('none').where(authorized_user_uuid: current_user.uuid).each do
788 return lambda { |view|
789 view.render partial: 'notifications/ssh_key_notification'
793 @@notification_tests.push lambda { |controller, current_user|
794 Collection.limit(1).with_count('none').where(created_by: current_user.uuid).each do
797 return lambda { |view|
798 view.render partial: 'notifications/collections_notification'
802 @@notification_tests.push lambda { |controller, current_user|
803 if PipelineInstance.api_exists?(:index)
804 PipelineInstance.limit(1).with_count('none').where(created_by: current_user.uuid).each do
810 return lambda { |view|
811 view.render partial: 'notifications/pipelines_notification'
815 helper_method :user_notifications
816 def user_notifications
817 @errors = nil if !defined?(@errors)
818 return [] if @errors or not current_user.andand.is_active or not Rails.configuration.Workbench.ShowUserNotifications
819 @notifications ||= @@notification_tests.map do |t|
820 t.call(self, current_user)
824 helper_method :all_projects
826 @all_projects ||= Group.
827 filter([['group_class','IN',['project','filter']]]).order('name')
830 helper_method :my_projects
832 return @my_projects if @my_projects
835 all_projects.each do |g|
836 root_of[g.uuid] = g.owner_uuid
842 root_of = root_of.each_with_object({}) do |(child, parent), h|
844 h[child] = root_of[parent]
851 @my_projects = @my_projects.select do |g|
852 root_of[g.uuid] == current_user.uuid
856 helper_method :projects_shared_with_me
857 def projects_shared_with_me
858 my_project_uuids = my_projects.collect &:uuid
859 all_projects.reject { |x| x.uuid.in? my_project_uuids }
862 helper_method :recent_jobs_and_pipelines
863 def recent_jobs_and_pipelines
865 PipelineInstance.limit(10).with_count("none")).
867 (x.finished_at || x.started_at rescue nil) || x.modified_at || x.created_at
871 helper_method :running_pipelines
872 def running_pipelines
873 pi = PipelineInstance.order(["started_at asc", "created_at asc"]).with_count("none").filter([["state", "in", ["RunningOnServer", "RunningOnClient"]]])
876 pl.components.each do |k,v|
877 if v.is_a? Hash and v[:job]
878 jobs[v[:job][:uuid]] = {}
884 Job.filter([["uuid", "in", jobs.keys]]).with_count("none").each do |j|
889 pl.components.each do |k,v|
890 if v.is_a? Hash and v[:job]
891 v[:job] = jobs[v[:job][:uuid]]
900 helper_method :recent_processes
901 def recent_processes lim
905 if PipelineInstance.api_exists?(:index)
906 cols = %w(uuid owner_uuid created_at modified_at pipeline_template_uuid name state started_at finished_at)
907 pipelines = PipelineInstance.select(cols).limit(lim).order(["created_at desc"]).with_count("none")
908 pipelines.results.each { |pi| procs[pi] = pi.created_at }
911 crs = ContainerRequest.limit(lim).with_count("none").order(["created_at desc"]).filter([["requesting_container_uuid", "=", nil]])
912 crs.results.each { |c| procs[c] = c.created_at }
914 Hash[procs.sort_by {|key, value| value}].keys.reverse.first(lim)
917 helper_method :recent_collections
918 def recent_collections lim
919 c = Collection.limit(lim).with_count("none").order(["modified_at desc"]).results
921 Group.filter([["uuid", "in", c.map(&:owner_uuid)]]).with_count("none").each do |g|
924 {collections: c, owners: own}
927 helper_method :my_starred_projects
928 def my_starred_projects user, group_class
929 return if defined?(@starred_projects) && @starred_projects
930 links = Link.filter([['owner_uuid', 'in', ["#{Rails.configuration.ClusterID}-j7d0g-publicfavorites", user.uuid]],
931 ['link_class', '=', 'star'],
932 ['head_uuid', 'is_a', 'arvados#group']]).with_count("none").select(%w(head_uuid))
933 uuids = links.collect { |x| x.head_uuid }
935 starred_projects = Group.filter([['uuid', 'in', uuids]]).order('name').with_count("none")
937 starred_projects = Group.filter([['uuid', 'in', uuids],['group_class', '=', group_class]]).order('name').with_count("none")
939 @starred_projects = starred_projects.results
942 # If there are more than 200 projects that are readable by the user,
943 # build the tree using only the top 200+ projects owned by the user,
944 # from the top three levels.
945 # That is: get toplevel projects under home, get subprojects of
946 # these projects, and so on until we hit the limit.
947 def my_wanted_projects(user, page_size=100)
948 return @my_wanted_projects if defined?(@my_wanted_projects) && @my_wanted_projects
953 @too_many_projects = false
954 @reached_level_limit = false
955 while from_top.size <= page_size*2
956 current_level = Group.filter([['group_class','IN',['project','filter']],
957 ['owner_uuid', 'in', uuids]])
958 .order('name').limit(page_size*2)
959 break if current_level.results.size == 0
960 @too_many_projects = true if current_level.items_available > current_level.results.size
961 from_top.concat current_level.results
962 uuids = current_level.results.collect(&:uuid)
965 @reached_level_limit = true
969 @my_wanted_projects = from_top
972 helper_method :my_wanted_projects_tree
973 def my_wanted_projects_tree(user, page_size=100)
974 build_my_wanted_projects_tree(user, page_size)
975 [@my_wanted_projects_tree, @too_many_projects, @reached_level_limit]
978 def build_my_wanted_projects_tree(user, page_size=100)
979 return @my_wanted_projects_tree if defined?(@my_wanted_projects_tree) && @my_wanted_projects_tree
981 parent_of = {user.uuid => 'me'}
982 my_wanted_projects(user, page_size).each do |ob|
983 parent_of[ob.uuid] = ob.owner_uuid
985 children_of = {false => [], 'me' => [user]}
986 my_wanted_projects(user, page_size).each do |ob|
987 if ob.owner_uuid != user.uuid and
988 not parent_of.has_key? ob.owner_uuid
989 parent_of[ob.uuid] = false
991 children_of[parent_of[ob.uuid]] ||= []
992 children_of[parent_of[ob.uuid]] << ob
994 buildtree = lambda do |chldrn_of, root_uuid=false|
996 chldrn_of[root_uuid].andand.each do |ob|
997 tree[ob] = buildtree.call(chldrn_of, ob.uuid)
1001 sorted_paths = lambda do |tree, depth=0|
1003 tree.keys.sort_by { |ob|
1004 ob.is_a?(String) ? ob : ob.friendly_link_name
1006 paths << {object: ob, depth: depth}
1007 paths += sorted_paths.call tree[ob], depth+1
1011 @my_wanted_projects_tree =
1012 sorted_paths.call buildtree.call(children_of, 'me')
1015 helper_method :get_object
1017 if @get_object.nil? and @objects
1018 @get_object = @objects.each_with_object({}) do |object, h|
1019 h[object.uuid] = object
1026 helper_method :project_breadcrumbs
1027 def project_breadcrumbs
1029 current = @name_link || @object
1031 # Halt if a group ownership loop is detected. API should refuse
1032 # to produce this state, but it could still arise from a race
1033 # condition when group ownership changes between our find()
1035 break if crumbs.collect(&:uuid).include? current.uuid
1037 if current.is_a?(Group) and current.group_class == 'project'
1038 crumbs.prepend current
1040 if current.is_a? Link
1041 current = Group.find?(current.tail_uuid)
1043 current = Group.find?(current.owner_uuid)
1049 helper_method :current_project_uuid
1050 def current_project_uuid
1051 if @object.is_a? Group and @object.group_class == 'project'
1053 elsif @name_link.andand.tail_uuid
1054 @name_link.tail_uuid
1055 elsif @object and resource_class_for_uuid(@object.owner_uuid) == Group
1062 # helper method to get links for given object or uuid
1063 helper_method :links_for_object
1064 def links_for_object object_or_uuid
1065 raise ArgumentError, 'No input argument' unless object_or_uuid
1066 preload_links_for_objects([object_or_uuid])
1067 uuid = object_or_uuid.is_a?(String) ? object_or_uuid : object_or_uuid.uuid
1068 @all_links_for[uuid] ||= []
1071 # helper method to preload links for given objects and uuids
1072 helper_method :preload_links_for_objects
1073 def preload_links_for_objects objects_and_uuids
1074 @all_links_for ||= {}
1076 raise ArgumentError, 'Argument is not an array' unless objects_and_uuids.is_a? Array
1077 return @all_links_for if objects_and_uuids.empty?
1079 uuids = objects_and_uuids.collect { |x| x.is_a?(String) ? x : x.uuid }
1081 # if already preloaded for all of these uuids, return
1082 if not uuids.select { |x| @all_links_for[x].nil? }.any?
1083 return @all_links_for
1087 @all_links_for[x] = []
1090 # TODO: make sure we get every page of results from API server
1091 Link.filter([['head_uuid', 'in', uuids]]).with_count("none").each do |link|
1092 @all_links_for[link.head_uuid] << link
1097 # helper method to get a certain number of objects of a specific type
1098 # this can be used to replace any uses of: "dataclass.limit(n)"
1099 helper_method :get_n_objects_of_class
1100 def get_n_objects_of_class dataclass, size
1101 @objects_map_for ||= {}
1103 raise ArgumentError, 'Argument is not a data class' unless dataclass.is_a? Class and dataclass < ArvadosBase
1104 raise ArgumentError, 'Argument is not a valid limit size' unless (size && size>0)
1106 # if the objects_map_for has a value for this dataclass, and the
1107 # size used to retrieve those objects is equal, return it
1108 size_key = "#{dataclass.name}_size"
1109 if @objects_map_for[dataclass.name] && @objects_map_for[size_key] &&
1110 (@objects_map_for[size_key] == size)
1111 return @objects_map_for[dataclass.name]
1114 @objects_map_for[size_key] = size
1115 @objects_map_for[dataclass.name] = dataclass.limit(size)
1118 # helper method to get collections for the given uuid
1119 helper_method :collections_for_object
1120 def collections_for_object uuid
1121 raise ArgumentError, 'No input argument' unless uuid
1122 preload_collections_for_objects([uuid])
1123 @all_collections_for[uuid] ||= []
1126 # helper method to preload collections for the given uuids
1127 helper_method :preload_collections_for_objects
1128 def preload_collections_for_objects uuids
1129 @all_collections_for ||= {}
1131 raise ArgumentError, 'Argument is not an array' unless uuids.is_a? Array
1132 return @all_collections_for if uuids.empty?
1134 # if already preloaded for all of these uuids, return
1135 if not uuids.select { |x| @all_collections_for[x].nil? }.any?
1136 return @all_collections_for
1140 @all_collections_for[x] = []
1143 # TODO: make sure we get every page of results from API server
1144 Collection.where(uuid: uuids).with_count("none").each do |collection|
1145 @all_collections_for[collection.uuid] << collection
1147 @all_collections_for
1150 # helper method to get log collections for the given log
1151 helper_method :log_collections_for_object
1152 def log_collections_for_object log
1153 raise ArgumentError, 'No input argument' unless log
1155 preload_log_collections_for_objects([log])
1158 fixup = /([a-f0-9]{32}\+\d+)(\+?.*)/.match(log)
1159 if fixup && fixup.size>1
1163 @all_log_collections_for[uuid] ||= []
1166 # helper method to preload collections for the given uuids
1167 helper_method :preload_log_collections_for_objects
1168 def preload_log_collections_for_objects logs
1169 @all_log_collections_for ||= {}
1171 raise ArgumentError, 'Argument is not an array' unless logs.is_a? Array
1172 return @all_log_collections_for if logs.empty?
1176 fixup = /([a-f0-9]{32}\+\d+)(\+?.*)/.match(log)
1177 if fixup && fixup.size>1
1184 # if already preloaded for all of these uuids, return
1185 if not uuids.select { |x| @all_log_collections_for[x].nil? }.any?
1186 return @all_log_collections_for
1190 @all_log_collections_for[x] = []
1193 # TODO: make sure we get every page of results from API server
1194 Collection.where(uuid: uuids).with_count("none").each do |collection|
1195 @all_log_collections_for[collection.uuid] << collection
1197 @all_log_collections_for
1200 # Helper method to get one collection for the given portable_data_hash
1201 # This is used to determine if a pdh is readable by the current_user
1202 helper_method :collection_for_pdh
1203 def collection_for_pdh pdh
1204 raise ArgumentError, 'No input argument' unless pdh
1205 preload_for_pdhs([pdh])
1206 @all_pdhs_for[pdh] ||= []
1209 # Helper method to preload one collection each for the given pdhs
1210 # This is used to determine if a pdh is readable by the current_user
1211 helper_method :preload_for_pdhs
1212 def preload_for_pdhs pdhs
1213 @all_pdhs_for ||= {}
1215 raise ArgumentError, 'Argument is not an array' unless pdhs.is_a? Array
1216 return @all_pdhs_for if pdhs.empty?
1218 # if already preloaded for all of these pdhs, return
1219 if not pdhs.select { |x| @all_pdhs_for[x].nil? }.any?
1220 return @all_pdhs_for
1224 @all_pdhs_for[x] = []
1227 Collection.select(%w(portable_data_hash)).where(portable_data_hash: pdhs).distinct().with_count("none").each do |collection|
1228 @all_pdhs_for[collection.portable_data_hash] << collection
1233 # helper method to get object of a given dataclass and uuid
1234 helper_method :object_for_dataclass
1235 def object_for_dataclass dataclass, uuid, by_attr=nil
1236 raise ArgumentError, 'No input argument dataclass' unless (dataclass && uuid)
1237 preload_objects_for_dataclass(dataclass, [uuid], by_attr)
1241 # helper method to preload objects for given dataclass and uuids
1242 helper_method :preload_objects_for_dataclass
1243 def preload_objects_for_dataclass dataclass, uuids, by_attr=nil
1246 raise ArgumentError, 'Argument is not a data class' unless dataclass.is_a? Class
1247 raise ArgumentError, 'Argument is not an array' unless uuids.is_a? Array
1249 return @objects_for if uuids.empty?
1251 # if already preloaded for all of these uuids, return
1252 if not uuids.select { |x| !@objects_for.include?(x) }.any?
1256 # preset all uuids to nil
1258 @objects_for[x] = nil
1260 if by_attr and ![:uuid, :name].include?(by_attr)
1261 raise ArgumentError, "Preloading only using lookups by uuid or name are supported: #{by_attr}"
1262 elsif by_attr and by_attr == :name
1263 dataclass.where(name: uuids).each do |obj|
1264 @objects_for[obj.name] = obj
1267 key_prefix = "request_#{Thread.current.object_id}_#{dataclass.to_s}_"
1268 dataclass.where(uuid: uuids).each do |obj|
1269 @objects_for[obj.uuid] = obj
1270 if dataclass == Collection
1271 # The collecions#index defaults to "all attributes except manifest_text"
1272 # Hence, this object is not suitable for preloading the find() cache.
1274 Rails.cache.write(key_prefix + obj.uuid, obj.as_json)
1281 # helper method to load objects that are already preloaded
1282 helper_method :load_preloaded_objects
1283 def load_preloaded_objects objs
1286 @objects_for[obj.uuid] = obj
1290 # helper method to get the names of collection files selected
1291 helper_method :selected_collection_files
1292 def selected_collection_files params
1293 link_uuids, coll_ids = params["selection"].partition do |sel_s|
1294 ArvadosBase::resource_class_for_uuid(sel_s) == Link
1297 unless link_uuids.empty?
1298 Link.select([:head_uuid]).where(uuid: link_uuids).with_count("none").each do |link|
1299 if ArvadosBase::resource_class_for_uuid(link.head_uuid) == Collection
1300 coll_ids << link.head_uuid
1307 source_paths = Hash.new { |hash, key| hash[key] = [] }
1308 coll_ids.each do |coll_id|
1309 if m = CollectionsHelper.match(coll_id)
1312 source_paths[key] << m[4]
1313 elsif m = CollectionsHelper.match_uuid_with_optional_filepath(coll_id)
1316 source_paths[key] << m[4]
1321 Collection.where(portable_data_hash: pdhs.uniq).with_count("none").
1322 select([:uuid, :portable_data_hash]).each do |coll|
1323 unless source_paths[coll.portable_data_hash].empty?
1325 source_paths[coll.uuid] = source_paths.delete(coll.portable_data_hash)
1330 [uuids, source_paths]
1333 def wiselinks_layout