14873: Fixes unit tests.
[arvados.git] / services / api / app / models / link.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 class Link < ArvadosModel
6   include HasUuid
7   include KindAndEtag
8   include CommonApiTemplate
9
10   # Posgresql JSONB columns should NOT be declared as serialized, Rails 5
11   # already know how to properly treat them.
12
13   before_create :permission_to_attach_to_objects
14   before_update :permission_to_attach_to_objects
15   after_update :maybe_invalidate_permissions_cache
16   after_create :maybe_invalidate_permissions_cache
17   after_destroy :maybe_invalidate_permissions_cache
18   validate :name_links_are_obsolete
19
20   api_accessible :user, extend: :common do |t|
21     t.add :tail_uuid
22     t.add :link_class
23     t.add :name
24     t.add :head_uuid
25     t.add :head_kind
26     t.add :tail_kind
27     t.add :properties
28   end
29
30   def head_kind
31     if k = ArvadosModel::resource_class_for_uuid(head_uuid)
32       k.kind
33     end
34   end
35
36   def tail_kind
37     if k = ArvadosModel::resource_class_for_uuid(tail_uuid)
38       k.kind
39     end
40   end
41
42   protected
43
44   def permission_to_attach_to_objects
45     # Anonymous users cannot write links
46     return false if !current_user
47
48     # All users can write links that don't affect permissions
49     return true if self.link_class != 'permission'
50
51     # Administrators can grant permissions
52     return true if current_user.is_admin
53
54     head_obj = ArvadosModel.find_by_uuid(head_uuid)
55
56     # No permission links can be pointed to past collection versions
57     return false if head_obj.is_a?(Collection) && head_obj.current_version_uuid != head_uuid
58
59     # All users can grant permissions on objects they own or can manage
60     return true if current_user.can?(manage: head_obj)
61
62     # Default = deny.
63     false
64   end
65
66   def maybe_invalidate_permissions_cache
67     if self.link_class == 'permission'
68       # Clearing the entire permissions cache can generate many
69       # unnecessary queries if many active users are not affected by
70       # this change. In such cases it would be better to search cached
71       # permissions for head_uuid and tail_uuid, and invalidate the
72       # cache for only those users. (This would require a browseable
73       # cache.)
74       User.invalidate_permissions_cache
75     end
76   end
77
78   def name_links_are_obsolete
79     if link_class == 'name'
80       errors.add('name', 'Name links are obsolete')
81       false
82     else
83       true
84     end
85   end
86
87   # A user is permitted to create, update or modify a permission link
88   # if and only if they have "manage" permission on the object
89   # indicated by the permission link's head_uuid.
90   #
91   # All other links are treated as regular ArvadosModel objects.
92   #
93   def ensure_owner_uuid_is_permitted
94     if link_class == 'permission'
95       ob = ArvadosModel.find_by_uuid(head_uuid)
96       raise PermissionDeniedError unless current_user.can?(manage: ob)
97       # All permission links should be owned by the system user.
98       self.owner_uuid = system_user_uuid
99       return true
100     else
101       super
102     end
103   end
104
105 end