1 class Link < ArvadosModel
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
15 api_accessible :user, extend: :common do |t|
22 t.add :head, :if => :head
23 t.add :tail, :if => :tail
28 @properties ||= Hash.new
34 def permission_to_attach_to_objects
35 # Anonymous users cannot write links
36 return false if !current_user
38 # All users can write links that don't affect permissions
39 return true if self.link_class != 'permission'
41 # Administrators can grant permissions
42 return true if current_user.is_admin
44 # All users can grant permissions on objects they own
45 head_obj = self.class.
46 kind_class(self.head_kind).
47 where('uuid=?',head_uuid).
50 return true if head_obj.owner_uuid == current_user.uuid
53 # Users with "can_grant" permission on an object can grant
54 # permissions on that object
55 has_grant_permission = self.class.
56 where('link_class=? AND name=? AND tail_uuid=? AND head_uuid=?',
57 'permission', 'can_grant', current_user.uuid, self.head_uuid).
59 return true if has_grant_permission
65 def maybe_invalidate_permissions_cache
66 if self.link_class == 'permission'
67 # Clearing the entire permissions cache can generate many
68 # unnecessary queries if many active users are not affected by
69 # this change. In such cases it would be better to search cached
70 # permissions for head_uuid and tail_uuid, and invalidate the
71 # cache for only those users. (This would require a browseable
73 User.invalidate_permissions_cache