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