1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
5 class Arvados::V1::LinksController < ApplicationController
7 def check_uuid_kind uuid, kind
8 if kind and ArvadosModel::resource_class_for_uuid(uuid).andand.kind != kind
9 send_error("'#{kind}' does not match uuid '#{uuid}', expected '#{ArvadosModel::resource_class_for_uuid(uuid).andand.kind}'",
18 return if ! check_uuid_kind resource_attrs[:head_uuid], resource_attrs[:head_kind]
19 return if ! check_uuid_kind resource_attrs[:tail_uuid], resource_attrs[:tail_kind]
21 resource_attrs.delete :head_kind
22 resource_attrs.delete :tail_kind
24 if resource_attrs[:link_class] == 'permission' && Link::PermLevel[resource_attrs[:name]]
26 lock. # select ... for update
27 where(link_class: 'permission',
28 tail_uuid: resource_attrs[:tail_uuid],
29 head_uuid: resource_attrs[:head_uuid],
30 name: Link::PermLevel.keys).first
33 if Link::PermLevel[resource_attrs[:name]] > Link::PermLevel[existing.name]
34 # upgrade existing permission link to the requested level.
37 # no-op: existing permission is already greater or equal to
38 # the newly requested permission.
42 elsif resource_attrs[:link_class] == 'permission' &&
43 resource_attrs[:name] == 'can_login' &&
44 resource_attrs[:properties].respond_to?(:has_key?) &&
45 resource_attrs[:properties].has_key?(:username)
47 lock. # select ... for update
48 where(link_class: 'permission',
49 tail_uuid: resource_attrs[:tail_uuid],
50 head_uuid: resource_attrs[:head_uuid]).
51 where('properties @> ?', SafeJSON.dump({'username' => resource_attrs[:properties][:username]})).
63 if current_user.andand.can?(manage: @object)
64 # find all links and return them
65 @objects = Link.unscoped.where(link_class: "permission",
66 head_uuid: params[:uuid])
68 @limit = @objects.count
71 render :json => { errors: ['Forbidden'] }.to_json, status: 403
77 def find_object_by_uuid(with_lock: false)
78 if params[:id] && params[:id].match(/\D/)
79 params[:uuid] = params.delete :id
81 if action_name == 'get_permissions'
82 # get_permissions accepts a UUID for any kind of object.
83 @object = ArvadosModel::resource_class_for_uuid(params[:uuid])
84 .readable_by(*@read_users)
85 .where(uuid: params[:uuid])
88 super(with_lock: with_lock)
90 # The usual permission-filtering index query is unnecessarily
91 # inefficient, and doesn't match all permission links that
92 # should be visible (see #18865). Instead, we look up the link
93 # by UUID, then check whether (a) its tail_uuid is the current
94 # user or (b) its head_uuid is an object the current_user
97 if with_lock && Rails.configuration.API.LockBeforeUpdate
100 link = model.unscoped.where(uuid: params[:uuid]).first
101 if link && link.link_class != 'permission'
102 # Not a permission link. Re-fetch using generic
103 # permission-filtering query.
104 super(with_lock: with_lock)
105 elsif link && (current_user.uuid == link.tail_uuid ||
106 current_user.can?(manage: link.head_uuid))
107 # Permission granted.
110 # Permission denied, i.e., link is invisible => 404.
116 # Overrides ApplicationController load_where_param
120 # head_kind and tail_kind columns are now virtual,
121 # equivalent functionality is now provided by
122 # 'is_a', so fix up any old-style 'where' clauses.
125 if @where[:head_kind]
126 @filters << ['head_uuid', 'is_a', @where[:head_kind]]
127 @where.delete :head_kind
129 if @where[:tail_kind]
130 @filters << ['tail_uuid', 'is_a', @where[:tail_kind]]
131 @where.delete :tail_kind
136 # Overrides ApplicationController load_filters_param
137 def load_filters_param
140 # head_kind and tail_kind columns are now virtual,
141 # equivalent functionality is now provided by
142 # 'is_a', so fix up any old-style 'filter' clauses.
143 @filters = @filters.map do |k|
144 if k[0] == 'head_kind' and k[1] == '='
145 ['head_uuid', 'is_a', k[2]]
146 elsif k[0] == 'tail_kind' and k[1] == '='
147 ['tail_uuid', 'is_a', k[2]]
153 # If the provided filters are enough to limit the results to
154 # permission links with specific head_uuids or
155 # tail_uuid=current_user, bypass the normal readable_by query
156 # (which doesn't match all can_manage-able items, see #18865) --
157 # just ensure the current user actually has can_manage permission
158 # for the provided head_uuids, removing any that don't. At that
159 # point the caller's filters are an effective permission filter.
160 if @filters.include?(['link_class', '=', 'permission'])
162 if k[0] == 'tail_uuid' && k[1] == '=' && k[2] == current_user.uuid
163 @objects = Link.unscoped
164 elsif k[0] == 'head_uuid'
165 if k[1] == '=' && current_user.can?(manage: k[2])
166 @objects = Link.unscoped
168 # Modify the filter operand element (k[2]) in place,
169 # removing any non-permitted UUIDs.
170 k[2].select! do |head_uuid|
171 current_user.can?(manage: head_uuid)
173 @objects = Link.unscoped