1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
5 class Link < ArvadosModel
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
17 api_accessible :user, extend: :common do |t|
28 if k = ArvadosModel::resource_class_for_uuid(head_uuid)
34 if k = ArvadosModel::resource_class_for_uuid(tail_uuid)
41 def permission_to_attach_to_objects
42 # Anonymous users cannot write links
43 return false if !current_user
45 # All users can write links that don't affect permissions
46 return true if self.link_class != 'permission'
48 # Administrators can grant permissions
49 return true if current_user.is_admin
51 # All users can grant permissions on objects they own or can manage
52 head_obj = ArvadosModel.find_by_uuid(head_uuid)
53 return true if current_user.can?(manage: head_obj)
59 def maybe_invalidate_permissions_cache
60 if self.link_class == 'permission'
61 # Clearing the entire permissions cache can generate many
62 # unnecessary queries if many active users are not affected by
63 # this change. In such cases it would be better to search cached
64 # permissions for head_uuid and tail_uuid, and invalidate the
65 # cache for only those users. (This would require a browseable
67 User.invalidate_permissions_cache db_current_time.to_i
71 def name_links_are_obsolete
72 if link_class == 'name'
73 errors.add('name', 'Name links are obsolete')
80 # A user is permitted to create, update or modify a permission link
81 # if and only if they have "manage" permission on the object
82 # indicated by the permission link's head_uuid.
84 # All other links are treated as regular ArvadosModel objects.
86 def ensure_owner_uuid_is_permitted
87 if link_class == 'permission'
88 ob = ArvadosModel.find_by_uuid(head_uuid)
89 raise PermissionDeniedError unless current_user.can?(manage: ob)
90 # All permission links should be owned by the system user.
91 self.owner_uuid = system_user_uuid