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_filter :find_object_by_uuid, only: :shared
11 skip_before_filter :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._contents_requires_parameters
23 params = _index_requires_parameters.
26 type: 'string', required: false, default: nil
29 type: 'boolean', required: false, description: 'Include contents from child groups recursively.'
32 params.delete(:select)
36 def render_404_if_no_object
37 if params[:action] == 'contents'
45 elsif (@object = User.where(uuid: params[:uuid]).first)
46 # "Home" pseudo-project
57 load_searchable_objects
59 :kind => "arvados#objectList",
64 :items_available => @items_available,
65 :items => @objects.as_api_response(nil)
68 list[:included] = @extra_included.as_api_response(nil, {select: @select})
74 # The purpose of this endpoint is to return the toplevel set of
75 # groups which are *not* reachable through a direct ownership
76 # chain of projects starting from the current user account. In
77 # other words, groups which to which access was granted via a
78 # permission link or chain of links.
80 # This also returns (in the "included" field) the objects that own
81 # those projects (users or non-project groups).
84 # The intended use of this endpoint is to support clients which
85 # wish to browse those projects which are visible to the user but
86 # are not part of the "home" project.
88 load_limit_offset_order_params
91 @objects = exclude_home Group.readable_by(*@read_users), Group
93 apply_where_limit_order_params
95 if params["include"] == "owner_uuid"
96 owners = @objects.map(&:owner_uuid).to_set
98 [Group, User].each do |klass|
99 @extra_included += klass.readable_by(*@read_users).where(uuid: owners.to_a).to_a
106 def self._shared_requires_parameters
107 rp = self._index_requires_parameters
108 rp[:include] = { type: 'string', required: false }
114 def load_searchable_objects
118 # Reload the orders param, this time without prefixing unqualified
119 # columns ("name" => "groups.name"). Here, unqualified orders
120 # apply to each table being searched, not "groups".
121 load_limit_offset_order_params(fill_table_names: false)
123 # Trick apply_where_limit_order_params into applying suitable
124 # per-table values. *_all are the real ones we'll apply to the
128 # save the orders from the current request as determined by load_param,
129 # but otherwise discard them because we're going to be getting objects
131 request_orders = @orders.clone
134 request_filters = @filters
137 Job, PipelineInstance, PipelineTemplate, ContainerRequest, Workflow,
139 Human, Specimen, Trait]
141 table_names = Hash[klasses.collect { |k| [k, k.table_name] }]
143 disabled_methods = Rails.configuration.disable_api_methods
144 avail_klasses = table_names.select{|k, t| !disabled_methods.include?(t+'.index')}
145 klasses = avail_klasses.keys
147 request_filters.each do |col, op, val|
148 if col.index('.') && !table_names.values.include?(col.split('.', 2)[0])
149 raise ArgumentError.new("Invalid attribute '#{col}' in filter")
154 request_filters.each do |col,op,val|
156 (val.is_a?(Array) ? val : [val]).each do |type|
157 type = type.split('#')[-1]
158 type[0] = type[0].capitalize
159 wanted_klasses << type
166 if params['recursive']
167 filter_by_owner[:owner_uuid] = [@object.uuid] + @object.descendant_project_uuids
169 filter_by_owner[:owner_uuid] = @object.uuid
172 if params['exclude_home_project']
173 raise ArgumentError.new "Cannot use 'exclude_home_project' with a parent object"
177 included_by_uuid = {}
179 seen_last_class = false
180 klasses.each do |klass|
181 @offset = 0 if seen_last_class # reset offset for the new next type being processed
183 # if current klass is same as params['last_object_class'], mark that fact
184 seen_last_class = true if((params['count'].andand.==('none')) and
185 (params['last_object_class'].nil? or
186 params['last_object_class'].empty? or
187 params['last_object_class'] == klass.to_s))
189 # if klasses are specified, skip all other klass types
190 next if wanted_klasses.any? and !wanted_klasses.include?(klass.to_s)
192 # don't reprocess klass types that were already seen
193 next if params['count'] == 'none' and !seen_last_class
195 # don't process rest of object types if we already have needed number of objects
196 break if params['count'] == 'none' and all_objects.size >= limit_all
198 # If the currently requested orders specifically match the
199 # table_name for the current klass, apply that order.
200 # Otherwise, order by recency.
202 request_orders.andand.find { |r| r =~ /^#{klass.table_name}\./i || r !~ /\./ } ||
203 klass.default_orders.join(", ")
206 where_conds = filter_by_owner
207 if klass == Collection
208 @select = klass.selectable_attributes - ["manifest_text"]
210 where_conds = where_conds.merge(group_class: "project")
213 @filters = request_filters.map do |col, op, val|
216 elsif (col = col.split('.', 2))[0] == klass.table_name
223 @objects = klass.readable_by(*@read_users, {:include_trash => params[:include_trash]}).
224 order(request_order).where(where_conds)
226 if params['exclude_home_project']
227 @objects = exclude_home @objects, klass
230 klass_limit = limit_all - all_objects.count
232 apply_where_limit_order_params klass
233 klass_object_list = object_list(model_class: klass)
234 klass_items_available = klass_object_list[:items_available] || 0
235 @items_available += klass_items_available
236 @offset = [@offset - klass_items_available, 0].max
237 all_objects += klass_object_list[:items]
239 if klass_object_list[:limit] < klass_limit
240 # object_list() had to reduce @limit to comply with
241 # max_index_database_read. From now on, we'll do all queries
242 # with limit=0 and just accumulate items_available.
243 limit_all = all_objects.count
246 if params["include"] == "owner_uuid"
247 owners = klass_object_list[:items].map {|i| i[:owner_uuid]}.to_set
248 [Group, User].each do |ownerklass|
249 ownerklass.readable_by(*@read_users).where(uuid: owners.to_a).each do |ow|
250 included_by_uuid[ow.uuid] = ow
257 @extra_included = included_by_uuid.values
260 @objects = all_objects
267 def exclude_home objectlist, klass
268 # select records that are readable by current user AND
269 # the owner_uuid is a user (but not the current user) OR
270 # the owner_uuid is not readable by the current user
271 # the owner_uuid is a group but group_class is not a project
273 read_parent_check = if current_user.is_admin
276 "NOT EXISTS(SELECT 1 FROM #{PERMISSION_VIEW} WHERE "+
277 "user_uuid=(:user_uuid) AND target_uuid=#{klass.table_name}.owner_uuid AND perm_level >= 1) OR "
280 objectlist.where("#{klass.table_name}.owner_uuid IN (SELECT users.uuid FROM users WHERE users.uuid != (:user_uuid)) OR "+
282 "EXISTS(SELECT 1 FROM groups as gp where gp.uuid=#{klass.table_name}.owner_uuid and gp.group_class != 'project')",
283 user_uuid: current_user.uuid)