Merge branch 'master' into 1971-show-image-thumbnails
[arvados.git] / services / api / app / models / link.rb
1 class Link < ArvadosModel
2   include AssignUuid
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
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
53     head_obj = self.class.
54       kind_class(self.head_uuid).
55       where('uuid=?',head_uuid).
56       first
57     if head_obj
58       return true if head_obj.owner_uuid == current_user.uuid
59     end
60
61     # Users with "can_grant" permission on an object can grant
62     # permissions on that object
63     has_grant_permission = self.class.
64       where('link_class=? AND name=? AND tail_uuid=? AND head_uuid=?',
65             'permission', 'can_grant', current_user.uuid, self.head_uuid).
66       count > 0
67     return true if has_grant_permission
68
69     # Default = deny.
70     false
71   end
72
73   def maybe_invalidate_permissions_cache
74     if self.link_class == 'permission'
75       # Clearing the entire permissions cache can generate many
76       # unnecessary queries if many active users are not affected by
77       # this change. In such cases it would be better to search cached
78       # permissions for head_uuid and tail_uuid, and invalidate the
79       # cache for only those users. (This would require a browseable
80       # cache.)
81       User.invalidate_permissions_cache
82     end
83   end
84 end