X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/5dbf72803717f58b4848b6a6490375450916e84d..b5c15775caf865d8fed5d4839d4a082f6518bba4:/services/api/app/controllers/arvados/v1/links_controller.rb diff --git a/services/api/app/controllers/arvados/v1/links_controller.rb b/services/api/app/controllers/arvados/v1/links_controller.rb index 384ffd64b7..c956bfc9b4 100644 --- a/services/api/app/controllers/arvados/v1/links_controller.rb +++ b/services/api/app/controllers/arvados/v1/links_controller.rb @@ -20,14 +20,50 @@ class Arvados::V1::LinksController < ApplicationController resource_attrs.delete :head_kind resource_attrs.delete :tail_kind + + if resource_attrs[:link_class] == 'permission' && Link::PermLevel[resource_attrs[:name]] + existing = Link. + lock. # select ... for update + where(link_class: 'permission', + tail_uuid: resource_attrs[:tail_uuid], + head_uuid: resource_attrs[:head_uuid], + name: Link::PermLevel.keys).first + if existing + @object = existing + if Link::PermLevel[resource_attrs[:name]] > Link::PermLevel[existing.name] + # upgrade existing permission link to the requested level. + return update + else + # no-op: existing permission is already greater or equal to + # the newly requested permission. + return show + end + end + elsif resource_attrs[:link_class] == 'permission' && + resource_attrs[:name] == 'can_login' && + resource_attrs[:properties].respond_to?(:has_key?) && + resource_attrs[:properties].has_key?(:username) + existing = Link. + lock. # select ... for update + where(link_class: 'permission', + tail_uuid: resource_attrs[:tail_uuid], + head_uuid: resource_attrs[:head_uuid]). + where('properties @> ?', SafeJSON.dump({'username' => resource_attrs[:properties][:username]})). + first + if existing + @object = existing + return show + end + end + super end def get_permissions if current_user.andand.can?(manage: @object) # find all links and return them - @objects = Link.where(link_class: "permission", - head_uuid: params[:uuid]) + @objects = Link.unscoped.where(link_class: "permission", + head_uuid: params[:uuid]) @offset = 0 @limit = @objects.count render_list @@ -38,15 +74,42 @@ class Arvados::V1::LinksController < ApplicationController protected - def find_object_by_uuid + def find_object_by_uuid(with_lock: false) + if params[:id] && params[:id].match(/\D/) + params[:uuid] = params.delete :id + end if action_name == 'get_permissions' # get_permissions accepts a UUID for any kind of object. @object = ArvadosModel::resource_class_for_uuid(params[:uuid]) .readable_by(*@read_users) .where(uuid: params[:uuid]) .first + elsif !current_user + super(with_lock: with_lock) else - super + # The usual permission-filtering index query is unnecessarily + # inefficient, and doesn't match all permission links that + # should be visible (see #18865). Instead, we look up the link + # by UUID, then check whether (a) its tail_uuid is the current + # user or (b) its head_uuid is an object the current_user + # can_manage. + model = Link + if with_lock && Rails.configuration.API.LockBeforeUpdate + model = model.lock + end + link = model.unscoped.where(uuid: params[:uuid]).first + if link && link.link_class != 'permission' + # Not a permission link. Re-fetch using generic + # permission-filtering query. + super(with_lock: with_lock) + elsif link && (current_user.uuid == link.tail_uuid || + current_user.can?(manage: link.head_uuid)) + # Permission granted. + @object = link + else + # Permission denied, i.e., link is invisible => 404. + @object = nil + end end end @@ -86,6 +149,32 @@ class Arvados::V1::LinksController < ApplicationController k end end + + # If the provided filters are enough to limit the results to + # permission links with specific head_uuids or + # tail_uuid=current_user, bypass the normal readable_by query + # (which doesn't match all can_manage-able items, see #18865) -- + # just ensure the current user actually has can_manage permission + # for the provided head_uuids, removing any that don't. At that + # point the caller's filters are an effective permission filter. + if @filters.include?(['link_class', '=', 'permission']) + @filters.map do |k| + if k[0] == 'tail_uuid' && k[1] == '=' && k[2] == current_user.uuid + @objects = Link.unscoped + elsif k[0] == 'head_uuid' + if k[1] == '=' && current_user.can?(manage: k[2]) + @objects = Link.unscoped + elsif k[1] == 'in' + # Modify the filter operand element (k[2]) in place, + # removing any non-permitted UUIDs. + k[2].select! do |head_uuid| + current_user.can?(manage: head_uuid) + end + @objects = Link.unscoped + end + end + end + end end end