2762: When deleting an object, delete permissions and fail if other
authorTom Clegg <tom@curoverse.com>
Sun, 11 May 2014 06:55:45 +0000 (02:55 -0400)
committerTom Clegg <tom@curoverse.com>
Sun, 11 May 2014 06:55:45 +0000 (02:55 -0400)
links would be left dangling.

services/api/app/models/arvados_model.rb
services/api/lib/assign_uuid.rb
services/api/test/unit/link_test.rb

index 69d329f02ad4c8d6d60356216e4d563de619ffcc..4faa0d041ffa28d3497a085d01285ab8ca6906df 100644 (file)
@@ -26,7 +26,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'"
+  has_many :permissions, :foreign_key => :head_uuid, :class_name => 'Link', :primary_key => :uuid, :conditions => "link_class = 'permission'", dependent: :destroy
 
   class PermissionDeniedError < StandardError
     def http_status
@@ -236,6 +236,11 @@ 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
index 50738aa65577fa1fa44bd16b258d9ecdf646ee9f..3e7c377d9b20943007cf3ace18189328a2173345 100644 (file)
@@ -3,6 +3,9 @@ module AssignUuid
   def self.included(base)
     base.extend(ClassMethods)
     base.before_create :assign_uuid
+    base.before_destroy :destroy_permission_links
+    base.has_many :links_via_head, class_name: 'Link', foreign_key: :head_uuid, primary_key: :uuid, conditions: "not (link_class = 'permission')", dependent: :restrict
+    base.has_many :links_via_tail, class_name: 'Link', foreign_key: :tail_uuid, primary_key: :uuid, conditions: "not (link_class = 'permission')", dependent: :restrict
   end
 
   module ClassMethods
index 72d6017ce7ac9c6beac74fe67579c4a35034290e..10f2b5eca54adffb9f6250198386c4292f79037d 100644 (file)
@@ -4,7 +4,7 @@ class LinkTest < ActiveSupport::TestCase
   fixtures :all
 
   setup do
-    Thread.current[:user] = users(:active)
+    set_user_from_auth :admin_trustedclient
   end
 
   test 'name links with the same tail_uuid must be unique' do
@@ -45,4 +45,16 @@ class LinkTest < ActiveSupport::TestCase
       assert a.invalid?, "invalid name was accepted as valid?"
     end
   end
+
+  test "cannot delete an object referenced by links" do
+    ob = Specimen.create
+    link = Link.create(tail_uuid: users(:active).uuid,
+                       head_uuid: ob.uuid,
+                       link_class: 'test',
+                       name: 'test')
+    assert_raises(ActiveRecord::DeleteRestrictionError,
+                  "should not delete #{ob.uuid} with link #{link.uuid}") do
+      ob.destroy
+    end
+  end
 end