2873: more code review changes
[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       render :json => { errors: ["'#{kind}' does not match uuid '#{uuid}', expected '#{ArvadosModel::resource_class_for_uuid(uuid).andand.kind}'"] }.to_json, status: 422
6       nil
7     else
8       true
9     end
10   end
11
12   def create
13     return if ! check_uuid_kind resource_attrs[:head_uuid], resource_attrs[:head_kind]
14     return if ! check_uuid_kind resource_attrs[:tail_uuid], resource_attrs[:tail_kind]
15
16     resource_attrs.delete :head_kind
17     resource_attrs.delete :tail_kind
18     super
19   end
20
21   def get_permissions
22     if current_user.can?(manage: @object)
23       # find all links and return them
24       @objects = Link.where(link_class: "permission",
25                             head_uuid: params[:uuid])
26       @offset = 0
27       @limit = @objects.count
28       render_list
29     else
30       render :json => { errors: ['Forbidden'] }.to_json, status: 403
31     end
32   end
33
34   protected
35
36   # Override find_object_by_uuid: the get_permissions method may be
37   # called on a uuid belonging to any class.
38   def find_object_by_uuid
39     if action_name == 'get_permissions'
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     end
47   end
48
49   # Overrides ApplicationController load_where_param
50   def load_where_param
51     super
52
53     # head_kind and tail_kind columns are now virtual,
54     # equivilent functionality is now provided by
55     # 'is_a', so fix up any old-style 'where' clauses.
56     if @where
57       @filters ||= []
58       if @where[:head_kind]
59         @filters << ['head_uuid', 'is_a', @where[:head_kind]]
60         @where.delete :head_kind
61       end
62       if @where[:tail_kind]
63         @filters << ['tail_uuid', 'is_a', @where[:tail_kind]]
64         @where.delete :tail_kind
65       end
66     end
67   end
68
69   # Overrides ApplicationController load_filters_param
70   def load_filters_param
71     super
72
73     # head_kind and tail_kind columns are now virtual,
74     # equivilent functionality is now provided by
75     # 'is_a', so fix up any old-style 'filter' clauses.
76     @filters = @filters.map do |k|
77       if k[0] == 'head_kind' and k[1] == '='
78         ['head_uuid', 'is_a', k[2]]
79       elsif k[0] == 'tail_kind' and k[1] == '='
80         ['tail_uuid', 'is_a', k[2]]
81       else
82         k
83       end
84     end
85   end
86
87 end