8784: Fix test for latest firefox.
[arvados.git] / services / api / app / controllers / arvados / v1 / links_controller.rb
1 class Arvados::V1::LinksController < ApplicationController
2
3   def check_uuid_kind uuid, kind
4     if kind and ArvadosModel::resource_class_for_uuid(uuid).andand.kind != kind
5       send_error("'#{kind}' does not match uuid '#{uuid}', expected '#{ArvadosModel::resource_class_for_uuid(uuid).andand.kind}'",
6                  status: 422)
7       nil
8     else
9       true
10     end
11   end
12
13   def create
14     return if ! check_uuid_kind resource_attrs[:head_uuid], resource_attrs[:head_kind]
15     return if ! check_uuid_kind resource_attrs[:tail_uuid], resource_attrs[:tail_kind]
16
17     resource_attrs.delete :head_kind
18     resource_attrs.delete :tail_kind
19     super
20   end
21
22   def get_permissions
23     if current_user.andand.can?(manage: @object)
24       # find all links and return them
25       @objects = Link.where(link_class: "permission",
26                             head_uuid: params[:uuid])
27       @offset = 0
28       @limit = @objects.count
29       render_list
30     else
31       render :json => { errors: ['Forbidden'] }.to_json, status: 403
32     end
33   end
34
35   protected
36
37   def find_object_by_uuid
38     if action_name == 'get_permissions'
39       # get_permissions accepts a UUID for any kind of object.
40       @object = ArvadosModel::resource_class_for_uuid(params[:uuid])
41         .readable_by(*@read_users)
42         .where(uuid: params[:uuid])
43         .first
44     else
45       super
46       if @object.nil?
47         # Normally group permission links are not readable_by users.
48         # Make an exception for users with permission to manage the group.
49         # FIXME: Solve this more generally - see the controller tests.
50         link = Link.find_by_uuid(params[:uuid])
51         if (not link.nil?) and
52             (link.link_class == "permission") and
53             (@read_users.any? { |u| u.can?(manage: link.head_uuid) })
54           @object = link
55         end
56       end
57     end
58   end
59
60   # Overrides ApplicationController load_where_param
61   def load_where_param
62     super
63
64     # head_kind and tail_kind columns are now virtual,
65     # equivilent functionality is now provided by
66     # 'is_a', so fix up any old-style 'where' clauses.
67     if @where
68       @filters ||= []
69       if @where[:head_kind]
70         @filters << ['head_uuid', 'is_a', @where[:head_kind]]
71         @where.delete :head_kind
72       end
73       if @where[:tail_kind]
74         @filters << ['tail_uuid', 'is_a', @where[:tail_kind]]
75         @where.delete :tail_kind
76       end
77     end
78   end
79
80   # Overrides ApplicationController load_filters_param
81   def load_filters_param
82     super
83
84     # head_kind and tail_kind columns are now virtual,
85     # equivilent functionality is now provided by
86     # 'is_a', so fix up any old-style 'filter' clauses.
87     @filters = @filters.map do |k|
88       if k[0] == 'head_kind' and k[1] == '='
89         ['head_uuid', 'is_a', k[2]]
90       elsif k[0] == 'tail_kind' and k[1] == '='
91         ['tail_uuid', 'is_a', k[2]]
92       else
93         k
94       end
95     end
96   end
97
98 end