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 ActionController::UnknownController,
33 AbstractController::ActionNotFound,
34 with: :render_not_found)
35 rescue_from(Exception,
36 ActionController::UrlGenerationError,
37 with: :render_exception)
41 response.headers["Cache-Control"] = "no-cache, no-store, max-age=0, must-revalidate"
42 response.headers["Pragma"] = "no-cache"
43 response.headers["Expires"] = "Fri, 01 Jan 1990 00:00:00 GMT"
46 def unprocessable(message=nil)
49 @errors << message if message
50 render_error status: 422
53 def render_error(opts={})
54 # Helpers can rely on the presence of @errors to know they're
55 # being used in an error page.
59 # json must come before html here, so it gets used as the
60 # default format when js is requested by the client. This lets
61 # ajax:error callback parse the response correctly, even though
63 f.json { render opts.merge(json: {success: false, errors: @errors}) }
64 f.html { render({action: 'error'}.merge(opts)) }
65 f.all { render({action: 'error', formats: 'text'}.merge(opts)) }
69 def render_exception(e)
70 logger.error e.inspect
71 logger.error e.backtrace.collect { |x| x + "\n" }.join('') if e.backtrace
72 err_opts = {status: 422}
73 if e.is_a?(ArvadosApiClient::ApiError)
74 err_opts.merge!(action: 'api_error', locals: {api_error: e})
75 @errors = e.api_response[:errors]
76 elsif @object.andand.errors.andand.full_messages.andand.any?
77 @errors = @object.errors.full_messages
81 # Make user information available on the error page, falling back to the
82 # session cache if the API server is unavailable.
84 load_api_token(session[:arvados_api_token])
85 rescue ArvadosApiClient::ApiError
86 unless session[:user].nil?
88 Thread.current[:user] = User.new(session[:user])
89 rescue ArvadosApiClient::ApiError
90 # This can happen if User's columns are unavailable. Nothing to do.
94 # Preload projects trees for the template. If that's not doable, set empty
95 # trees so error page rendering can proceed. (It's easier to rescue the
96 # exception here than in a template.)
97 unless current_user.nil?
99 my_starred_projects current_user
100 build_my_wanted_projects_tree current_user
101 rescue ArvadosApiClient::ApiError
102 # Fall back to the default-setting code later.
105 @starred_projects ||= []
106 @my_wanted_projects_tree ||= []
107 render_error(err_opts)
110 def render_not_found(e=ActionController::RoutingError.new("Path not found"))
111 logger.error e.inspect
112 @errors = ["Path not found"]
113 set_thread_api_token do
114 self.render_error(action: '404', status: 404)
120 # The order can be left empty to allow it to default.
121 # Or it can be a comma separated list of real database column names, one per model.
122 # Column names should always be qualified by a table name and a direction is optional, defaulting to asc
123 # (e.g. "collections.name" or "collections.name desc").
124 # If a column name is specified, that table will be sorted by that column.
125 # If there are objects from different models that will be shown (such as in Pipelines and processes tab),
126 # 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")
127 # Currently only one sort column name and direction can be specified for each model.
128 def load_filters_and_paging_params
129 if params[:order].blank?
130 @order = 'created_at desc'
131 elsif params[:order].is_a? Array
132 @order = params[:order]
135 @order = JSON.load(params[:order])
137 @order = params[:order].split(',')
140 @order = [@order] unless @order.is_a? Array
144 @limit = params[:limit].to_i
149 @offset = params[:offset].to_i
154 filters = params[:filters]
155 if filters.is_a? String
156 filters = Oj.load filters
157 elsif filters.is_a? Array
158 filters = filters.collect do |filter|
159 if filter.is_a? String
160 # Accept filters[]=["foo","=","bar"]
163 # Accept filters=[["foo","=","bar"]]
168 # After this, params[:filters] can be trusted to be an array of arrays:
169 params[:filters] = filters
174 def find_objects_for_index
175 @objects ||= model_class
176 @objects = @objects.filter(@filters).limit(@limit).offset(@offset)
177 @objects.fetch_multiple_pages(false)
184 @next_page_href = next_page_href(partial: params[:partial], filters: @filters.to_json)
186 content: render_to_string(partial: "show_#{params[:partial]}",
188 next_page_href: @next_page_href
191 render json: @objects
196 render_pane params[:tab_pane]
205 helper_method :render_pane
206 def render_pane tab_pane, opts={}
208 partial: 'show_' + tab_pane.downcase,
210 comparable: self.respond_to?(:compare),
213 }.merge(opts[:locals] || {})
216 render_to_string render_opts
222 def ensure_arvados_api_exists
223 if model_class.is_a?(Class) && model_class < ArvadosBase && !model_class.api_exists?(params['action'].to_sym)
224 @errors = ["#{params['action']} method is not supported for #{params['controller']}"]
225 return render_error(status: 404)
230 @objects = nil if !defined?(@objects)
231 find_objects_for_index if !@objects
235 helper_method :next_page_offset
236 def next_page_offset objects=nil
240 if objects.respond_to?(:result_offset) and
241 objects.respond_to?(:result_limit)
242 next_offset = objects.result_offset + objects.result_limit
243 if objects.respond_to?(:items_available) and (next_offset < objects.items_available)
245 elsif @objects.results.size > 0 and (params[:count] == 'none' or
246 (params[:controller] == 'search' and params[:action] == 'choose'))
247 last_object_class = @objects.last.class
248 if params['last_object_class'].nil? or params['last_object_class'] == last_object_class.to_s
251 @objects.select{|obj| obj.class == last_object_class}.size
259 helper_method :next_page_href
260 def next_page_href with_params={}
262 url_for with_params.merge(offset: next_page_offset)
266 helper_method :next_page_filters
267 def next_page_filters nextpage_operator
268 next_page_filters = @filters.reject do |attr, op, val|
269 (attr == 'created_at' and op == nextpage_operator) or
270 (attr == 'uuid' and op == 'not in')
274 last_created_at = @objects.last.created_at
277 @objects.each do |obj|
278 last_uuids << obj.uuid if obj.created_at.eql?(last_created_at)
281 next_page_filters += [['created_at', nextpage_operator, last_created_at]]
282 next_page_filters += [['uuid', 'not in', last_uuids]]
290 return render_not_found("object not found")
294 extra_attrs = { href: url_for(action: :show, id: @object) }
295 @object.textile_attributes.each do |textile_attr|
296 extra_attrs.merge!({ "#{textile_attr}Textile" => view_context.render_markup(@object.attributes[textile_attr]) })
298 render json: @object.attributes.merge(extra_attrs)
301 if params['tab_pane']
302 render_pane(if params['tab_pane'].is_a? Hash then params['tab_pane']["name"] else params['tab_pane'] end)
303 elsif request.request_method.in? ['GET', 'HEAD']
306 redirect_to (params[:return_to] ||
307 polymorphic_url(@object,
308 anchor: params[:redirect_to_anchor]))
315 def redirect_to uri, *args
317 if not uri.is_a? String
318 uri = polymorphic_url(uri)
320 render json: {href: uri}
327 @objects = nil if !defined?(@objects)
328 params[:limit] ||= 40
332 find_objects_for_index if !@objects
334 content: render_to_string(partial: "choose_rows.html",
336 next_page_href: next_page_href(partial: params[:partial])
341 find_objects_for_index if !@objects
342 render partial: 'choose', locals: {multiple: params[:multiple]}
349 return render_not_found("object not found")
354 @object = model_class.new
358 @updates ||= params[@object.resource_param_name.to_sym]
359 if @updates.is_a? ActionController::Parameters
360 @updates = @updates.to_unsafe_hash
362 @updates.keys.each do |attr|
363 if @object.send(attr).is_a? Hash
364 if @updates[attr].is_a? String
365 @updates[attr] = Oj.load @updates[attr]
367 if params[:merge] || params["merge_#{attr}".to_sym]
368 # Merge provided Hash with current Hash, instead of
370 if @updates[attr].is_a? ActionController::Parameters
371 @updates[attr] = @updates[attr].to_unsafe_hash
373 @updates[attr] = @object.send(attr).with_indifferent_access.
374 deep_merge(@updates[attr].with_indifferent_access)
378 if @object.update_attributes @updates
381 self.render_error status: 422
386 @new_resource_attrs ||= params[model_class.to_s.underscore.singularize]
387 @new_resource_attrs ||= {}
388 @new_resource_attrs.reject! { |k,v| k.to_s == 'uuid' }
389 @object ||= model_class.new @new_resource_attrs, params["options"]
394 render_error status: 422
398 # Clone the given object, merging any attribute values supplied as
399 # with a create action.
401 @new_resource_attrs ||= params[model_class.to_s.underscore.singularize]
402 @new_resource_attrs ||= {}
403 @object = @object.dup
404 @object.update_attributes @new_resource_attrs
405 if not @new_resource_attrs[:name] and @object.respond_to? :name
406 if @object.name and @object.name != ''
407 @object.name = "Copy of #{@object.name}"
419 f.json { render json: @object }
421 redirect_to(params[:return_to] || :back)
426 self.render_error status: 422
431 Thread.current[:user]
435 controller_name.classify.constantize
438 def breadcrumb_page_name
439 (@breadcrumb_page_name ||
440 (@object.friendly_link_name if @object.respond_to? :friendly_link_name) ||
449 %w(Attributes Advanced)
453 @user_is_manager = false
456 if @object.uuid != current_user.andand.uuid
458 @share_links = Link.permissions_for(@object)
459 @user_is_manager = true
460 rescue ArvadosApiClient::AccessForbiddenException,
461 ArvadosApiClient::NotFoundException
467 if not params[:uuids].andand.any?
468 @errors = ["No user/group UUIDs specified to share with."]
469 return render_error(status: 422)
471 results = {"success" => [], "errors" => []}
472 params[:uuids].each do |shared_uuid|
474 Link.create(tail_uuid: shared_uuid, link_class: "permission",
475 name: "can_read", head_uuid: @object.uuid)
476 rescue ArvadosApiClient::ApiError => error
477 error_list = error.api_response.andand[:errors]
478 if error_list.andand.any?
479 results["errors"] += error_list.map { |e| "#{shared_uuid}: #{e}" }
481 error_code = error.api_status || "Bad status"
482 results["errors"] << "#{shared_uuid}: #{error_code} response"
485 results["success"] << shared_uuid
488 if results["errors"].empty?
489 results.delete("errors")
495 f.json { render(json: results, status: status) }
499 helper_method :is_starred
501 links = Link.where(tail_uuid: current_user.uuid,
502 head_uuid: @object.uuid,
503 link_class: 'star').with_count("none")
505 return links.andand.any?
510 helper_method :strip_token_from_path
511 def strip_token_from_path(path)
512 path.sub(/([\?&;])api_token=[^&;]*[&;]?/, '\1')
515 def redirect_to_login
516 if request.xhr? or request.format.json?
517 @errors = ['You are not logged in. Most likely your session has timed out and you need to log in again.']
518 render_error status: 401
519 elsif request.method.in? ['GET', 'HEAD']
520 redirect_to arvados_api_client.arvados_login_url(return_to: strip_token_from_path(request.url))
522 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."
525 false # For convenience to return from callbacks
528 def using_specific_api_token(api_token, opts={})
530 [:arvados_api_token, :user].each do |key|
531 start_values[key] = Thread.current[key]
533 if opts.fetch(:load_user, true)
534 load_api_token(api_token)
536 Thread.current[:arvados_api_token] = api_token
537 Thread.current[:user] = nil
542 start_values.each_key { |key| Thread.current[key] = start_values[key] }
547 def accept_uuid_as_id_param
548 if params[:id] and params[:id].match(/\D/)
549 params[:uuid] = params.delete :id
553 def find_object_by_uuid
557 elsif params[:uuid].nil? or params[:uuid].empty?
559 elsif not params[:uuid].is_a?(String)
560 @object = model_class.where(uuid: params[:uuid]).first
561 elsif (model_class != Link and
562 resource_class_for_uuid(params[:uuid]) == Link)
563 @name_link = Link.find(params[:uuid])
564 @object = model_class.find(@name_link.head_uuid)
566 @object = model_class.find(params[:uuid])
567 load_preloaded_objects [@object]
569 rescue ArvadosApiClient::NotFoundException, ArvadosApiClient::NotLoggedInException, RuntimeError => error
570 if error.is_a?(RuntimeError) and (error.message !~ /^argument to find\(/)
573 render_not_found(error)
580 Rails.cache.delete_matched(/^request_#{Thread.current.object_id}_/)
582 Rails.cache.delete_matched(/^request_#{Thread.current.object_id}_/)
585 def set_current_request_id
586 response.headers['X-Request-Id'] =
587 Thread.current[:request_id] =
588 "req-" + Random::DEFAULT.rand(2**128).to_s(36)[0..19]
590 Thread.current[:request_id] = nil
593 def append_info_to_payload(payload)
595 payload[:request_id] = response.headers['X-Request-Id']
598 # Set up the thread with the given API token and associated user object.
599 def load_api_token(new_token)
600 Thread.current[:arvados_api_token] = new_token
602 Thread.current[:user] = nil
604 Thread.current[:user] = User.current
608 # If there's a valid api_token parameter, set up the session with that
609 # user's information. Return true if the method redirects the request
610 # (usually a post-login redirect); false otherwise.
611 def setup_user_session
612 return false unless params[:api_token]
613 Thread.current[:arvados_api_token] = params[:api_token]
616 rescue ArvadosApiClient::NotLoggedInException
617 false # We may redirect to login, or not, based on the current action.
619 session[:arvados_api_token] = params[:api_token]
620 # If we later have trouble contacting the API server, we still want
621 # to be able to render basic user information in the UI--see
622 # render_exception above. We store that in the session here. This is
623 # not intended to be used as a general-purpose cache. See #2891.
627 first_name: user.first_name,
628 last_name: user.last_name,
629 is_active: user.is_active,
630 is_admin: user.is_admin,
634 if !request.format.json? and request.method.in? ['GET', 'HEAD']
635 # Repeat this request with api_token in the (new) session
636 # cookie instead of the query string. This prevents API
637 # tokens from appearing in (and being inadvisedly copied
638 # and pasted from) browser Location bars.
639 redirect_to strip_token_from_path(request.fullpath)
645 Thread.current[:arvados_api_token] = nil
649 # Save the session API token in thread-local storage, and yield.
650 # This method also takes care of session setup if the request
651 # provides a valid api_token parameter.
652 # If a token is unavailable or expired, the block is still run, with
654 def set_thread_api_token
655 if Thread.current[:arvados_api_token]
656 yield # An API token has already been found - pass it through.
658 elsif setup_user_session
659 return # A new session was set up and received a response.
663 load_api_token(session[:arvados_api_token])
665 rescue ArvadosApiClient::NotLoggedInException
666 # If we got this error with a token, it must've expired.
667 # Retry the request without a token.
668 unless Thread.current[:arvados_api_token].nil?
673 # Remove token in case this Thread is used for anything else.
678 # Redirect to login/welcome if client provided expired API token (or
680 def require_thread_api_token
681 if Thread.current[:arvados_api_token]
683 elsif session[:arvados_api_token]
684 # Expired session. Clear it before refreshing login so that,
685 # if this login procedure fails, we end up showing the "please
686 # log in" page instead of getting stuck in a redirect loop.
687 session.delete :arvados_api_token
690 # If we redirect to the welcome page, the browser will handle
691 # the 302 by itself and the client code will end up rendering
692 # the "welcome" page in some content area where it doesn't make
693 # sense. Instead, we send 401 ("authenticate and try again" or
694 # "display error", depending on how smart the client side is).
695 @errors = ['You are not logged in.']
696 render_error status: 401
698 redirect_to welcome_users_path(return_to: request.fullpath)
702 def ensure_current_user_is_admin
704 @errors = ['Not logged in']
705 render_error status: 401
706 elsif not current_user.is_admin
707 @errors = ['Permission denied']
708 render_error status: 403
712 helper_method :unsigned_user_agreements
713 def unsigned_user_agreements
714 @signed_ua_uuids ||= UserAgreement.signatures.map &:head_uuid
715 @unsigned_user_agreements ||= UserAgreement.all.map do |ua|
716 if not @signed_ua_uuids.index ua.uuid
717 Collection.find(ua.uuid)
722 def check_user_agreements
723 if current_user && !current_user.is_active
724 if not current_user.is_invited
725 return redirect_to inactive_users_path(return_to: request.fullpath)
727 if unsigned_user_agreements.empty?
728 # No agreements to sign. Perhaps we just need to ask?
729 current_user.activate
730 if !current_user.is_active
731 logger.warn "#{current_user.uuid.inspect}: " +
732 "No user agreements to sign, but activate failed!"
735 if !current_user.is_active
736 redirect_to user_agreements_path(return_to: request.fullpath)
742 def check_user_profile
743 return true if !current_user
744 if request.method.downcase != 'get' || params[:partial] ||
745 params[:tab_pane] || params[:action_method] ||
746 params[:action] == 'setup_popup'
750 if missing_required_profile?
751 redirect_to profile_user_path(current_user.uuid, return_to: request.fullpath)
756 helper_method :missing_required_profile?
757 def missing_required_profile?
758 missing_required = false
760 profile_config = Rails.configuration.Workbench.UserProfileFormFields
761 if current_user && !profile_config.empty?
762 current_user_profile = current_user.prefs[:profile]
763 profile_config.each do |k, entry|
765 if !current_user_profile ||
766 !current_user_profile[k] ||
767 current_user_profile[k].empty?
768 missing_required = true
779 return Rails.configuration.Workbench.Theme
782 @@notification_tests = []
784 @@notification_tests.push lambda { |controller, current_user|
785 return nil if Rails.configuration.Services.WebShell.ExternalURL != URI("")
786 AuthorizedKey.limit(1).with_count('none').where(authorized_user_uuid: current_user.uuid).each do
789 return lambda { |view|
790 view.render partial: 'notifications/ssh_key_notification'
794 @@notification_tests.push lambda { |controller, current_user|
795 Collection.limit(1).with_count('none').where(created_by: current_user.uuid).each do
798 return lambda { |view|
799 view.render partial: 'notifications/collections_notification'
803 @@notification_tests.push lambda { |controller, current_user|
804 if PipelineInstance.api_exists?(:index)
805 PipelineInstance.limit(1).with_count('none').where(created_by: current_user.uuid).each do
811 return lambda { |view|
812 view.render partial: 'notifications/pipelines_notification'
816 helper_method :user_notifications
817 def user_notifications
818 @errors = nil if !defined?(@errors)
819 return [] if @errors or not current_user.andand.is_active or not Rails.configuration.Workbench.ShowUserNotifications
820 @notifications ||= @@notification_tests.map do |t|
821 t.call(self, current_user)
825 helper_method :all_projects
827 @all_projects ||= Group.
828 filter([['group_class','=','project']]).order('name')
831 helper_method :my_projects
833 return @my_projects if @my_projects
836 all_projects.each do |g|
837 root_of[g.uuid] = g.owner_uuid
843 root_of = root_of.each_with_object({}) do |(child, parent), h|
845 h[child] = root_of[parent]
852 @my_projects = @my_projects.select do |g|
853 root_of[g.uuid] == current_user.uuid
857 helper_method :projects_shared_with_me
858 def projects_shared_with_me
859 my_project_uuids = my_projects.collect &:uuid
860 all_projects.reject { |x| x.uuid.in? my_project_uuids }
863 helper_method :recent_jobs_and_pipelines
864 def recent_jobs_and_pipelines
866 PipelineInstance.limit(10).with_count("none")).
868 (x.finished_at || x.started_at rescue nil) || x.modified_at || x.created_at
872 helper_method :running_pipelines
873 def running_pipelines
874 pi = PipelineInstance.order(["started_at asc", "created_at asc"]).with_count("none").filter([["state", "in", ["RunningOnServer", "RunningOnClient"]]])
877 pl.components.each do |k,v|
878 if v.is_a? Hash and v[:job]
879 jobs[v[:job][:uuid]] = {}
885 Job.filter([["uuid", "in", jobs.keys]]).with_count("none").each do |j|
890 pl.components.each do |k,v|
891 if v.is_a? Hash and v[:job]
892 v[:job] = jobs[v[:job][:uuid]]
901 helper_method :recent_processes
902 def recent_processes lim
906 if PipelineInstance.api_exists?(:index)
907 cols = %w(uuid owner_uuid created_at modified_at pipeline_template_uuid name state started_at finished_at)
908 pipelines = PipelineInstance.select(cols).limit(lim).order(["created_at desc"]).with_count("none")
909 pipelines.results.each { |pi| procs[pi] = pi.created_at }
912 crs = ContainerRequest.limit(lim).with_count("none").order(["created_at desc"]).filter([["requesting_container_uuid", "=", nil]])
913 crs.results.each { |c| procs[c] = c.created_at }
915 Hash[procs.sort_by {|key, value| value}].keys.reverse.first(lim)
918 helper_method :recent_collections
919 def recent_collections lim
920 c = Collection.limit(lim).with_count("none").order(["modified_at desc"]).results
922 Group.filter([["uuid", "in", c.map(&:owner_uuid)]]).with_count("none").each do |g|
925 {collections: c, owners: own}
928 helper_method :my_starred_projects
929 def my_starred_projects user
930 return if defined?(@starred_projects) && @starred_projects
931 links = Link.filter([['owner_uuid', 'in', ["#{Rails.configuration.ClusterID}-j7d0g-fffffffffffffff", user.uuid]],
932 ['link_class', '=', 'star'],
933 ['head_uuid', 'is_a', 'arvados#group']]).with_count("none").select(%w(head_uuid))
934 uuids = links.collect { |x| x.head_uuid }
935 starred_projects = Group.filter([['uuid', 'in', uuids]]).order('name').with_count("none")
936 @starred_projects = starred_projects.results
939 # If there are more than 200 projects that are readable by the user,
940 # build the tree using only the top 200+ projects owned by the user,
941 # from the top three levels.
942 # That is: get toplevel projects under home, get subprojects of
943 # these projects, and so on until we hit the limit.
944 def my_wanted_projects(user, page_size=100)
945 return @my_wanted_projects if defined?(@my_wanted_projects) && @my_wanted_projects
950 @too_many_projects = false
951 @reached_level_limit = false
952 while from_top.size <= page_size*2
953 current_level = Group.filter([['group_class','=','project'],
954 ['owner_uuid', 'in', uuids]])
955 .order('name').limit(page_size*2)
956 break if current_level.results.size == 0
957 @too_many_projects = true if current_level.items_available > current_level.results.size
958 from_top.concat current_level.results
959 uuids = current_level.results.collect(&:uuid)
962 @reached_level_limit = true
966 @my_wanted_projects = from_top
969 helper_method :my_wanted_projects_tree
970 def my_wanted_projects_tree(user, page_size=100)
971 build_my_wanted_projects_tree(user, page_size)
972 [@my_wanted_projects_tree, @too_many_projects, @reached_level_limit]
975 def build_my_wanted_projects_tree(user, page_size=100)
976 return @my_wanted_projects_tree if defined?(@my_wanted_projects_tree) && @my_wanted_projects_tree
978 parent_of = {user.uuid => 'me'}
979 my_wanted_projects(user, page_size).each do |ob|
980 parent_of[ob.uuid] = ob.owner_uuid
982 children_of = {false => [], 'me' => [user]}
983 my_wanted_projects(user, page_size).each do |ob|
984 if ob.owner_uuid != user.uuid and
985 not parent_of.has_key? ob.owner_uuid
986 parent_of[ob.uuid] = false
988 children_of[parent_of[ob.uuid]] ||= []
989 children_of[parent_of[ob.uuid]] << ob
991 buildtree = lambda do |chldrn_of, root_uuid=false|
993 chldrn_of[root_uuid].andand.each do |ob|
994 tree[ob] = buildtree.call(chldrn_of, ob.uuid)
998 sorted_paths = lambda do |tree, depth=0|
1000 tree.keys.sort_by { |ob|
1001 ob.is_a?(String) ? ob : ob.friendly_link_name
1003 paths << {object: ob, depth: depth}
1004 paths += sorted_paths.call tree[ob], depth+1
1008 @my_wanted_projects_tree =
1009 sorted_paths.call buildtree.call(children_of, 'me')
1012 helper_method :get_object
1014 if @get_object.nil? and @objects
1015 @get_object = @objects.each_with_object({}) do |object, h|
1016 h[object.uuid] = object
1023 helper_method :project_breadcrumbs
1024 def project_breadcrumbs
1026 current = @name_link || @object
1028 # Halt if a group ownership loop is detected. API should refuse
1029 # to produce this state, but it could still arise from a race
1030 # condition when group ownership changes between our find()
1032 break if crumbs.collect(&:uuid).include? current.uuid
1034 if current.is_a?(Group) and current.group_class == 'project'
1035 crumbs.prepend current
1037 if current.is_a? Link
1038 current = Group.find?(current.tail_uuid)
1040 current = Group.find?(current.owner_uuid)
1046 helper_method :current_project_uuid
1047 def current_project_uuid
1048 if @object.is_a? Group and @object.group_class == 'project'
1050 elsif @name_link.andand.tail_uuid
1051 @name_link.tail_uuid
1052 elsif @object and resource_class_for_uuid(@object.owner_uuid) == Group
1059 # helper method to get links for given object or uuid
1060 helper_method :links_for_object
1061 def links_for_object object_or_uuid
1062 raise ArgumentError, 'No input argument' unless object_or_uuid
1063 preload_links_for_objects([object_or_uuid])
1064 uuid = object_or_uuid.is_a?(String) ? object_or_uuid : object_or_uuid.uuid
1065 @all_links_for[uuid] ||= []
1068 # helper method to preload links for given objects and uuids
1069 helper_method :preload_links_for_objects
1070 def preload_links_for_objects objects_and_uuids
1071 @all_links_for ||= {}
1073 raise ArgumentError, 'Argument is not an array' unless objects_and_uuids.is_a? Array
1074 return @all_links_for if objects_and_uuids.empty?
1076 uuids = objects_and_uuids.collect { |x| x.is_a?(String) ? x : x.uuid }
1078 # if already preloaded for all of these uuids, return
1079 if not uuids.select { |x| @all_links_for[x].nil? }.any?
1080 return @all_links_for
1084 @all_links_for[x] = []
1087 # TODO: make sure we get every page of results from API server
1088 Link.filter([['head_uuid', 'in', uuids]]).with_count("none").each do |link|
1089 @all_links_for[link.head_uuid] << link
1094 # helper method to get a certain number of objects of a specific type
1095 # this can be used to replace any uses of: "dataclass.limit(n)"
1096 helper_method :get_n_objects_of_class
1097 def get_n_objects_of_class dataclass, size
1098 @objects_map_for ||= {}
1100 raise ArgumentError, 'Argument is not a data class' unless dataclass.is_a? Class and dataclass < ArvadosBase
1101 raise ArgumentError, 'Argument is not a valid limit size' unless (size && size>0)
1103 # if the objects_map_for has a value for this dataclass, and the
1104 # size used to retrieve those objects is equal, return it
1105 size_key = "#{dataclass.name}_size"
1106 if @objects_map_for[dataclass.name] && @objects_map_for[size_key] &&
1107 (@objects_map_for[size_key] == size)
1108 return @objects_map_for[dataclass.name]
1111 @objects_map_for[size_key] = size
1112 @objects_map_for[dataclass.name] = dataclass.limit(size)
1115 # helper method to get collections for the given uuid
1116 helper_method :collections_for_object
1117 def collections_for_object uuid
1118 raise ArgumentError, 'No input argument' unless uuid
1119 preload_collections_for_objects([uuid])
1120 @all_collections_for[uuid] ||= []
1123 # helper method to preload collections for the given uuids
1124 helper_method :preload_collections_for_objects
1125 def preload_collections_for_objects uuids
1126 @all_collections_for ||= {}
1128 raise ArgumentError, 'Argument is not an array' unless uuids.is_a? Array
1129 return @all_collections_for if uuids.empty?
1131 # if already preloaded for all of these uuids, return
1132 if not uuids.select { |x| @all_collections_for[x].nil? }.any?
1133 return @all_collections_for
1137 @all_collections_for[x] = []
1140 # TODO: make sure we get every page of results from API server
1141 Collection.where(uuid: uuids).with_count("none").each do |collection|
1142 @all_collections_for[collection.uuid] << collection
1144 @all_collections_for
1147 # helper method to get log collections for the given log
1148 helper_method :log_collections_for_object
1149 def log_collections_for_object log
1150 raise ArgumentError, 'No input argument' unless log
1152 preload_log_collections_for_objects([log])
1155 fixup = /([a-f0-9]{32}\+\d+)(\+?.*)/.match(log)
1156 if fixup && fixup.size>1
1160 @all_log_collections_for[uuid] ||= []
1163 # helper method to preload collections for the given uuids
1164 helper_method :preload_log_collections_for_objects
1165 def preload_log_collections_for_objects logs
1166 @all_log_collections_for ||= {}
1168 raise ArgumentError, 'Argument is not an array' unless logs.is_a? Array
1169 return @all_log_collections_for if logs.empty?
1173 fixup = /([a-f0-9]{32}\+\d+)(\+?.*)/.match(log)
1174 if fixup && fixup.size>1
1181 # if already preloaded for all of these uuids, return
1182 if not uuids.select { |x| @all_log_collections_for[x].nil? }.any?
1183 return @all_log_collections_for
1187 @all_log_collections_for[x] = []
1190 # TODO: make sure we get every page of results from API server
1191 Collection.where(uuid: uuids).with_count("none").each do |collection|
1192 @all_log_collections_for[collection.uuid] << collection
1194 @all_log_collections_for
1197 # Helper method to get one collection for the given portable_data_hash
1198 # This is used to determine if a pdh is readable by the current_user
1199 helper_method :collection_for_pdh
1200 def collection_for_pdh pdh
1201 raise ArgumentError, 'No input argument' unless pdh
1202 preload_for_pdhs([pdh])
1203 @all_pdhs_for[pdh] ||= []
1206 # Helper method to preload one collection each for the given pdhs
1207 # This is used to determine if a pdh is readable by the current_user
1208 helper_method :preload_for_pdhs
1209 def preload_for_pdhs pdhs
1210 @all_pdhs_for ||= {}
1212 raise ArgumentError, 'Argument is not an array' unless pdhs.is_a? Array
1213 return @all_pdhs_for if pdhs.empty?
1215 # if already preloaded for all of these pdhs, return
1216 if not pdhs.select { |x| @all_pdhs_for[x].nil? }.any?
1217 return @all_pdhs_for
1221 @all_pdhs_for[x] = []
1224 Collection.select(%w(portable_data_hash)).where(portable_data_hash: pdhs).distinct().with_count("none").each do |collection|
1225 @all_pdhs_for[collection.portable_data_hash] << collection
1230 # helper method to get object of a given dataclass and uuid
1231 helper_method :object_for_dataclass
1232 def object_for_dataclass dataclass, uuid, by_attr=nil
1233 raise ArgumentError, 'No input argument dataclass' unless (dataclass && uuid)
1234 preload_objects_for_dataclass(dataclass, [uuid], by_attr)
1238 # helper method to preload objects for given dataclass and uuids
1239 helper_method :preload_objects_for_dataclass
1240 def preload_objects_for_dataclass dataclass, uuids, by_attr=nil
1243 raise ArgumentError, 'Argument is not a data class' unless dataclass.is_a? Class
1244 raise ArgumentError, 'Argument is not an array' unless uuids.is_a? Array
1246 return @objects_for if uuids.empty?
1248 # if already preloaded for all of these uuids, return
1249 if not uuids.select { |x| !@objects_for.include?(x) }.any?
1253 # preset all uuids to nil
1255 @objects_for[x] = nil
1257 if by_attr and ![:uuid, :name].include?(by_attr)
1258 raise ArgumentError, "Preloading only using lookups by uuid or name are supported: #{by_attr}"
1259 elsif by_attr and by_attr == :name
1260 dataclass.where(name: uuids).each do |obj|
1261 @objects_for[obj.name] = obj
1264 key_prefix = "request_#{Thread.current.object_id}_#{dataclass.to_s}_"
1265 dataclass.where(uuid: uuids).each do |obj|
1266 @objects_for[obj.uuid] = obj
1267 if dataclass == Collection
1268 # The collecions#index defaults to "all attributes except manifest_text"
1269 # Hence, this object is not suitable for preloading the find() cache.
1271 Rails.cache.write(key_prefix + obj.uuid, obj.as_json)
1278 # helper method to load objects that are already preloaded
1279 helper_method :load_preloaded_objects
1280 def load_preloaded_objects objs
1283 @objects_for[obj.uuid] = obj
1287 # helper method to get the names of collection files selected
1288 helper_method :selected_collection_files
1289 def selected_collection_files params
1290 link_uuids, coll_ids = params["selection"].partition do |sel_s|
1291 ArvadosBase::resource_class_for_uuid(sel_s) == Link
1294 unless link_uuids.empty?
1295 Link.select([:head_uuid]).where(uuid: link_uuids).with_count("none").each do |link|
1296 if ArvadosBase::resource_class_for_uuid(link.head_uuid) == Collection
1297 coll_ids << link.head_uuid
1304 source_paths = Hash.new { |hash, key| hash[key] = [] }
1305 coll_ids.each do |coll_id|
1306 if m = CollectionsHelper.match(coll_id)
1309 source_paths[key] << m[4]
1310 elsif m = CollectionsHelper.match_uuid_with_optional_filepath(coll_id)
1313 source_paths[key] << m[4]
1318 Collection.where(portable_data_hash: pdhs.uniq).with_count("none").
1319 select([:uuid, :portable_data_hash]).each do |coll|
1320 unless source_paths[coll.portable_data_hash].empty?
1322 source_paths[coll.uuid] = source_paths.delete(coll.portable_data_hash)
1327 [uuids, source_paths]
1330 def wiselinks_layout