Merge branch '8784-dir-listings'
[arvados.git] / services / api / app / controllers / arvados / v1 / groups_controller.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 class Arvados::V1::GroupsController < ApplicationController
6
7   def self._contents_requires_parameters
8     params = _index_requires_parameters.
9       merge({
10               uuid: {
11                 type: 'string', required: false, default: nil
12               },
13               recursive: {
14                 type: 'boolean', required: false, description: 'Include contents from child groups recursively.'
15               },
16             })
17     params.delete(:select)
18     params
19   end
20
21   def render_404_if_no_object
22     if params[:action] == 'contents'
23       if !params[:uuid]
24         # OK!
25         @object = nil
26         true
27       elsif @object
28         # Project group
29         true
30       elsif (@object = User.where(uuid: params[:uuid]).first)
31         # "Home" pseudo-project
32         true
33       else
34         super
35       end
36     else
37       super
38     end
39   end
40
41   def contents
42     load_searchable_objects
43     send_json({
44       :kind => "arvados#objectList",
45       :etag => "",
46       :self_link => "",
47       :offset => @offset,
48       :limit => @limit,
49       :items_available => @items_available,
50       :items => @objects.as_api_response(nil)
51     })
52   end
53
54   protected
55
56   def load_searchable_objects
57     all_objects = []
58     @items_available = 0
59
60     # Trick apply_where_limit_order_params into applying suitable
61     # per-table values. *_all are the real ones we'll apply to the
62     # aggregate set.
63     limit_all = @limit
64     offset_all = @offset
65     # save the orders from the current request as determined by load_param,
66     # but otherwise discard them because we're going to be getting objects
67     # from many models
68     request_orders = @orders.clone
69     @orders = []
70
71     request_filters = @filters
72
73     klasses = [Group,
74      Job, PipelineInstance, PipelineTemplate, ContainerRequest, Workflow,
75      Collection,
76      Human, Specimen, Trait]
77
78     table_names = Hash[klasses.collect { |k| [k, k.table_name] }]
79
80     disabled_methods = Rails.configuration.disable_api_methods
81     avail_klasses = table_names.select{|k, t| !disabled_methods.include?(t+'.index')}
82     klasses = avail_klasses.keys
83
84     request_filters.each do |col, op, val|
85       if col.index('.') && !table_names.values.include?(col.split('.', 2)[0])
86         raise ArgumentError.new("Invalid attribute '#{col}' in filter")
87       end
88     end
89
90     wanted_klasses = []
91     request_filters.each do |col,op,val|
92       if op == 'is_a'
93         (val.is_a?(Array) ? val : [val]).each do |type|
94           type = type.split('#')[-1]
95           type[0] = type[0].capitalize
96           wanted_klasses << type
97         end
98       end
99     end
100
101     filter_by_owner = {}
102     if @object
103       if params['recursive']
104         filter_by_owner[:owner_uuid] = [@object.uuid] + @object.descendant_project_uuids
105       else
106         filter_by_owner[:owner_uuid] = @object.uuid
107       end
108     end
109
110     seen_last_class = false
111     klasses.each do |klass|
112       @offset = 0 if seen_last_class  # reset offset for the new next type being processed
113
114       # if current klass is same as params['last_object_class'], mark that fact
115       seen_last_class = true if((params['count'].andand.==('none')) and
116                                 (params['last_object_class'].nil? or
117                                  params['last_object_class'].empty? or
118                                  params['last_object_class'] == klass.to_s))
119
120       # if klasses are specified, skip all other klass types
121       next if wanted_klasses.any? and !wanted_klasses.include?(klass.to_s)
122
123       # don't reprocess klass types that were already seen
124       next if params['count'] == 'none' and !seen_last_class
125
126       # don't process rest of object types if we already have needed number of objects
127       break if params['count'] == 'none' and all_objects.size >= limit_all
128
129       # If the currently requested orders specifically match the
130       # table_name for the current klass, apply that order.
131       # Otherwise, order by recency.
132       request_order =
133         request_orders.andand.find { |r| r =~ /^#{klass.table_name}\./i } ||
134         klass.default_orders.join(", ")
135
136       @select = nil
137       where_conds = filter_by_owner
138       if klass == Collection
139         @select = klass.selectable_attributes - ["manifest_text"]
140       elsif klass == Group
141         where_conds = where_conds.merge(group_class: "project")
142       end
143
144       @filters = request_filters.map do |col, op, val|
145         if !col.index('.')
146           [col, op, val]
147         elsif (col = col.split('.', 2))[0] == klass.table_name
148           [col[1], op, val]
149         else
150           nil
151         end
152       end.compact
153
154       if klass == Collection and params[:include_trash]
155         @objects = klass.unscoped.readable_by(*@read_users).
156           order(request_order).where(where_conds)
157       else
158         @objects = klass.readable_by(*@read_users).
159           order(request_order).where(where_conds)
160       end
161       klass_limit = limit_all - all_objects.count
162       @limit = klass_limit
163       apply_where_limit_order_params klass
164       klass_object_list = object_list(model_class: klass)
165       klass_items_available = klass_object_list[:items_available] || 0
166       @items_available += klass_items_available
167       @offset = [@offset - klass_items_available, 0].max
168       all_objects += klass_object_list[:items]
169
170       if klass_object_list[:limit] < klass_limit
171         # object_list() had to reduce @limit to comply with
172         # max_index_database_read. From now on, we'll do all queries
173         # with limit=0 and just accumulate items_available.
174         limit_all = all_objects.count
175       end
176     end
177
178     @objects = all_objects
179     @limit = limit_all
180     @offset = offset_all
181   end
182
183 end