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