Merge branch '8784-dir-listings'
[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.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 action_name == 'get_permissions'
43       # get_permissions accepts a UUID for any kind of object.
44       @object = ArvadosModel::resource_class_for_uuid(params[:uuid])
45         .readable_by(*@read_users)
46         .where(uuid: params[:uuid])
47         .first
48     else
49       super
50       if @object.nil?
51         # Normally group permission links are not readable_by users.
52         # Make an exception for users with permission to manage the group.
53         # FIXME: Solve this more generally - see the controller tests.
54         link = Link.find_by_uuid(params[:uuid])
55         if (not link.nil?) and
56             (link.link_class == "permission") and
57             (@read_users.any? { |u| u.can?(manage: link.head_uuid) })
58           @object = link
59         end
60       end
61     end
62   end
63
64   # Overrides ApplicationController load_where_param
65   def load_where_param
66     super
67
68     # head_kind and tail_kind columns are now virtual,
69     # equivilent functionality is now provided by
70     # 'is_a', so fix up any old-style 'where' clauses.
71     if @where
72       @filters ||= []
73       if @where[:head_kind]
74         @filters << ['head_uuid', 'is_a', @where[:head_kind]]
75         @where.delete :head_kind
76       end
77       if @where[:tail_kind]
78         @filters << ['tail_uuid', 'is_a', @where[:tail_kind]]
79         @where.delete :tail_kind
80       end
81     end
82   end
83
84   # Overrides ApplicationController load_filters_param
85   def load_filters_param
86     super
87
88     # head_kind and tail_kind columns are now virtual,
89     # equivilent functionality is now provided by
90     # 'is_a', so fix up any old-style 'filter' clauses.
91     @filters = @filters.map do |k|
92       if k[0] == 'head_kind' and k[1] == '='
93         ['head_uuid', 'is_a', k[2]]
94       elsif k[0] == 'tail_kind' and k[1] == '='
95         ['tail_uuid', 'is_a', k[2]]
96       else
97         k
98       end
99     end
100   end
101
102 end