From 68aa07e5c152fd573e254614b34b71957b50ad8c Mon Sep 17 00:00:00 2001 From: Tom Clegg Date: Fri, 18 Jul 2014 17:37:05 -0400 Subject: [PATCH] 3214: Readability: rearrange conditionals, and say "old" instead of "existing" --- services/api/app/models/arvados_model.rb | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/services/api/app/models/arvados_model.rb b/services/api/app/models/arvados_model.rb index 469b0a3f78..5cd0c77c49 100644 --- a/services/api/app/models/arvados_model.rb +++ b/services/api/app/models/arvados_model.rb @@ -209,15 +209,17 @@ class ArvadosModel < ActiveRecord::Base if new_record? and respond_to? :owner_uuid= self.owner_uuid ||= current_user.uuid end - if owner_uuid_changed? and owner_uuid_was - # Verify permission to write to existing owner - unless current_user.uuid == self.owner_uuid_was or - current_user.uuid == self.uuid or - current_user.can? write: self.owner_uuid_was - logger.warn "User #{current_user.uuid} tried to modify #{self.class.to_s} #{uuid} but does not have permission to write existing owner_uuid #{owner_uuid_was}" - errors.add :owner_uuid, "cannot be changed without write permission on existing owner" - raise PermissionDeniedError - end + # Verify permission to write to old owner (unless owner_uuid was + # nil -- or hasn't changed, in which case the following + # "permission to write to new owner" block will take care of us) + unless !owner_uuid_changed? or + owner_uuid_was.nil? or + current_user.uuid == self.owner_uuid_was or + current_user.uuid == self.uuid or + current_user.can? write: self.owner_uuid_was + logger.warn "User #{current_user.uuid} tried to modify #{self.class.to_s} #{uuid} but does not have permission to write old owner_uuid #{owner_uuid_was}" + errors.add :owner_uuid, "cannot be changed without write permission on old owner" + raise PermissionDeniedError end # Verify permission to write to new owner unless current_user == self or current_user.can? write: owner_uuid -- 2.30.2