From 8b4b8b3c31ee1aec336e0184f9ea68734c4dc49f Mon Sep 17 00:00:00 2001 From: Jim Graham Date: Thu, 26 Jan 2023 10:48:02 -0500 Subject: [PATCH] Prevent OnResourceDelete from firing on every Resource update (#16328) Fix logic that sets resourceDeleted --- .../Revolution/Processors/Resource/Update.php | 33 ++++++++++++------- 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/core/src/Revolution/Processors/Resource/Update.php b/core/src/Revolution/Processors/Resource/Update.php index 6f8a71a140c..7baeafda327 100644 --- a/core/src/Revolution/Processors/Resource/Update.php +++ b/core/src/Revolution/Processors/Resource/Update.php @@ -1,4 +1,5 @@ modx->lexicon('access_denied'); } // If changing the resource type, check if we have permission to **create the selected resource type** - if (($this->object->get('class_key') !== $properties['class_key']) + if ( + ($this->object->get('class_key') !== $properties['class_key']) && !$this->checkActionPermission($properties['class_key'], 'new') ) { return $this->modx->lexicon('access_denied'); @@ -382,8 +384,8 @@ public function checkFriendlyAlias() if ($this->getProperty('uri_override', 0) !== 1) { $this->addFieldError('alias', $err); } - } // If friendly urls is not enabled, and we automatically generated the alias, then we just unset it - elseif ($autoGenerated) { + } elseif ($autoGenerated) { + // If friendly urls is not enabled, and we automatically generated the alias, then we just unset it $alias = ''; } } @@ -551,17 +553,20 @@ public function checkForUnPublishOnSitePages() */ public function checkDeletedStatus(): bool { - $deleted = $this->getProperty('deleted'); + $proposedDeleted = (bool)$this->getProperty('deleted'); + $currentDeleted = (bool)$this->object->get('deleted'); - if ($deleted !== null && $deleted !== $this->object->get('deleted')) { - if ($this->object->get('deleted')) { /* undelete */ + if ($proposedDeleted !== $currentDeleted) { + if ($currentDeleted) { + // The previously-saved value was 1 (resource was deleted), so attempt to undelete if (!$this->modx->hasPermission('undelete_document')) { - $this->setProperty('deleted', $this->object->get('deleted')); + $this->setProperty('deleted', $currentDeleted); } else { $this->object->set('deleted', false); $this->resourceUnDeleted = true; } } else { + // The previously-saved value was 0 or null, so attempt to delete $hasPermission = $this->modx->hasPermission('delete_document'); $map = [ @@ -576,15 +581,15 @@ public function checkDeletedStatus(): bool } if (!$hasPermission) { - $this->setProperty('deleted', $this->object->get('deleted')); + $this->setProperty('deleted', $currentDeleted); } else { - $this->object->set('deleted',true); + $this->object->set('deleted', true); $this->resourceDeleted = true; } } } - return (bool)$deleted; + return $proposedDeleted; } /** @@ -686,7 +691,9 @@ public function afterSave() public function fixParents() { $autoIsFolder = $this->modx->getOption('auto_isfolder', null, true); - if (!$autoIsFolder) return; + if (!$autoIsFolder) { + return; + } if (!empty($this->oldParent)) { $oldParentChildrenCount = $this->modx->getCount(modResource::class, ['parent' => $this->oldParent->get('id')]); @@ -810,7 +817,9 @@ public function setResourceGroups() foreach ($resourceGroups as $id => $resourceGroupAccess) { /* prevent adding records for non-existing groups */ $resourceGroup = $this->modx->getObject(modResourceGroup::class, $resourceGroupAccess['id']); - if (empty($resourceGroup)) continue; + if (empty($resourceGroup)) { + continue; + } /* if assigning to group */ if ($resourceGroupAccess['access']) {