Merge branch 'master' into 2755-python-sdk-permissions
[arvados.git] / services / api / app / models / arvados_model.rb
index 79237b80e39b481d5ff72eb797a67b6534814223..a6c9d31d416868446c9bfce54040482f6d1031f9 100644 (file)
@@ -27,7 +27,7 @@ class ArvadosModel < ActiveRecord::Base
   # Note: This only returns permission links. It does not account for
   # permissions obtained via user.is_admin or
   # user.uuid==object.owner_uuid.
-  has_many :permissions, :foreign_key => :head_uuid, :class_name => 'Link', :primary_key => :uuid, :conditions => "link_class = 'permission'", dependent: :destroy
+  has_many :permissions, :foreign_key => :head_uuid, :class_name => 'Link', :primary_key => :uuid, :conditions => "link_class = 'permission'"
 
   class PermissionDeniedError < StandardError
     def http_status
@@ -187,9 +187,13 @@ class ArvadosModel < ActiveRecord::Base
 
   def ensure_owner_uuid_is_permitted
     raise PermissionDeniedError if !current_user
-    self.owner_uuid ||= current_user.uuid
+    if respond_to? :owner_uuid=
+      self.owner_uuid ||= current_user.uuid
+    end
     if self.owner_uuid_changed?
-      if current_user.uuid == self.owner_uuid or
+      if new_record?
+        return true
+      elsif current_user.uuid == self.owner_uuid or
           current_user.can? write: self.owner_uuid
         # current_user is, or has :write permission on, the new owner
       else
@@ -237,11 +241,6 @@ class ArvadosModel < ActiveRecord::Base
     return true
   end
 
-  def destroy_permission_links
-    Link.destroy_all(['link_class=? and (head_uuid=? or tail_uuid=?)',
-                      'permission', uuid, uuid])
-  end
-
   def ensure_permission_to_destroy
     raise PermissionDeniedError unless permission_to_destroy
   end