1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
7 class Arvados::V1::GroupsController < ApplicationController
8 include TrashableController
10 skip_before_action :find_object_by_uuid, only: :shared
11 skip_before_action :render_404_if_no_object, only: :shared
13 TRASHABLE_CLASSES = ['project']
15 def self._index_requires_parameters
19 type: 'boolean', required: false, default: false, description: "Include items whose is_trashed attribute is true.",
24 def self._show_requires_parameters
28 type: 'boolean', required: false, default: false, description: "Show group/project even if its is_trashed attribute is true.",
33 def self._contents_requires_parameters
34 params = _index_requires_parameters.
37 type: 'string', required: false, default: '',
40 type: 'boolean', required: false, default: false, description: 'Include contents from child groups recursively.',
43 type: 'string', required: false, description: 'Include objects referred to by listed field in "included" (only owner_uuid).',
45 include_old_versions: {
46 type: 'boolean', required: false, default: false, description: 'Include past collection versions.',
52 def self._create_requires_parameters
60 description: 'defer permissions update',
66 def self._update_requires_parameters
74 description: 'defer permissions update',
82 @object = model_class.new(resource_attrs.merge({async_permissions_update: true}))
92 attrs_to_update = resource_attrs.reject { |k, v|
93 [:kind, :etag, :href].index k
94 }.merge({async_permissions_update: true})
95 @object.update!(attrs_to_update)
104 if !TRASHABLE_CLASSES.include?(@object.group_class)
108 super # Calls destroy from TrashableController module
112 def render_404_if_no_object
113 if params[:action] == 'contents'
121 elsif (@object = User.where(uuid: params[:uuid]).first)
122 # "Home" pseudo-project
133 load_searchable_objects
135 :kind => "arvados#objectList",
140 :items => @objects.as_api_response(nil)
142 if params[:count] != 'none'
143 list[:items_available] = @items_available
146 list[:included] = @extra_included.as_api_response(nil, {select: @select})
152 # The purpose of this endpoint is to return the toplevel set of
153 # groups which are *not* reachable through a direct ownership
154 # chain of projects starting from the current user account. In
155 # other words, groups which to which access was granted via a
156 # permission link or chain of links.
158 # This also returns (in the "included" field) the objects that own
159 # those projects (users or non-project groups).
162 # The intended use of this endpoint is to support clients which
163 # wish to browse those projects which are visible to the user but
164 # are not part of the "home" project.
166 load_limit_offset_order_params
169 @objects = exclude_home Group.readable_by(*@read_users), Group
171 apply_where_limit_order_params
173 if params["include"] == "owner_uuid"
174 owners = @objects.map(&:owner_uuid).to_set
176 [Group, User].each do |klass|
177 @extra_included += klass.readable_by(*@read_users).where(uuid: owners.to_a).to_a
184 def self._shared_requires_parameters
185 rp = self._index_requires_parameters
186 rp[:include] = { type: 'string', required: false }
192 def load_searchable_objects
196 # Reload the orders param, this time without prefixing unqualified
197 # columns ("name" => "groups.name"). Here, unqualified orders
198 # apply to each table being searched, not "groups".
199 load_limit_offset_order_params(fill_table_names: false)
201 if params['count'] == 'none' and @offset != 0 and (params['last_object_class'].nil? or params['last_object_class'].empty?)
202 # can't use offset without getting counts, so
203 # fall back to count=exact behavior.
204 params['count'] = 'exact'
205 set_count_none = true
208 # Trick apply_where_limit_order_params into applying suitable
209 # per-table values. *_all are the real ones we'll apply to the
213 # save the orders from the current request as determined by load_param,
214 # but otherwise discard them because we're going to be getting objects
216 request_orders = @orders.clone
219 request_filters = @filters
221 klasses = [Group, ContainerRequest, Workflow, Collection]
223 table_names = Hash[klasses.collect { |k| [k, k.table_name] }]
225 disabled_methods = Rails.configuration.API.DisabledAPIs
226 avail_klasses = table_names.select{|k, t| !disabled_methods[t+'.index']}
227 klasses = avail_klasses.keys
229 request_filters.each do |col, op, val|
230 if col.index('.') && !table_names.values.include?(col.split('.', 2)[0])
231 raise ArgumentError.new("Invalid attribute '#{col}' in filter")
236 request_filters.each do |col,op,val|
238 (val.is_a?(Array) ? val : [val]).each do |type|
239 type = type.split('#')[-1]
240 type[0] = type[0].capitalize
241 wanted_klasses << type
248 if params['recursive']
249 filter_by_owner[:owner_uuid] = [@object.uuid] + @object.descendant_project_uuids
251 filter_by_owner[:owner_uuid] = @object.uuid
254 if params['exclude_home_project']
255 raise ArgumentError.new "Cannot use 'exclude_home_project' with a parent object"
259 # Check that any fields in @select are valid for at least one class
262 klasses.each do |klass|
263 all_attributes.concat klass.selectable_attributes
265 @select.each do |check|
266 if !all_attributes.include? check
267 raise ArgumentError.new "Invalid attribute '#{check}' in select"
271 any_selections = @select
273 included_by_uuid = {}
275 seen_last_class = false
279 klasses.each do |klass|
280 # check if current klass is same as params['last_object_class']
281 seen_last_class = true if((params['count'].andand.==('none')) and
282 (params['last_object_class'].nil? or
283 params['last_object_class'].empty? or
284 params['last_object_class'] == klass.to_s))
286 # if klasses are specified, skip all other klass types
287 next if wanted_klasses.any? and !wanted_klasses.include?(klass.to_s)
289 # if specified, and count=none, then only look at the klass in
291 # for whatever reason, this parameter exists separately from 'wanted_klasses'
292 next if params['count'] == 'none' and !seen_last_class
294 # don't process rest of object types if we already have needed number of objects
295 break if params['count'] == 'none' and all_objects.size >= limit_all
297 # If the currently requested orders specifically match the
298 # table_name for the current klass, apply that order.
299 # Otherwise, order by recency.
301 request_orders.andand.find { |r| r =~ /^#{klass.table_name}\./i || r !~ /\./ } ||
302 klass.default_orders.join(", ")
304 @select = select_for_klass any_selections, klass, false
306 where_conds = filter_by_owner
307 if klass == Collection && @select.nil?
308 @select = klass.selectable_attributes - ["manifest_text", "unsigned_manifest_text"]
310 where_conds = where_conds.merge(group_class: ["project","filter"])
313 # Make signed manifest_text not selectable because controller
314 # currently doesn't know to sign it.
316 @select = @select - ["manifest_text"]
319 @filters = request_filters.map do |col, op, val|
322 elsif (col = col.split('.', 2))[0] == klass.table_name
329 @objects = klass.readable_by(*@read_users, {
330 :include_trash => params[:include_trash],
331 :include_old_versions => params[:include_old_versions]
332 }).order(request_order).where(where_conds)
334 if params['exclude_home_project']
335 @objects = exclude_home @objects, klass
338 # Adjust the limit based on number of objects fetched so far
339 klass_limit = limit_all - all_objects.count
343 apply_where_limit_order_params klass
344 rescue ArgumentError => e
345 if e.inspect =~ /Invalid attribute '.+' for operator '.+' in filter/ or
346 e.inspect =~ /Invalid attribute '.+' for subproperty filter/
347 error_by_class[klass.name] = e
355 # This actually fetches the objects
356 klass_object_list = object_list(model_class: klass)
358 # If count=none, :items_available will be nil, and offset is
360 klass_items_available = klass_object_list[:items_available] || 0
361 @items_available += klass_items_available
362 @offset = [@offset - klass_items_available, 0].max
364 # Add objects to the list of objects to be returned.
365 all_objects += klass_object_list[:items]
367 if klass_object_list[:limit] < klass_limit
368 # object_list() had to reduce @limit to comply with
369 # max_index_database_read. From now on, we'll do all queries
370 # with limit=0 and just accumulate items_available.
371 limit_all = all_objects.count
374 if params["include"] == "owner_uuid"
375 owners = klass_object_list[:items].map {|i| i[:owner_uuid]}.to_set
376 [Group, User].each do |ownerklass|
377 ownerklass.readable_by(*@read_users).where(uuid: owners.to_a).each do |ow|
378 included_by_uuid[ow.uuid] = ow
384 # Only error out when every searchable object type errored out
385 if !any_success && error_by_class.size > 0
386 error_msg = error_by_class.collect do |klass, err|
387 "#{err} on object type #{klass}"
389 raise ArgumentError.new(error_msg)
393 @extra_included = included_by_uuid.values
397 params['count'] = 'none'
400 @objects = all_objects
405 def exclude_home objectlist, klass
406 # select records that are readable by current user AND
407 # the owner_uuid is a user (but not the current user) OR
408 # the owner_uuid is not readable by the current user
409 # the owner_uuid is a group but group_class is not a project
411 read_parent_check = if current_user.is_admin
414 "NOT EXISTS(SELECT 1 FROM #{PERMISSION_VIEW} WHERE "+
415 "user_uuid=(:user_uuid) AND target_uuid=#{klass.table_name}.owner_uuid AND perm_level >= 1) OR "
418 objectlist.where("#{klass.table_name}.owner_uuid IN (SELECT users.uuid FROM users WHERE users.uuid != (:user_uuid)) OR "+
420 "EXISTS(SELECT 1 FROM groups as gp where gp.uuid=#{klass.table_name}.owner_uuid and gp.group_class != 'project')",
421 user_uuid: current_user.uuid)