Added ArvadosModel.search_for_user to perform full-text search on a model.
[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
12   attr_accessor :head
13   attr_accessor :tail
14
15   api_accessible :user, extend: :common do |t|
16     t.add :tail_kind
17     t.add :tail_uuid
18     t.add :link_class
19     t.add :name
20     t.add :head_kind
21     t.add :head_uuid
22     t.add :head, :if => :head
23     t.add :tail, :if => :tail
24     t.add :properties
25   end
26
27   def properties
28     @properties ||= Hash.new
29     super
30   end
31
32   def self.searchable?
33     true
34   end
35
36   protected
37
38   def permission_to_attach_to_objects
39     # Anonymous users cannot write links
40     return false if !current_user
41
42     # All users can write links that don't affect permissions
43     return true if self.link_class != 'permission'
44
45     # Administrators can grant permissions
46     return true if current_user.is_admin
47
48     # All users can grant permissions on objects they own
49     head_obj = self.class.
50       kind_class(self.head_kind).
51       where('uuid=?',head_uuid).
52       first
53     if head_obj
54       return true if head_obj.owner_uuid == current_user.uuid
55     end
56
57     # Users with "can_grant" permission on an object can grant
58     # permissions on that object
59     has_grant_permission = self.class.
60       where('link_class=? AND name=? AND tail_uuid=? AND head_uuid=?',
61             'permission', 'can_grant', current_user.uuid, self.head_uuid).
62       count > 0
63     return true if has_grant_permission
64
65     # Default = deny.
66     false
67   end
68
69   def maybe_invalidate_permissions_cache
70     if self.link_class == 'permission'
71       # Clearing the entire permissions cache can generate many
72       # unnecessary queries if many active users are not affected by
73       # this change. In such cases it would be better to search cached
74       # permissions for head_uuid and tail_uuid, and invalidate the
75       # cache for only those users. (This would require a browseable
76       # cache.)
77       User.invalidate_permissions_cache
78     end
79   end
80 end