Skip to content

Commit

Permalink
Prevent OnResourceDelete from firing on every Resource update (#16328)
Browse files Browse the repository at this point in the history
Fix logic that sets resourceDeleted
  • Loading branch information
Jim Graham authored Jan 26, 2023
1 parent de50915 commit 8b4b8b3
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions core/src/Revolution/Processors/Resource/Update.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/*
* This file is part of MODX Revolution.
*
Expand Down Expand Up @@ -159,7 +160,8 @@ public function beforeSet()
return $this->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');
Expand Down Expand Up @@ -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 = '';
}
}
Expand Down Expand Up @@ -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 = [
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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')]);
Expand Down Expand Up @@ -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']) {
Expand Down

0 comments on commit 8b4b8b3

Please sign in to comment.