ac3efe310dc6435ae5b4b303991778c84838eb41
[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   serialize :properties, Hash
10   before_create :permission_to_attach_to_objects
11   before_update :permission_to_attach_to_objects
12   after_update :maybe_invalidate_permissions_cache
13   after_create :maybe_invalidate_permissions_cache
14   after_destroy :maybe_invalidate_permissions_cache
15   validate :name_links_are_obsolete
16
17   api_accessible :user, extend: :common do |t|
18     t.add :tail_uuid
19     t.add :link_class
20     t.add :name
21     t.add :head_uuid
22     t.add :head_kind
23     t.add :tail_kind
24     t.add :properties
25   end
26
27   def head_kind
28     if k = ArvadosModel::resource_class_for_uuid(head_uuid)
29       k.kind
30     end
31   end
32
33   def tail_kind
34     if k = ArvadosModel::resource_class_for_uuid(tail_uuid)
35       k.kind
36     end
37   end
38
39   protected
40
41   def permission_to_attach_to_objects
42     # Anonymous users cannot write links
43     return false if !current_user
44
45     # All users can write links that don't affect permissions
46     return true if self.link_class != 'permission'
47
48     # Administrators can grant permissions
49     return true if current_user.is_admin
50
51     head_obj = ArvadosModel.find_by_uuid(head_uuid)
52
53     # No permission links can be pointed to past collection versions
54     return false if head_obj.is_a?(Collection) && head_obj.current_version_uuid != head_uuid
55
56     # All users can grant permissions on objects they own or can manage
57     return true if current_user.can?(manage: head_obj)
58
59     # Default = deny.
60     false
61   end
62
63   def maybe_invalidate_permissions_cache
64     if self.link_class == 'permission'
65       # Clearing the entire permissions cache can generate many
66       # unnecessary queries if many active users are not affected by
67       # this change. In such cases it would be better to search cached
68       # permissions for head_uuid and tail_uuid, and invalidate the
69       # cache for only those users. (This would require a browseable
70       # cache.)
71       User.invalidate_permissions_cache
72     end
73   end
74
75   def name_links_are_obsolete
76     if link_class == 'name'
77       errors.add('name', 'Name links are obsolete')
78       false
79     else
80       true
81     end
82   end
83
84   # A user is permitted to create, update or modify a permission link
85   # if and only if they have "manage" permission on the object
86   # indicated by the permission link's head_uuid.
87   #
88   # All other links are treated as regular ArvadosModel objects.
89   #
90   def ensure_owner_uuid_is_permitted
91     if link_class == 'permission'
92       ob = ArvadosModel.find_by_uuid(head_uuid)
93       raise PermissionDeniedError unless current_user.can?(manage: ob)
94       # All permission links should be owned by the system user.
95       self.owner_uuid = system_user_uuid
96       return true
97     else
98       super
99     end
100   end
101
102 end