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 def self._index_requires_parameters
17 type: 'boolean', required: false, description: "Include items whose is_trashed attribute is true."
22 def self._show_requires_parameters
26 type: 'boolean', required: false, description: "Show group/project even if its is_trashed attribute is true."
31 def self._contents_requires_parameters
32 params = _index_requires_parameters.
35 type: 'string', required: false, default: nil
38 type: 'boolean', required: false, description: 'Include contents from child groups recursively.'
41 type: 'string', required: false, description: 'Include objects referred to by listed field in "included" (only owner_uuid)'
44 params.delete(:select)
48 def self._create_requires_parameters
56 description: 'defer permissions update'
62 def self._update_requires_parameters
70 description: 'defer permissions update'
78 @object = model_class.new(resource_attrs.merge({async_permissions_update: true}))
88 attrs_to_update = resource_attrs.reject { |k, v|
89 [:kind, :etag, :href].index k
90 }.merge({async_permissions_update: true})
91 @object.update_attributes!(attrs_to_update)
99 def render_404_if_no_object
100 if params[:action] == 'contents'
108 elsif (@object = User.where(uuid: params[:uuid]).first)
109 # "Home" pseudo-project
120 load_searchable_objects
122 :kind => "arvados#objectList",
127 :items_available => @items_available,
128 :items => @objects.as_api_response(nil)
131 list[:included] = @extra_included.as_api_response(nil, {select: @select})
137 # The purpose of this endpoint is to return the toplevel set of
138 # groups which are *not* reachable through a direct ownership
139 # chain of projects starting from the current user account. In
140 # other words, groups which to which access was granted via a
141 # permission link or chain of links.
143 # This also returns (in the "included" field) the objects that own
144 # those projects (users or non-project groups).
147 # The intended use of this endpoint is to support clients which
148 # wish to browse those projects which are visible to the user but
149 # are not part of the "home" project.
151 load_limit_offset_order_params
154 @objects = exclude_home Group.readable_by(*@read_users), Group
156 apply_where_limit_order_params
158 if params["include"] == "owner_uuid"
159 owners = @objects.map(&:owner_uuid).to_set
161 [Group, User].each do |klass|
162 @extra_included += klass.readable_by(*@read_users).where(uuid: owners.to_a).to_a
169 def self._shared_requires_parameters
170 rp = self._index_requires_parameters
171 rp[:include] = { type: 'string', required: false }
177 def load_searchable_objects
181 # Reload the orders param, this time without prefixing unqualified
182 # columns ("name" => "groups.name"). Here, unqualified orders
183 # apply to each table being searched, not "groups".
184 load_limit_offset_order_params(fill_table_names: false)
186 # Trick apply_where_limit_order_params into applying suitable
187 # per-table values. *_all are the real ones we'll apply to the
191 # save the orders from the current request as determined by load_param,
192 # but otherwise discard them because we're going to be getting objects
194 request_orders = @orders.clone
197 request_filters = @filters
200 Job, PipelineInstance, PipelineTemplate, ContainerRequest, Workflow,
202 Human, Specimen, Trait]
204 table_names = Hash[klasses.collect { |k| [k, k.table_name] }]
206 disabled_methods = Rails.configuration.API.DisabledAPIs
207 avail_klasses = table_names.select{|k, t| !disabled_methods[t+'.index']}
208 klasses = avail_klasses.keys
210 request_filters.each do |col, op, val|
211 if col.index('.') && !table_names.values.include?(col.split('.', 2)[0])
212 raise ArgumentError.new("Invalid attribute '#{col}' in filter")
217 request_filters.each do |col,op,val|
219 (val.is_a?(Array) ? val : [val]).each do |type|
220 type = type.split('#')[-1]
221 type[0] = type[0].capitalize
222 wanted_klasses << type
229 if params['recursive']
230 filter_by_owner[:owner_uuid] = [@object.uuid] + @object.descendant_project_uuids
232 filter_by_owner[:owner_uuid] = @object.uuid
235 if params['exclude_home_project']
236 raise ArgumentError.new "Cannot use 'exclude_home_project' with a parent object"
240 included_by_uuid = {}
242 seen_last_class = false
243 klasses.each do |klass|
244 @offset = 0 if seen_last_class # reset offset for the new next type being processed
246 # if current klass is same as params['last_object_class'], mark that fact
247 seen_last_class = true if((params['count'].andand.==('none')) and
248 (params['last_object_class'].nil? or
249 params['last_object_class'].empty? or
250 params['last_object_class'] == klass.to_s))
252 # if klasses are specified, skip all other klass types
253 next if wanted_klasses.any? and !wanted_klasses.include?(klass.to_s)
255 # don't reprocess klass types that were already seen
256 next if params['count'] == 'none' and !seen_last_class
258 # don't process rest of object types if we already have needed number of objects
259 break if params['count'] == 'none' and all_objects.size >= limit_all
261 # If the currently requested orders specifically match the
262 # table_name for the current klass, apply that order.
263 # Otherwise, order by recency.
265 request_orders.andand.find { |r| r =~ /^#{klass.table_name}\./i || r !~ /\./ } ||
266 klass.default_orders.join(", ")
269 where_conds = filter_by_owner
270 if klass == Collection
271 @select = klass.selectable_attributes - ["manifest_text"]
273 where_conds = where_conds.merge(group_class: "project")
276 @filters = request_filters.map do |col, op, val|
279 elsif (col = col.split('.', 2))[0] == klass.table_name
286 @objects = klass.readable_by(*@read_users, {:include_trash => params[:include_trash]}).
287 order(request_order).where(where_conds)
289 if params['exclude_home_project']
290 @objects = exclude_home @objects, klass
293 klass_limit = limit_all - all_objects.count
295 apply_where_limit_order_params klass
296 klass_object_list = object_list(model_class: klass)
297 klass_items_available = klass_object_list[:items_available] || 0
298 @items_available += klass_items_available
299 @offset = [@offset - klass_items_available, 0].max
300 all_objects += klass_object_list[:items]
302 if klass_object_list[:limit] < klass_limit
303 # object_list() had to reduce @limit to comply with
304 # max_index_database_read. From now on, we'll do all queries
305 # with limit=0 and just accumulate items_available.
306 limit_all = all_objects.count
309 if params["include"] == "owner_uuid"
310 owners = klass_object_list[:items].map {|i| i[:owner_uuid]}.to_set
311 [Group, User].each do |ownerklass|
312 ownerklass.readable_by(*@read_users).where(uuid: owners.to_a).each do |ow|
313 included_by_uuid[ow.uuid] = ow
320 @extra_included = included_by_uuid.values
323 @objects = all_objects
330 def exclude_home objectlist, klass
331 # select records that are readable by current user AND
332 # the owner_uuid is a user (but not the current user) OR
333 # the owner_uuid is not readable by the current user
334 # the owner_uuid is a group but group_class is not a project
336 read_parent_check = if current_user.is_admin
339 "NOT EXISTS(SELECT 1 FROM #{PERMISSION_VIEW} WHERE "+
340 "user_uuid=(:user_uuid) AND target_uuid=#{klass.table_name}.owner_uuid AND perm_level >= 1) OR "
343 objectlist.where("#{klass.table_name}.owner_uuid IN (SELECT users.uuid FROM users WHERE users.uuid != (:user_uuid)) OR "+
345 "EXISTS(SELECT 1 FROM groups as gp where gp.uuid=#{klass.table_name}.owner_uuid and gp.group_class != 'project')",
346 user_uuid: current_user.uuid)