Merge branch '2762-owner-uuid-integrity'
[arvados.git] / services / api / app / models / link.rb
index b685bb6d75cff206a4d9d6652a4f1c0846f799b9..af3918551e441a2ccaea47ea67e19e8f23a73b1f 100644 (file)
@@ -1,5 +1,5 @@
 class Link < ArvadosModel
-  include AssignUuid
+  include HasUuid
   include KindAndEtag
   include CommonApiTemplate
   serialize :properties, Hash
@@ -8,19 +8,16 @@ class Link < ArvadosModel
   after_update :maybe_invalidate_permissions_cache
   after_create :maybe_invalidate_permissions_cache
   after_destroy :maybe_invalidate_permissions_cache
+  attr_accessor :head_kind, :tail_kind
+  validate :name_link_has_valid_name
 
-  attr_accessor :head
-  attr_accessor :tail
-
-  api_accessible :superuser, :extend => :common do |t|
-    t.add :tail_kind
+  api_accessible :user, extend: :common do |t|
     t.add :tail_uuid
     t.add :link_class
     t.add :name
-    t.add :head_kind
     t.add :head_uuid
-    t.add :head, :if => :head
-    t.add :tail, :if => :tail
+    t.add :head_kind
+    t.add :tail_kind
     t.add :properties
   end
 
@@ -29,6 +26,18 @@ class Link < ArvadosModel
     super
   end
 
+  def head_kind
+    if k = ArvadosModel::resource_class_for_uuid(head_uuid)
+      k.kind
+    end
+  end
+
+  def tail_kind
+    if k = ArvadosModel::resource_class_for_uuid(tail_uuid)
+      k.kind
+    end
+  end
+
   protected
 
   def permission_to_attach_to_objects
@@ -43,11 +52,11 @@ class Link < ArvadosModel
 
     # All users can grant permissions on objects they own
     head_obj = self.class.
-      kind_class(self.head_kind).
+      resource_class_for_uuid(self.head_uuid).
       where('uuid=?',head_uuid).
       first
     if head_obj
-      return true if head_obj.owner == current_user.uuid
+      return true if head_obj.owner_uuid == current_user.uuid
     end
 
     # Users with "can_grant" permission on an object can grant
@@ -73,4 +82,14 @@ class Link < ArvadosModel
       User.invalidate_permissions_cache
     end
   end
+
+  def name_link_has_valid_name
+    if link_class == 'name'
+      unless name.is_a? String and !name.empty?
+        errors.add('name', 'must be a non-empty string')
+      end
+    else
+      true
+    end
+  end
 end