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