Merge branch '18631-shell-login-sync'
[arvados.git] / services / api / app / controllers / arvados / v1 / links_controller.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 class Arvados::V1::LinksController < ApplicationController
6
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}'",
10                  status: 422)
11       nil
12     else
13       true
14     end
15   end
16
17   def create
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]
20
21     resource_attrs.delete :head_kind
22     resource_attrs.delete :tail_kind
23     super
24   end
25
26   def get_permissions
27     if current_user.andand.can?(manage: @object)
28       # find all links and return them
29       @objects = Link.unscoped.where(link_class: "permission",
30                                      head_uuid: params[:uuid])
31       @offset = 0
32       @limit = @objects.count
33       render_list
34     else
35       render :json => { errors: ['Forbidden'] }.to_json, status: 403
36     end
37   end
38
39   protected
40
41   def find_object_by_uuid
42     if params[:id] && params[:id].match(/\D/)
43       params[:uuid] = params.delete :id
44     end
45     if action_name == 'get_permissions'
46       # get_permissions accepts a UUID for any kind of object.
47       @object = ArvadosModel::resource_class_for_uuid(params[:uuid])
48         .readable_by(*@read_users)
49         .where(uuid: params[:uuid])
50         .first
51     elsif !current_user
52       super
53     else
54       # The usual permission-filtering index query is unnecessarily
55       # inefficient, and doesn't match all permission links that
56       # should be visible (see #18865).  Instead, we look up the link
57       # by UUID, then check whether (a) its tail_uuid is the current
58       # user or (b) its head_uuid is an object the current_user
59       # can_manage.
60       link = Link.unscoped.where(uuid: params[:uuid]).first
61       if link && link.link_class != 'permission'
62         # Not a permission link. Re-fetch using generic
63         # permission-filtering query.
64         super
65       elsif link && (current_user.uuid == link.tail_uuid ||
66                      current_user.can?(manage: link.head_uuid))
67         # Permission granted.
68         @object = link
69       else
70         # Permission denied, i.e., link is invisible => 404.
71         @object = nil
72       end
73     end
74   end
75
76   # Overrides ApplicationController load_where_param
77   def load_where_param
78     super
79
80     # head_kind and tail_kind columns are now virtual,
81     # equivalent functionality is now provided by
82     # 'is_a', so fix up any old-style 'where' clauses.
83     if @where
84       @filters ||= []
85       if @where[:head_kind]
86         @filters << ['head_uuid', 'is_a', @where[:head_kind]]
87         @where.delete :head_kind
88       end
89       if @where[:tail_kind]
90         @filters << ['tail_uuid', 'is_a', @where[:tail_kind]]
91         @where.delete :tail_kind
92       end
93     end
94   end
95
96   # Overrides ApplicationController load_filters_param
97   def load_filters_param
98     super
99
100     # head_kind and tail_kind columns are now virtual,
101     # equivalent functionality is now provided by
102     # 'is_a', so fix up any old-style 'filter' clauses.
103     @filters = @filters.map do |k|
104       if k[0] == 'head_kind' and k[1] == '='
105         ['head_uuid', 'is_a', k[2]]
106       elsif k[0] == 'tail_kind' and k[1] == '='
107         ['tail_uuid', 'is_a', k[2]]
108       else
109         k
110       end
111     end
112
113     # If the provided filters are enough to limit the results to
114     # permission links with specific head_uuids or
115     # tail_uuid=current_user, bypass the normal readable_by query
116     # (which doesn't match all can_manage-able items, see #18865) --
117     # just ensure the current user actually has can_manage permission
118     # for the provided head_uuids, removing any that don't. At that
119     # point the caller's filters are an effective permission filter.
120     if @filters.include?(['link_class', '=', 'permission'])
121       @filters.map do |k|
122         if k[0] == 'tail_uuid' && k[1] == '=' && k[2] == current_user.uuid
123           @objects = Link.unscoped
124         elsif k[0] == 'head_uuid'
125           if k[1] == '=' && current_user.can?(manage: k[2])
126             @objects = Link.unscoped
127           elsif k[1] == 'in'
128             # Modify the filter operand element (k[2]) in place,
129             # removing any non-permitted UUIDs.
130             k[2].select! do |head_uuid|
131               current_user.can?(manage: head_uuid)
132             end
133             @objects = Link.unscoped
134           end
135         end
136       end
137     end
138   end
139
140 end