create content-addressed collections with uuid=locator
[arvados.git] / app / models / link.rb
1 class Link < OrvosModel
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
9   attr_accessor :head
10   attr_accessor :tail
11
12   api_accessible :superuser, :extend => :common do |t|
13     t.add :tail_kind
14     t.add :tail_uuid
15     t.add :link_class
16     t.add :name
17     t.add :head_kind
18     t.add :head_uuid
19     t.add :head, :if => :head
20     t.add :tail, :if => :tail
21     t.add :properties
22   end
23
24   def properties
25     @properties ||= Hash.new
26     super
27   end
28
29   protected
30
31   def permission_to_attach_to_objects
32     # Anonymous users cannot write links
33     return false if !current_user
34
35     # All users can write links that don't affect permissions
36     return true if self.link_class != 'permission'
37
38     # Administrators can grant permissions
39     return true if current_user.is_admin
40
41     # All users can grant permissions on objects they own
42     head_obj = self.class.
43       kind_class(self.head_kind).
44       where('uuid=?',head_uuid).
45       first
46     if head_obj
47       return true if head_obj.owner == current_user.uuid
48     end
49
50     # Users with "can_grant" permission on an object can grant
51     # permissions on that object
52     has_grant_permission = self.class.
53       where('link_class=? AND name=? AND tail_uuid=? AND head_uuid=?',
54             'permission', 'can_grant', current_user.uuid, self.head_uuid).
55       count > 0
56     return true if has_grant_permission
57
58     # Default = deny.
59     false
60   end
61 end