21700: Install Bundler system-wide in Rails postinst
[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
24     if resource_attrs[:link_class] == 'permission' && Link::PermLevel[resource_attrs[:name]]
25       existing = Link.
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
31       if existing
32         @object = existing
33         if Link::PermLevel[resource_attrs[:name]] > Link::PermLevel[existing.name]
34           # upgrade existing permission link to the requested level.
35           return update
36         else
37           # no-op: existing permission is already greater or equal to
38           # the newly requested permission.
39           return show
40         end
41       end
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)
46       existing = Link.
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]})).
52                    first
53       if existing
54         @object = existing
55         return show
56       end
57     end
58
59     super
60   end
61
62   def get_permissions
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])
67       @offset = 0
68       @limit = @objects.count
69       render_list
70     else
71       render :json => { errors: ['Forbidden'] }.to_json, status: 403
72     end
73   end
74
75   protected
76
77   def find_object_by_uuid(with_lock: false)
78     if params[:id] && params[:id].match(/\D/)
79       params[:uuid] = params.delete :id
80     end
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])
86         .first
87     elsif !current_user
88       super(with_lock: with_lock)
89     else
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
95       # can_manage.
96       model = Link
97       if with_lock && Rails.configuration.API.LockBeforeUpdate
98         model = model.lock
99       end
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.
108         @object = link
109       else
110         # Permission denied, i.e., link is invisible => 404.
111         @object = nil
112       end
113     end
114   end
115
116   # Overrides ApplicationController load_where_param
117   def load_where_param
118     super
119
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.
123     if @where
124       @filters ||= []
125       if @where[:head_kind]
126         @filters << ['head_uuid', 'is_a', @where[:head_kind]]
127         @where.delete :head_kind
128       end
129       if @where[:tail_kind]
130         @filters << ['tail_uuid', 'is_a', @where[:tail_kind]]
131         @where.delete :tail_kind
132       end
133     end
134   end
135
136   # Overrides ApplicationController load_filters_param
137   def load_filters_param
138     super
139
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]]
148       else
149         k
150       end
151     end
152
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'])
161       @filters.map do |k|
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
167           elsif k[1] == 'in'
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)
172             end
173             @objects = Link.unscoped
174           end
175         end
176       end
177     end
178   end
179
180 end