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.',
49 params.delete(:select)
53 def self._create_requires_parameters
61 description: 'defer permissions update',
67 def self._update_requires_parameters
75 description: 'defer permissions update',
83 @object = model_class.new(resource_attrs.merge({async_permissions_update: true}))
93 attrs_to_update = resource_attrs.reject { |k, v|
94 [:kind, :etag, :href].index k
95 }.merge({async_permissions_update: true})
96 @object.update_attributes!(attrs_to_update)
105 if !TRASHABLE_CLASSES.include?(@object.group_class)
109 super # Calls destroy from TrashableController module
113 def render_404_if_no_object
114 if params[:action] == 'contents'
122 elsif (@object = User.where(uuid: params[:uuid]).first)
123 # "Home" pseudo-project
134 load_searchable_objects
136 :kind => "arvados#objectList",
141 :items => @objects.as_api_response(nil)
143 if params[:count] != 'none'
144 list[:items_available] = @items_available
147 list[:included] = @extra_included.as_api_response(nil, {select: @select})
153 # The purpose of this endpoint is to return the toplevel set of
154 # groups which are *not* reachable through a direct ownership
155 # chain of projects starting from the current user account. In
156 # other words, groups which to which access was granted via a
157 # permission link or chain of links.
159 # This also returns (in the "included" field) the objects that own
160 # those projects (users or non-project groups).
163 # The intended use of this endpoint is to support clients which
164 # wish to browse those projects which are visible to the user but
165 # are not part of the "home" project.
167 load_limit_offset_order_params
170 @objects = exclude_home Group.readable_by(*@read_users), Group
172 apply_where_limit_order_params
174 if params["include"] == "owner_uuid"
175 owners = @objects.map(&:owner_uuid).to_set
177 [Group, User].each do |klass|
178 @extra_included += klass.readable_by(*@read_users).where(uuid: owners.to_a).to_a
185 def self._shared_requires_parameters
186 rp = self._index_requires_parameters
187 rp[:include] = { type: 'string', required: false }
193 def load_searchable_objects
197 # Reload the orders param, this time without prefixing unqualified
198 # columns ("name" => "groups.name"). Here, unqualified orders
199 # apply to each table being searched, not "groups".
200 load_limit_offset_order_params(fill_table_names: false)
202 if params['count'] == 'none' and @offset != 0 and (params['last_object_class'].nil? or params['last_object_class'].empty?)
203 # can't use offset without getting counts, so
204 # fall back to count=exact behavior.
205 params['count'] = 'exact'
206 set_count_none = true
209 # Trick apply_where_limit_order_params into applying suitable
210 # per-table values. *_all are the real ones we'll apply to the
214 # save the orders from the current request as determined by load_param,
215 # but otherwise discard them because we're going to be getting objects
217 request_orders = @orders.clone
220 request_filters = @filters
223 Job, PipelineInstance, PipelineTemplate, ContainerRequest, Workflow,
225 Human, Specimen, Trait]
227 table_names = Hash[klasses.collect { |k| [k, k.table_name] }]
229 disabled_methods = Rails.configuration.API.DisabledAPIs
230 avail_klasses = table_names.select{|k, t| !disabled_methods[t+'.index']}
231 klasses = avail_klasses.keys
233 request_filters.each do |col, op, val|
234 if col.index('.') && !table_names.values.include?(col.split('.', 2)[0])
235 raise ArgumentError.new("Invalid attribute '#{col}' in filter")
240 request_filters.each do |col,op,val|
242 (val.is_a?(Array) ? val : [val]).each do |type|
243 type = type.split('#')[-1]
244 type[0] = type[0].capitalize
245 wanted_klasses << type
252 if params['recursive']
253 filter_by_owner[:owner_uuid] = [@object.uuid] + @object.descendant_project_uuids
255 filter_by_owner[:owner_uuid] = @object.uuid
258 if params['exclude_home_project']
259 raise ArgumentError.new "Cannot use 'exclude_home_project' with a parent object"
263 included_by_uuid = {}
265 seen_last_class = false
269 klasses.each do |klass|
270 # check if current klass is same as params['last_object_class']
271 seen_last_class = true if((params['count'].andand.==('none')) and
272 (params['last_object_class'].nil? or
273 params['last_object_class'].empty? or
274 params['last_object_class'] == klass.to_s))
276 # if klasses are specified, skip all other klass types
277 next if wanted_klasses.any? and !wanted_klasses.include?(klass.to_s)
279 # if specified, and count=none, then only look at the klass in
281 # for whatever reason, this parameter exists separately from 'wanted_klasses'
282 next if params['count'] == 'none' and !seen_last_class
284 # don't process rest of object types if we already have needed number of objects
285 break if params['count'] == 'none' and all_objects.size >= limit_all
287 # If the currently requested orders specifically match the
288 # table_name for the current klass, apply that order.
289 # Otherwise, order by recency.
291 request_orders.andand.find { |r| r =~ /^#{klass.table_name}\./i || r !~ /\./ } ||
292 klass.default_orders.join(", ")
295 where_conds = filter_by_owner
296 if klass == Collection
297 @select = klass.selectable_attributes - ["manifest_text", "unsigned_manifest_text"]
299 where_conds = where_conds.merge(group_class: ["project","filter"])
302 @filters = request_filters.map do |col, op, val|
305 elsif (col = col.split('.', 2))[0] == klass.table_name
312 @objects = klass.readable_by(*@read_users, {
313 :include_trash => params[:include_trash],
314 :include_old_versions => params[:include_old_versions]
315 }).order(request_order).where(where_conds)
317 if params['exclude_home_project']
318 @objects = exclude_home @objects, klass
321 # Adjust the limit based on number of objects fetched so far
322 klass_limit = limit_all - all_objects.count
326 apply_where_limit_order_params klass
327 rescue ArgumentError => e
328 if e.inspect =~ /Invalid attribute '.+' for operator '.+' in filter/ or
329 e.inspect =~ /Invalid attribute '.+' for subproperty filter/
330 error_by_class[klass.name] = e
338 # This actually fetches the objects
339 klass_object_list = object_list(model_class: klass)
341 # If count=none, :items_available will be nil, and offset is
343 klass_items_available = klass_object_list[:items_available] || 0
344 @items_available += klass_items_available
345 @offset = [@offset - klass_items_available, 0].max
347 # Add objects to the list of objects to be returned.
348 all_objects += klass_object_list[:items]
350 if klass_object_list[:limit] < klass_limit
351 # object_list() had to reduce @limit to comply with
352 # max_index_database_read. From now on, we'll do all queries
353 # with limit=0 and just accumulate items_available.
354 limit_all = all_objects.count
357 if params["include"] == "owner_uuid"
358 owners = klass_object_list[:items].map {|i| i[:owner_uuid]}.to_set
359 [Group, User].each do |ownerklass|
360 ownerklass.readable_by(*@read_users).where(uuid: owners.to_a).each do |ow|
361 included_by_uuid[ow.uuid] = ow
367 # Only error out when every searchable object type errored out
368 if !any_success && error_by_class.size > 0
369 error_msg = error_by_class.collect do |klass, err|
370 "#{err} on object type #{klass}"
372 raise ArgumentError.new(error_msg)
376 @extra_included = included_by_uuid.values
380 params['count'] = 'none'
383 @objects = all_objects
388 def exclude_home objectlist, klass
389 # select records that are readable by current user AND
390 # the owner_uuid is a user (but not the current user) OR
391 # the owner_uuid is not readable by the current user
392 # the owner_uuid is a group but group_class is not a project
394 read_parent_check = if current_user.is_admin
397 "NOT EXISTS(SELECT 1 FROM #{PERMISSION_VIEW} WHERE "+
398 "user_uuid=(:user_uuid) AND target_uuid=#{klass.table_name}.owner_uuid AND perm_level >= 1) OR "
401 objectlist.where("#{klass.table_name}.owner_uuid IN (SELECT users.uuid FROM users WHERE users.uuid != (:user_uuid)) OR "+
403 "EXISTS(SELECT 1 FROM groups as gp where gp.uuid=#{klass.table_name}.owner_uuid and gp.group_class != 'project')",
404 user_uuid: current_user.uuid)