19129: Select specific fields to speed up dashboard loading
[arvados.git] / apps / workbench / app / controllers / application_controller.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 class ApplicationController < ActionController::Base
6   include ArvadosApiClientHelper
7   include ApplicationHelper
8
9   respond_to :html, :json, :js
10   protect_from_forgery
11
12   ERROR_ACTIONS = [:render_error, :render_not_found]
13
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
27   theme :select_theme
28
29   begin
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)
37   end
38
39   def set_cache_buster
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"
43   end
44
45   def unprocessable(message=nil)
46     @errors ||= []
47
48     @errors << message if message
49     render_error status: 422
50   end
51
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.
55     @errors ||= []
56     opts[:status] ||= 500
57     respond_to do |f|
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
61       # the browser can't.
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)) }
65     end
66   end
67
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
77     else
78       @errors = [e.to_s]
79     end
80     # Make user information available on the error page, falling back to the
81     # session cache if the API server is unavailable.
82     begin
83       load_api_token(session[:arvados_api_token])
84     rescue ArvadosApiClient::ApiError
85       unless session[:user].nil?
86         begin
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.
90         end
91       end
92     end
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?
97       begin
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.
102       end
103     end
104     @starred_projects ||= []
105     @my_wanted_projects_tree ||= []
106     render_error(err_opts)
107   end
108
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)
114     end
115   end
116
117   # params[:order]:
118   #
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]
132     else
133       begin
134         @order = JSON.load(params[:order])
135       rescue
136         @order = params[:order].split(',')
137       end
138     end
139     @order = [@order] unless @order.is_a? Array
140
141     @limit ||= 200
142     if params[:limit]
143       @limit = params[:limit].to_i
144     end
145
146     @offset ||= 0
147     if params[:offset]
148       @offset = params[:offset].to_i
149     end
150
151     @filters ||= []
152     if params[:filters]
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"]
160             Oj.load filter
161           else
162             # Accept filters=[["foo","=","bar"]]
163             filter
164           end
165         end
166       end
167       # After this, params[:filters] can be trusted to be an array of arrays:
168       params[:filters] = filters
169       @filters += filters
170     end
171   end
172
173   def find_objects_for_index
174     @objects ||= model_class
175     @objects = @objects.filter(@filters).limit(@limit).offset(@offset).order(@order)
176     @objects.fetch_multiple_pages(false)
177   end
178
179   def render_index
180     respond_to do |f|
181       f.json {
182         if params[:partial]
183           @next_page_href = next_page_href(partial: params[:partial], filters: @filters.to_json)
184           render json: {
185             content: render_to_string(partial: "show_#{params[:partial]}",
186                                       formats: [:html]),
187             next_page_href: @next_page_href
188           }
189         else
190           render json: @objects
191         end
192       }
193       f.html {
194         if params[:tab_pane]
195           render_pane params[:tab_pane]
196         else
197           render
198         end
199       }
200       f.js { render }
201     end
202   end
203
204   helper_method :render_pane
205   def render_pane tab_pane, opts={}
206     render_opts = {
207       partial: 'show_' + tab_pane.downcase,
208       locals: {
209         comparable: self.respond_to?(:compare),
210         objects: @objects,
211         tab_pane: tab_pane
212       }.merge(opts[:locals] || {})
213     }
214     if opts[:to_string]
215       render_to_string render_opts
216     else
217       render render_opts
218     end
219   end
220
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)
225     end
226   end
227
228   def index
229     @objects = nil if !defined?(@objects)
230     find_objects_for_index if !@objects
231     render_index
232   end
233
234   helper_method :next_page_offset
235   def next_page_offset objects=nil
236     if !objects
237       objects = @objects
238     end
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)
243         next_offset
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
248           next_offset
249         else
250           @objects.select{|obj| obj.class == last_object_class}.size
251         end
252       else
253         nil
254       end
255     end
256   end
257
258   helper_method :next_page_href
259   def next_page_href with_params={}
260     if next_page_offset
261       url_for with_params.merge(offset: next_page_offset)
262     end
263   end
264
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')
270     end
271
272     if @objects.any?
273       last_created_at = @objects.last.created_at
274
275       last_uuids = []
276       @objects.each do |obj|
277         last_uuids << obj.uuid if obj.created_at.eql?(last_created_at)
278       end
279
280       next_page_filters += [['created_at', nextpage_operator, last_created_at]]
281       next_page_filters += [['uuid', 'not in', last_uuids]]
282     end
283
284     next_page_filters
285   end
286
287   def show
288     if !@object
289       return render_not_found("object not found")
290     end
291     respond_to do |f|
292       f.json do
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]) })
296         end
297         render json: @object.attributes.merge(extra_attrs)
298       end
299       f.html {
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']
303           render
304         else
305           redirect_to (params[:return_to] ||
306                        polymorphic_url(@object,
307                                        anchor: params[:redirect_to_anchor]))
308         end
309       }
310       f.js { render }
311     end
312   end
313
314   def redirect_to uri, *args
315     if request.xhr?
316       if not uri.is_a? String
317         uri = polymorphic_url(uri)
318       end
319       render json: {href: uri}
320     else
321       super
322     end
323   end
324
325   def choose
326     @objects = nil if !defined?(@objects)
327     params[:limit] ||= 40
328     respond_to do |f|
329       if params[:partial]
330         f.json {
331           find_objects_for_index if !@objects
332           render json: {
333             content: render_to_string(partial: "choose_rows.html",
334                                       formats: [:html]),
335             next_page_href: next_page_href(partial: params[:partial])
336           }
337         }
338       end
339       f.js {
340         find_objects_for_index if !@objects
341         render partial: 'choose', locals: {multiple: params[:multiple]}
342       }
343     end
344   end
345
346   def render_content
347     if !@object
348       return render_not_found("object not found")
349     end
350   end
351
352   def new
353     @object = model_class.new
354   end
355
356   def update
357     @updates ||= params[@object.resource_param_name.to_sym]
358     if @updates.is_a? ActionController::Parameters
359       @updates = @updates.to_unsafe_hash
360     end
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]
365         end
366         if params[:merge] || params["merge_#{attr}".to_sym]
367           # Merge provided Hash with current Hash, instead of
368           # replacing.
369           if @updates[attr].is_a? ActionController::Parameters
370             @updates[attr] = @updates[attr].to_unsafe_hash
371           end
372           @updates[attr] = @object.send(attr).with_indifferent_access.
373             deep_merge(@updates[attr].with_indifferent_access)
374         end
375       end
376     end
377     if @object.update_attributes @updates
378       show
379     else
380       self.render_error status: 422
381     end
382   end
383
384   def create
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"]
389
390     if @object.save
391       show
392     else
393       render_error status: 422
394     end
395   end
396
397   # Clone the given object, merging any attribute values supplied as
398   # with a create action.
399   def copy
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}"
407       else
408         @object.name = ""
409       end
410     end
411     @object.save!
412     show
413   end
414
415   def destroy
416     if @object.destroy
417       respond_to do |f|
418         f.json { render json: @object }
419         f.html {
420           if params[:return_to]
421             redirect_to(params[:return_to])
422           else
423             redirect_back(fallback_location: root_path)
424           end
425         }
426         f.js { render }
427       end
428     else
429       self.render_error status: 422
430     end
431   end
432
433   def current_user
434     Thread.current[:user]
435   end
436
437   def model_class
438     controller_name.classify.constantize
439   end
440
441   def breadcrumb_page_name
442     (@breadcrumb_page_name ||
443      (@object.friendly_link_name if @object.respond_to? :friendly_link_name) ||
444      action_name)
445   end
446
447   def index_pane_list
448     %w(Recent)
449   end
450
451   def show_pane_list
452     %w(Attributes Advanced)
453   end
454
455   def set_share_links
456     @user_is_manager = false
457     @share_links = []
458
459     if @object.uuid != current_user.andand.uuid
460       begin
461         @share_links = Link.permissions_for(@object)
462         @user_is_manager = true
463       rescue ArvadosApiClient::AccessForbiddenException,
464         ArvadosApiClient::NotFoundException
465       end
466     end
467   end
468
469   def share_with
470     if not params[:uuids].andand.any?
471       @errors = ["No user/group UUIDs specified to share with."]
472       return render_error(status: 422)
473     end
474     results = {"success" => [], "errors" => []}
475     params[:uuids].each do |shared_uuid|
476       begin
477         Link.create(tail_uuid: shared_uuid, link_class: "permission",
478                     name: "can_read", head_uuid: @object.uuid)
479       rescue ArvadosApiClient::ApiError => error
480         error_list = error.api_response.andand[:errors]
481         if error_list.andand.any?
482           results["errors"] += error_list.map { |e| "#{shared_uuid}: #{e}" }
483         else
484           error_code = error.api_status || "Bad status"
485           results["errors"] << "#{shared_uuid}: #{error_code} response"
486         end
487       else
488         results["success"] << shared_uuid
489       end
490     end
491     if results["errors"].empty?
492       results.delete("errors")
493       status = 200
494     else
495       status = 422
496     end
497     respond_to do |f|
498       f.json { render(json: results, status: status) }
499     end
500   end
501
502   helper_method :is_starred
503   def is_starred
504     links = Link.where(tail_uuid: current_user.uuid,
505                head_uuid: @object.uuid,
506                link_class: 'star').with_count("none")
507
508     return links.andand.any?
509   end
510
511   protected
512
513   helper_method :strip_token_from_path
514   def strip_token_from_path(path)
515     path.sub(/([\?&;])api_token=[^&;]*[&;]?/, '\1')
516   end
517
518   def redirect_to_login
519     if request.xhr? or request.format.json?
520       @errors = ['You are not logged in. Most likely your session has timed out and you need to log in again.']
521       render_error status: 401
522     elsif request.method.in? ['GET', 'HEAD']
523       redirect_to arvados_api_client.arvados_login_url(return_to: strip_token_from_path(request.url))
524     else
525       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."
526       redirect_back(fallback_location: root_path)
527     end
528     false  # For convenience to return from callbacks
529   end
530
531   def using_specific_api_token(api_token, opts={})
532     start_values = {}
533     [:arvados_api_token, :user].each do |key|
534       start_values[key] = Thread.current[key]
535     end
536     if opts.fetch(:load_user, true)
537       load_api_token(api_token)
538     else
539       Thread.current[:arvados_api_token] = api_token
540       Thread.current[:user] = nil
541     end
542     begin
543       yield
544     ensure
545       start_values.each_key { |key| Thread.current[key] = start_values[key] }
546     end
547   end
548
549
550   def accept_uuid_as_id_param
551     if params[:id] and params[:id].match(/\D/)
552       params[:uuid] = params.delete :id
553     end
554   end
555
556   def find_object_by_uuid
557     begin
558       if not model_class
559         @object = nil
560       elsif params[:uuid].nil? or params[:uuid].empty?
561         @object = nil
562       elsif not params[:uuid].is_a?(String)
563         @object = model_class.where(uuid: params[:uuid]).first
564       elsif (model_class != Link and
565              resource_class_for_uuid(params[:uuid]) == Link)
566         @name_link = Link.find(params[:uuid])
567         @object = model_class.find(@name_link.head_uuid)
568       else
569         @object = model_class.find(params[:uuid])
570         load_preloaded_objects [@object]
571       end
572     rescue ArvadosApiClient::NotFoundException, ArvadosApiClient::NotLoggedInException, RuntimeError => error
573       if error.is_a?(RuntimeError) and (error.message !~ /^argument to find\(/)
574         raise
575       end
576       render_not_found(error)
577       return false
578     end
579   end
580
581   def thread_clear
582     load_api_token(nil)
583     Rails.cache.delete_matched(/^request_#{Thread.current.object_id}_/)
584     yield
585     Rails.cache.delete_matched(/^request_#{Thread.current.object_id}_/)
586   end
587
588   def set_current_request_id
589     response.headers['X-Request-Id'] =
590       Thread.current[:request_id] =
591       "req-" + Random::DEFAULT.rand(2**128).to_s(36)[0..19]
592     yield
593     Thread.current[:request_id] = nil
594   end
595
596   def append_info_to_payload(payload)
597     super
598     payload[:request_id] = response.headers['X-Request-Id']
599   end
600
601   # Set up the thread with the given API token and associated user object.
602   def load_api_token(new_token)
603     Thread.current[:arvados_api_token] = new_token
604     if new_token.nil?
605       Thread.current[:user] = nil
606     else
607       Thread.current[:user] = User.current
608     end
609   end
610
611   # If there's a valid api_token parameter, set up the session with that
612   # user's information.  Return true if the method redirects the request
613   # (usually a post-login redirect); false otherwise.
614   def setup_user_session
615     return false unless params[:api_token]
616     Thread.current[:arvados_api_token] = params[:api_token]
617     begin
618       user = User.current
619     rescue ArvadosApiClient::NotLoggedInException
620       false  # We may redirect to login, or not, based on the current action.
621     else
622       session[:arvados_api_token] = params[:api_token]
623       # If we later have trouble contacting the API server, we still want
624       # to be able to render basic user information in the UI--see
625       # render_exception above.  We store that in the session here.  This is
626       # not intended to be used as a general-purpose cache.  See #2891.
627       session[:user] = {
628         uuid: user.uuid,
629         email: user.email,
630         first_name: user.first_name,
631         last_name: user.last_name,
632         is_active: user.is_active,
633         is_admin: user.is_admin,
634         prefs: user.prefs
635       }
636
637       if !request.format.json? and request.method.in? ['GET', 'HEAD']
638         # Repeat this request with api_token in the (new) session
639         # cookie instead of the query string.  This prevents API
640         # tokens from appearing in (and being inadvisedly copied
641         # and pasted from) browser Location bars.
642         redirect_to strip_token_from_path(request.fullpath)
643         true
644       else
645         false
646       end
647     ensure
648       Thread.current[:arvados_api_token] = nil
649     end
650   end
651
652   # Save the session API token in thread-local storage, and yield.
653   # This method also takes care of session setup if the request
654   # provides a valid api_token parameter.
655   # If a token is unavailable or expired, the block is still run, with
656   # a nil token.
657   def set_thread_api_token
658     if Thread.current[:arvados_api_token]
659       yield   # An API token has already been found - pass it through.
660       return
661     elsif setup_user_session
662       return  # A new session was set up and received a response.
663     end
664
665     begin
666       load_api_token(session[:arvados_api_token])
667       yield
668     rescue ArvadosApiClient::NotLoggedInException
669       # If we got this error with a token, it must've expired.
670       # Retry the request without a token.
671       unless Thread.current[:arvados_api_token].nil?
672         load_api_token(nil)
673         yield
674       end
675     ensure
676       # Remove token in case this Thread is used for anything else.
677       load_api_token(nil)
678     end
679   end
680
681   # Redirect to login/welcome if client provided expired API token (or
682   # none at all)
683   def require_thread_api_token
684     if Thread.current[:arvados_api_token]
685       yield
686     elsif session[:arvados_api_token]
687       # Expired session. Clear it before refreshing login so that,
688       # if this login procedure fails, we end up showing the "please
689       # log in" page instead of getting stuck in a redirect loop.
690       session.delete :arvados_api_token
691       redirect_to_login
692     elsif request.xhr?
693       # If we redirect to the welcome page, the browser will handle
694       # the 302 by itself and the client code will end up rendering
695       # the "welcome" page in some content area where it doesn't make
696       # sense. Instead, we send 401 ("authenticate and try again" or
697       # "display error", depending on how smart the client side is).
698       @errors = ['You are not logged in.']
699       render_error status: 401
700     else
701       redirect_to welcome_users_path(return_to: request.fullpath)
702     end
703   end
704
705   def ensure_current_user_is_admin
706     if not current_user
707       @errors = ['Not logged in']
708       render_error status: 401
709     elsif not current_user.is_admin
710       @errors = ['Permission denied']
711       render_error status: 403
712     end
713   end
714
715   helper_method :unsigned_user_agreements
716   def unsigned_user_agreements
717     @signed_ua_uuids ||= UserAgreement.signatures.map &:head_uuid
718     @unsigned_user_agreements ||= UserAgreement.all.map do |ua|
719       if not @signed_ua_uuids.index ua.uuid
720         Collection.find(ua.uuid)
721       end
722     end.compact
723   end
724
725   def check_user_agreements
726     if current_user && !current_user.is_active
727       if not current_user.is_invited
728         return redirect_to inactive_users_path(return_to: request.fullpath)
729       end
730       if unsigned_user_agreements.empty?
731         # No agreements to sign. Perhaps we just need to ask?
732         current_user.activate
733         if !current_user.is_active
734           logger.warn "#{current_user.uuid.inspect}: " +
735             "No user agreements to sign, but activate failed!"
736         end
737       end
738       if !current_user.is_active
739         redirect_to user_agreements_path(return_to: request.fullpath)
740       end
741     end
742     true
743   end
744
745   def check_user_profile
746     return true if !current_user
747     if request.method.downcase != 'get' || params[:partial] ||
748        params[:tab_pane] || params[:action_method] ||
749        params[:action] == 'setup_popup'
750       return true
751     end
752
753     if missing_required_profile?
754       redirect_to profile_user_path(current_user.uuid, return_to: request.fullpath)
755     end
756     true
757   end
758
759   helper_method :missing_required_profile?
760   def missing_required_profile?
761     missing_required = false
762
763     profile_config = Rails.configuration.Workbench.UserProfileFormFields
764     if current_user && !profile_config.empty?
765       current_user_profile = current_user.prefs[:profile]
766       profile_config.each do |k, entry|
767         if entry[:Required]
768           if !current_user_profile ||
769              !current_user_profile[k] ||
770              current_user_profile[k].empty?
771             missing_required = true
772             break
773           end
774         end
775       end
776     end
777
778     missing_required
779   end
780
781   def select_theme
782     return Rails.configuration.Workbench.Theme
783   end
784
785   @@notification_tests = []
786
787   @@notification_tests.push lambda { |controller, current_user|
788     return nil if Rails.configuration.Services.WebShell.ExternalURL != URI("")
789     AuthorizedKey.limit(1).with_count('none').where(authorized_user_uuid: current_user.uuid).each do
790       return nil
791     end
792     return lambda { |view|
793       view.render partial: 'notifications/ssh_key_notification'
794     }
795   }
796
797   @@notification_tests.push lambda { |controller, current_user|
798     Collection.limit(1).with_count('none').where(created_by: current_user.uuid).each do
799       return nil
800     end
801     return lambda { |view|
802       view.render partial: 'notifications/collections_notification'
803     }
804   }
805
806   @@notification_tests.push lambda { |controller, current_user|
807     if PipelineInstance.api_exists?(:index)
808       PipelineInstance.limit(1).with_count('none').where(created_by: current_user.uuid).each do
809         return nil
810       end
811     else
812       return nil
813     end
814     return lambda { |view|
815       view.render partial: 'notifications/pipelines_notification'
816     }
817   }
818
819   helper_method :user_notifications
820   def user_notifications
821     @errors = nil if !defined?(@errors)
822     return [] if @errors or not current_user.andand.is_active or not Rails.configuration.Workbench.ShowUserNotifications
823     @notifications ||= @@notification_tests.map do |t|
824       t.call(self, current_user)
825     end.compact
826   end
827
828   helper_method :all_projects
829   def all_projects
830     @all_projects ||= Group.
831       filter([['group_class','IN',['project','filter']]]).order('name')
832   end
833
834   helper_method :my_projects
835   def my_projects
836     return @my_projects if @my_projects
837     @my_projects = []
838     root_of = {}
839     all_projects.each do |g|
840       root_of[g.uuid] = g.owner_uuid
841       @my_projects << g
842     end
843     done = false
844     while not done
845       done = true
846       root_of = root_of.each_with_object({}) do |(child, parent), h|
847         if root_of[parent]
848           h[child] = root_of[parent]
849           done = false
850         else
851           h[child] = parent
852         end
853       end
854     end
855     @my_projects = @my_projects.select do |g|
856       root_of[g.uuid] == current_user.uuid
857     end
858   end
859
860   helper_method :projects_shared_with_me
861   def projects_shared_with_me
862     my_project_uuids = my_projects.collect &:uuid
863     all_projects.reject { |x| x.uuid.in? my_project_uuids }
864   end
865
866   helper_method :recent_jobs_and_pipelines
867   def recent_jobs_and_pipelines
868     (Job.limit(10) |
869      PipelineInstance.limit(10).with_count("none")).
870       sort_by do |x|
871       (x.finished_at || x.started_at rescue nil) || x.modified_at || x.created_at
872     end.reverse
873   end
874
875   helper_method :running_pipelines
876   def running_pipelines
877     pi = PipelineInstance.order(["started_at asc", "created_at asc"]).with_count("none").filter([["state", "in", ["RunningOnServer", "RunningOnClient"]]])
878     jobs = {}
879     pi.each do |pl|
880       pl.components.each do |k,v|
881         if v.is_a? Hash and v[:job]
882           jobs[v[:job][:uuid]] = {}
883         end
884       end
885     end
886
887     if jobs.keys.any?
888       Job.filter([["uuid", "in", jobs.keys]]).with_count("none").each do |j|
889         jobs[j[:uuid]] = j
890       end
891
892       pi.each do |pl|
893         pl.components.each do |k,v|
894           if v.is_a? Hash and v[:job]
895             v[:job] = jobs[v[:job][:uuid]]
896           end
897         end
898       end
899     end
900
901     pi
902   end
903
904   helper_method :recent_processes
905   def recent_processes lim
906     lim = 12 if lim.nil?
907
908     procs = {}
909     if PipelineInstance.api_exists?(:index)
910       cols = %w(uuid owner_uuid created_at modified_at pipeline_template_uuid name state started_at finished_at)
911       pipelines = PipelineInstance.select(cols).limit(lim).order(["created_at desc"]).with_count("none")
912       pipelines.results.each { |pi| procs[pi] = pi.created_at }
913     end
914
915     crs = ContainerRequest.limit(lim).with_count("none").order(["created_at desc"]).filter([["requesting_container_uuid", "=", nil]]).select(
916       ["uuid", "name", "container_uuid", "output_uuid", "state", "created_at", "modified_at"])
917     crs.results.each { |c| procs[c] = c.created_at }
918
919     Hash[procs.sort_by {|key, value| value}].keys.reverse.first(lim)
920   end
921
922   helper_method :recent_collections
923   def recent_collections lim
924     c = Collection.limit(lim).with_count("none").order(["modified_at desc"]).results
925     own = {}
926     Group.filter([["uuid", "in", c.map(&:owner_uuid)]]).with_count("none").each do |g|
927       own[g[:uuid]] = g
928     end
929     {collections: c, owners: own}
930   end
931
932   helper_method :my_starred_projects
933   def my_starred_projects user, group_class
934     return if defined?(@starred_projects) && @starred_projects
935     links = Link.filter([['owner_uuid', 'in', ["#{Rails.configuration.ClusterID}-j7d0g-publicfavorites", user.uuid]],
936                          ['link_class', '=', 'star'],
937                          ['head_uuid', 'is_a', 'arvados#group']]).with_count("none").select(%w(head_uuid))
938     uuids = links.collect { |x| x.head_uuid }
939     if group_class == ""
940       starred_projects = Group.filter([['uuid', 'in', uuids]]).order('name').with_count("none")
941     else
942       starred_projects = Group.filter([['uuid', 'in', uuids],['group_class', '=', group_class]]).order('name').with_count("none")
943     end
944     @starred_projects = starred_projects.results
945   end
946
947   # If there are more than 200 projects that are readable by the user,
948   # build the tree using only the top 200+ projects owned by the user,
949   # from the top three levels.
950   # That is: get toplevel projects under home, get subprojects of
951   # these projects, and so on until we hit the limit.
952   def my_wanted_projects(user, page_size=100)
953     return @my_wanted_projects if defined?(@my_wanted_projects) && @my_wanted_projects
954
955     from_top = []
956     uuids = [user.uuid]
957     depth = 0
958     @too_many_projects = false
959     @reached_level_limit = false
960     while from_top.size <= page_size*2
961       current_level = Group.filter([['group_class','IN',['project','filter']],
962                                     ['owner_uuid', 'in', uuids]])
963                       .order('name').limit(page_size*2)
964       break if current_level.results.size == 0
965       @too_many_projects = true if current_level.items_available > current_level.results.size
966       from_top.concat current_level.results
967       uuids = current_level.results.collect(&:uuid)
968       depth += 1
969       if depth >= 3
970         @reached_level_limit = true
971         break
972       end
973     end
974     @my_wanted_projects = from_top
975   end
976
977   helper_method :my_wanted_projects_tree
978   def my_wanted_projects_tree(user, page_size=100)
979     build_my_wanted_projects_tree(user, page_size)
980     [@my_wanted_projects_tree, @too_many_projects, @reached_level_limit]
981   end
982
983   def build_my_wanted_projects_tree(user, page_size=100)
984     return @my_wanted_projects_tree if defined?(@my_wanted_projects_tree) && @my_wanted_projects_tree
985
986     parent_of = {user.uuid => 'me'}
987     my_wanted_projects(user, page_size).each do |ob|
988       parent_of[ob.uuid] = ob.owner_uuid
989     end
990     children_of = {false => [], 'me' => [user]}
991     my_wanted_projects(user, page_size).each do |ob|
992       if ob.owner_uuid != user.uuid and
993           not parent_of.has_key? ob.owner_uuid
994         parent_of[ob.uuid] = false
995       end
996       children_of[parent_of[ob.uuid]] ||= []
997       children_of[parent_of[ob.uuid]] << ob
998     end
999     buildtree = lambda do |chldrn_of, root_uuid=false|
1000       tree = {}
1001       chldrn_of[root_uuid].andand.each do |ob|
1002         tree[ob] = buildtree.call(chldrn_of, ob.uuid)
1003       end
1004       tree
1005     end
1006     sorted_paths = lambda do |tree, depth=0|
1007       paths = []
1008       tree.keys.sort_by { |ob|
1009         ob.is_a?(String) ? ob : ob.friendly_link_name
1010       }.each do |ob|
1011         paths << {object: ob, depth: depth}
1012         paths += sorted_paths.call tree[ob], depth+1
1013       end
1014       paths
1015     end
1016     @my_wanted_projects_tree =
1017       sorted_paths.call buildtree.call(children_of, 'me')
1018   end
1019
1020   helper_method :get_object
1021   def get_object uuid
1022     if @get_object.nil? and @objects
1023       @get_object = @objects.each_with_object({}) do |object, h|
1024         h[object.uuid] = object
1025       end
1026     end
1027     @get_object ||= {}
1028     @get_object[uuid]
1029   end
1030
1031   helper_method :project_breadcrumbs
1032   def project_breadcrumbs
1033     crumbs = []
1034     current = @name_link || @object
1035     while current
1036       # Halt if a group ownership loop is detected. API should refuse
1037       # to produce this state, but it could still arise from a race
1038       # condition when group ownership changes between our find()
1039       # queries.
1040       break if crumbs.collect(&:uuid).include? current.uuid
1041
1042       if current.is_a?(Group) and current.group_class == 'project'
1043         crumbs.prepend current
1044       end
1045       if current.is_a? Link
1046         current = Group.find?(current.tail_uuid)
1047       else
1048         current = Group.find?(current.owner_uuid)
1049       end
1050     end
1051     crumbs
1052   end
1053
1054   helper_method :current_project_uuid
1055   def current_project_uuid
1056     if @object.is_a? Group and @object.group_class == 'project'
1057       @object.uuid
1058     elsif @name_link.andand.tail_uuid
1059       @name_link.tail_uuid
1060     elsif @object and resource_class_for_uuid(@object.owner_uuid) == Group
1061       @object.owner_uuid
1062     else
1063       nil
1064     end
1065   end
1066
1067   # helper method to get links for given object or uuid
1068   helper_method :links_for_object
1069   def links_for_object object_or_uuid
1070     raise ArgumentError, 'No input argument' unless object_or_uuid
1071     preload_links_for_objects([object_or_uuid])
1072     uuid = object_or_uuid.is_a?(String) ? object_or_uuid : object_or_uuid.uuid
1073     @all_links_for[uuid] ||= []
1074   end
1075
1076   # helper method to preload links for given objects and uuids
1077   helper_method :preload_links_for_objects
1078   def preload_links_for_objects objects_and_uuids
1079     @all_links_for ||= {}
1080
1081     raise ArgumentError, 'Argument is not an array' unless objects_and_uuids.is_a? Array
1082     return @all_links_for if objects_and_uuids.empty?
1083
1084     uuids = objects_and_uuids.collect { |x| x.is_a?(String) ? x : x.uuid }
1085
1086     # if already preloaded for all of these uuids, return
1087     if not uuids.select { |x| @all_links_for[x].nil? }.any?
1088       return @all_links_for
1089     end
1090
1091     uuids.each do |x|
1092       @all_links_for[x] = []
1093     end
1094
1095     # TODO: make sure we get every page of results from API server
1096     Link.filter([['head_uuid', 'in', uuids]]).with_count("none").each do |link|
1097       @all_links_for[link.head_uuid] << link
1098     end
1099     @all_links_for
1100   end
1101
1102   # helper method to get a certain number of objects of a specific type
1103   # this can be used to replace any uses of: "dataclass.limit(n)"
1104   helper_method :get_n_objects_of_class
1105   def get_n_objects_of_class dataclass, size
1106     @objects_map_for ||= {}
1107
1108     raise ArgumentError, 'Argument is not a data class' unless dataclass.is_a? Class and dataclass < ArvadosBase
1109     raise ArgumentError, 'Argument is not a valid limit size' unless (size && size>0)
1110
1111     # if the objects_map_for has a value for this dataclass, and the
1112     # size used to retrieve those objects is equal, return it
1113     size_key = "#{dataclass.name}_size"
1114     if @objects_map_for[dataclass.name] && @objects_map_for[size_key] &&
1115         (@objects_map_for[size_key] == size)
1116       return @objects_map_for[dataclass.name]
1117     end
1118
1119     @objects_map_for[size_key] = size
1120     @objects_map_for[dataclass.name] = dataclass.limit(size)
1121   end
1122
1123   # helper method to get collections for the given uuid
1124   helper_method :collections_for_object
1125   def collections_for_object uuid
1126     raise ArgumentError, 'No input argument' unless uuid
1127     preload_collections_for_objects([uuid])
1128     @all_collections_for[uuid] ||= []
1129   end
1130
1131   # helper method to preload collections for the given uuids
1132   helper_method :preload_collections_for_objects
1133   def preload_collections_for_objects uuids
1134     @all_collections_for ||= {}
1135
1136     raise ArgumentError, 'Argument is not an array' unless uuids.is_a? Array
1137     return @all_collections_for if uuids.empty?
1138
1139     # if already preloaded for all of these uuids, return
1140     if not uuids.select { |x| @all_collections_for[x].nil? }.any?
1141       return @all_collections_for
1142     end
1143
1144     uuids.each do |x|
1145       @all_collections_for[x] = []
1146     end
1147
1148     # TODO: make sure we get every page of results from API server
1149     Collection.where(uuid: uuids).with_count("none").each do |collection|
1150       @all_collections_for[collection.uuid] << collection
1151     end
1152     @all_collections_for
1153   end
1154
1155   # helper method to get log collections for the given log
1156   helper_method :log_collections_for_object
1157   def log_collections_for_object log
1158     raise ArgumentError, 'No input argument' unless log
1159
1160     preload_log_collections_for_objects([log])
1161
1162     uuid = log
1163     fixup = /([a-f0-9]{32}\+\d+)(\+?.*)/.match(log)
1164     if fixup && fixup.size>1
1165       uuid = fixup[1]
1166     end
1167
1168     @all_log_collections_for[uuid] ||= []
1169   end
1170
1171   # helper method to preload collections for the given uuids
1172   helper_method :preload_log_collections_for_objects
1173   def preload_log_collections_for_objects logs
1174     @all_log_collections_for ||= {}
1175
1176     raise ArgumentError, 'Argument is not an array' unless logs.is_a? Array
1177     return @all_log_collections_for if logs.empty?
1178
1179     uuids = []
1180     logs.each do |log|
1181       fixup = /([a-f0-9]{32}\+\d+)(\+?.*)/.match(log)
1182       if fixup && fixup.size>1
1183         uuids << fixup[1]
1184       else
1185         uuids << log
1186       end
1187     end
1188
1189     # if already preloaded for all of these uuids, return
1190     if not uuids.select { |x| @all_log_collections_for[x].nil? }.any?
1191       return @all_log_collections_for
1192     end
1193
1194     uuids.each do |x|
1195       @all_log_collections_for[x] = []
1196     end
1197
1198     # TODO: make sure we get every page of results from API server
1199     Collection.where(uuid: uuids).with_count("none").each do |collection|
1200       @all_log_collections_for[collection.uuid] << collection
1201     end
1202     @all_log_collections_for
1203   end
1204
1205   # Helper method to get one collection for the given portable_data_hash
1206   # This is used to determine if a pdh is readable by the current_user
1207   helper_method :collection_for_pdh
1208   def collection_for_pdh pdh
1209     raise ArgumentError, 'No input argument' unless pdh
1210     preload_for_pdhs([pdh])
1211     @all_pdhs_for[pdh] ||= []
1212   end
1213
1214   # Helper method to preload one collection each for the given pdhs
1215   # This is used to determine if a pdh is readable by the current_user
1216   helper_method :preload_for_pdhs
1217   def preload_for_pdhs pdhs
1218     @all_pdhs_for ||= {}
1219
1220     raise ArgumentError, 'Argument is not an array' unless pdhs.is_a? Array
1221     return @all_pdhs_for if pdhs.empty?
1222
1223     # if already preloaded for all of these pdhs, return
1224     if not pdhs.select { |x| @all_pdhs_for[x].nil? }.any?
1225       return @all_pdhs_for
1226     end
1227
1228     pdhs.each do |x|
1229       @all_pdhs_for[x] = []
1230     end
1231
1232     Collection.select(%w(portable_data_hash)).where(portable_data_hash: pdhs).distinct().with_count("none").each do |collection|
1233       @all_pdhs_for[collection.portable_data_hash] << collection
1234     end
1235     @all_pdhs_for
1236   end
1237
1238   # helper method to get object of a given dataclass and uuid
1239   helper_method :object_for_dataclass
1240   def object_for_dataclass dataclass, uuid, by_attr=nil
1241     raise ArgumentError, 'No input argument dataclass' unless (dataclass && uuid)
1242     preload_objects_for_dataclass(dataclass, [uuid], by_attr)
1243     @objects_for[uuid]
1244   end
1245
1246   # helper method to preload objects for given dataclass and uuids
1247   helper_method :preload_objects_for_dataclass
1248   def preload_objects_for_dataclass dataclass, uuids, by_attr=nil, select_fields=nil
1249     @objects_for ||= {}
1250
1251     raise ArgumentError, 'Argument is not a data class' unless dataclass.is_a? Class
1252     raise ArgumentError, 'Argument is not an array' unless uuids.is_a? Array
1253
1254     return @objects_for if uuids.empty?
1255
1256     # if already preloaded for all of these uuids, return
1257     if not uuids.select { |x| !@objects_for.include?(x) }.any?
1258       return @objects_for
1259     end
1260
1261     # preset all uuids to nil
1262     uuids.each do |x|
1263       @objects_for[x] = nil
1264     end
1265     if by_attr and ![:uuid, :name].include?(by_attr)
1266       raise ArgumentError, "Preloading only using lookups by uuid or name are supported: #{by_attr}"
1267     elsif by_attr and by_attr == :name
1268       dataclass.where(name: uuids).each do |obj|
1269         @objects_for[obj.name] = obj
1270       end
1271     else
1272       key_prefix = "request_#{Thread.current.object_id}_#{dataclass.to_s}_"
1273       dataclass.where(uuid: uuids).select(select_fields).each do |obj|
1274         @objects_for[obj.uuid] = obj
1275         if dataclass == Collection
1276           # The collecions#index defaults to "all attributes except manifest_text"
1277           # Hence, this object is not suitable for preloading the find() cache.
1278         else
1279           Rails.cache.write(key_prefix + obj.uuid, obj.as_json)
1280         end
1281       end
1282     end
1283     @objects_for
1284   end
1285
1286   # helper method to load objects that are already preloaded
1287   helper_method :load_preloaded_objects
1288   def load_preloaded_objects objs
1289     @objects_for ||= {}
1290     objs.each do |obj|
1291       @objects_for[obj.uuid] = obj
1292     end
1293   end
1294
1295   # helper method to get the names of collection files selected
1296   helper_method :selected_collection_files
1297   def selected_collection_files params
1298     link_uuids, coll_ids = params["selection"].partition do |sel_s|
1299       ArvadosBase::resource_class_for_uuid(sel_s) == Link
1300     end
1301
1302     unless link_uuids.empty?
1303       Link.select([:head_uuid]).where(uuid: link_uuids).with_count("none").each do |link|
1304         if ArvadosBase::resource_class_for_uuid(link.head_uuid) == Collection
1305           coll_ids << link.head_uuid
1306         end
1307       end
1308     end
1309
1310     uuids = []
1311     pdhs = []
1312     source_paths = Hash.new { |hash, key| hash[key] = [] }
1313     coll_ids.each do |coll_id|
1314       if m = CollectionsHelper.match(coll_id)
1315         key = m[1] + m[2]
1316         pdhs << key
1317         source_paths[key] << m[4]
1318       elsif m = CollectionsHelper.match_uuid_with_optional_filepath(coll_id)
1319         key = m[1]
1320         uuids << key
1321         source_paths[key] << m[4]
1322       end
1323     end
1324
1325     unless pdhs.empty?
1326       Collection.where(portable_data_hash: pdhs.uniq).with_count("none").
1327           select([:uuid, :portable_data_hash]).each do |coll|
1328         unless source_paths[coll.portable_data_hash].empty?
1329           uuids << coll.uuid
1330           source_paths[coll.uuid] = source_paths.delete(coll.portable_data_hash)
1331         end
1332       end
1333     end
1334
1335     [uuids, source_paths]
1336   end
1337
1338   def wiselinks_layout
1339     'body'
1340   end
1341 end