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, default: false, description: "Include items whose is_trashed attribute is true.",
22 def self._show_requires_parameters
26 type: 'boolean', required: false, default: 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, default: 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).',
43 include_old_versions: {
44 type: 'boolean', required: false, default: false, description: 'Include past collection versions.',
47 params.delete(:select)
51 def self._create_requires_parameters
59 description: 'defer permissions update',
65 def self._update_requires_parameters
73 description: 'defer permissions update',
81 @object = model_class.new(resource_attrs.merge({async_permissions_update: true}))
91 attrs_to_update = resource_attrs.reject { |k, v|
92 [:kind, :etag, :href].index k
93 }.merge({async_permissions_update: true})
94 @object.update_attributes!(attrs_to_update)
102 def render_404_if_no_object
103 if params[:action] == 'contents'
111 elsif (@object = User.where(uuid: params[:uuid]).first)
112 # "Home" pseudo-project
123 load_searchable_objects
125 :kind => "arvados#objectList",
130 :items => @objects.as_api_response(nil)
132 if params[:count] != 'none'
133 list[:items_available] = @items_available
136 list[:included] = @extra_included.as_api_response(nil, {select: @select})
142 # The purpose of this endpoint is to return the toplevel set of
143 # groups which are *not* reachable through a direct ownership
144 # chain of projects starting from the current user account. In
145 # other words, groups which to which access was granted via a
146 # permission link or chain of links.
148 # This also returns (in the "included" field) the objects that own
149 # those projects (users or non-project groups).
152 # The intended use of this endpoint is to support clients which
153 # wish to browse those projects which are visible to the user but
154 # are not part of the "home" project.
156 load_limit_offset_order_params
159 @objects = exclude_home Group.readable_by(*@read_users), Group
161 apply_where_limit_order_params
163 if params["include"] == "owner_uuid"
164 owners = @objects.map(&:owner_uuid).to_set
166 [Group, User].each do |klass|
167 @extra_included += klass.readable_by(*@read_users).where(uuid: owners.to_a).to_a
174 def self._shared_requires_parameters
175 rp = self._index_requires_parameters
176 rp[:include] = { type: 'string', required: false }
182 def load_searchable_objects
186 # Reload the orders param, this time without prefixing unqualified
187 # columns ("name" => "groups.name"). Here, unqualified orders
188 # apply to each table being searched, not "groups".
189 load_limit_offset_order_params(fill_table_names: false)
191 if params['count'] == 'none' and @offset != 0 and (params['last_object_class'].nil? or params['last_object_class'].empty?)
192 # can't use offset without getting counts, so
193 # fall back to count=exact behavior.
194 params['count'] = 'exact'
195 set_count_none = true
198 # Trick apply_where_limit_order_params into applying suitable
199 # per-table values. *_all are the real ones we'll apply to the
203 # save the orders from the current request as determined by load_param,
204 # but otherwise discard them because we're going to be getting objects
206 request_orders = @orders.clone
209 request_filters = @filters
212 Job, PipelineInstance, PipelineTemplate, ContainerRequest, Workflow,
214 Human, Specimen, Trait]
216 table_names = Hash[klasses.collect { |k| [k, k.table_name] }]
218 disabled_methods = Rails.configuration.API.DisabledAPIs
219 avail_klasses = table_names.select{|k, t| !disabled_methods[t+'.index']}
220 klasses = avail_klasses.keys
222 request_filters.each do |col, op, val|
223 if col.index('.') && !table_names.values.include?(col.split('.', 2)[0])
224 raise ArgumentError.new("Invalid attribute '#{col}' in filter")
229 request_filters.each do |col,op,val|
231 (val.is_a?(Array) ? val : [val]).each do |type|
232 type = type.split('#')[-1]
233 type[0] = type[0].capitalize
234 wanted_klasses << type
241 if params['recursive']
242 filter_by_owner[:owner_uuid] = [@object.uuid] + @object.descendant_project_uuids
244 filter_by_owner[:owner_uuid] = @object.uuid
247 if params['exclude_home_project']
248 raise ArgumentError.new "Cannot use 'exclude_home_project' with a parent object"
252 included_by_uuid = {}
254 seen_last_class = false
255 klasses.each do |klass|
256 # check if current klass is same as params['last_object_class']
257 seen_last_class = true if((params['count'].andand.==('none')) and
258 (params['last_object_class'].nil? or
259 params['last_object_class'].empty? or
260 params['last_object_class'] == klass.to_s))
262 # if klasses are specified, skip all other klass types
263 next if wanted_klasses.any? and !wanted_klasses.include?(klass.to_s)
265 # if specified, and count=none, then only look at the klass in
267 # for whatever reason, this parameter exists separately from 'wanted_klasses'
268 next if params['count'] == 'none' and !seen_last_class
270 # don't process rest of object types if we already have needed number of objects
271 break if params['count'] == 'none' and all_objects.size >= limit_all
273 # If the currently requested orders specifically match the
274 # table_name for the current klass, apply that order.
275 # Otherwise, order by recency.
277 request_orders.andand.find { |r| r =~ /^#{klass.table_name}\./i || r !~ /\./ } ||
278 klass.default_orders.join(", ")
281 where_conds = filter_by_owner
282 if klass == Collection
283 @select = klass.selectable_attributes - ["manifest_text", "unsigned_manifest_text"]
285 where_conds = where_conds.merge(group_class: ["project","filter"])
288 @filters = request_filters.map do |col, op, val|
291 elsif (col = col.split('.', 2))[0] == klass.table_name
298 @objects = klass.readable_by(*@read_users, {
299 :include_trash => params[:include_trash],
300 :include_old_versions => params[:include_old_versions]
301 }).order(request_order).where(where_conds)
303 if params['exclude_home_project']
304 @objects = exclude_home @objects, klass
307 # Adjust the limit based on number of objects fetched so far
308 klass_limit = limit_all - all_objects.count
310 apply_where_limit_order_params klass
312 # This actually fetches the objects
313 klass_object_list = object_list(model_class: klass)
315 # If count=none, :items_available will be nil, and offset is
317 klass_items_available = klass_object_list[:items_available] || 0
318 @items_available += klass_items_available
319 @offset = [@offset - klass_items_available, 0].max
321 # Add objects to the list of objects to be returned.
322 all_objects += klass_object_list[:items]
324 if klass_object_list[:limit] < klass_limit
325 # object_list() had to reduce @limit to comply with
326 # max_index_database_read. From now on, we'll do all queries
327 # with limit=0 and just accumulate items_available.
328 limit_all = all_objects.count
331 if params["include"] == "owner_uuid"
332 owners = klass_object_list[:items].map {|i| i[:owner_uuid]}.to_set
333 [Group, User].each do |ownerklass|
334 ownerklass.readable_by(*@read_users).where(uuid: owners.to_a).each do |ow|
335 included_by_uuid[ow.uuid] = ow
342 @extra_included = included_by_uuid.values
346 params['count'] = 'none'
349 @objects = all_objects
356 def exclude_home objectlist, klass
357 # select records that are readable by current user AND
358 # the owner_uuid is a user (but not the current user) OR
359 # the owner_uuid is not readable by the current user
360 # the owner_uuid is a group but group_class is not a project
362 read_parent_check = if current_user.is_admin
365 "NOT EXISTS(SELECT 1 FROM #{PERMISSION_VIEW} WHERE "+
366 "user_uuid=(:user_uuid) AND target_uuid=#{klass.table_name}.owner_uuid AND perm_level >= 1) OR "
369 objectlist.where("#{klass.table_name}.owner_uuid IN (SELECT users.uuid FROM users WHERE users.uuid != (:user_uuid)) OR "+
371 "EXISTS(SELECT 1 FROM groups as gp where gp.uuid=#{klass.table_name}.owner_uuid and gp.group_class != 'project')",
372 user_uuid: current_user.uuid)