Skip to content

Commit

Permalink
Improved orphaned resources detection in backend, fixed #4007 (#4022)
Browse files Browse the repository at this point in the history
  • Loading branch information
kiatng authored Jun 4, 2024
1 parent 6147a15 commit 71d9072
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 9 deletions.
11 changes: 5 additions & 6 deletions app/code/core/Mage/Admin/Model/Resource/Acl.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ class Mage_Admin_Model_Resource_Acl extends Mage_Core_Model_Resource_Db_Abstract
{
public const ACL_ALL_RULES = 'all';

protected $_orphanedResources = [];

/**
* Initialize resource
*
Expand Down Expand Up @@ -114,6 +112,7 @@ public function loadRoles(Mage_Admin_Model_Acl $acl, array $rolesArr)
*/
public function loadRules(Mage_Admin_Model_Acl $acl, array $rulesArr)
{
$orphanedResources = [];
foreach ($rulesArr as $rule) {
$role = $rule['role_type'] . $rule['role_id'];
$resource = $rule['resource_id'];
Expand All @@ -134,8 +133,8 @@ public function loadRules(Mage_Admin_Model_Acl $acl, array $rulesArr)
$acl->deny($role, $resource, $privileges, $assert);
}
} catch (Zend_Acl_Exception $e) {
if (!in_array($resource, $this->_orphanedResources) && strpos($e->getMessage(), "Resource '$resource' not found") !== false) {
$this->_orphanedResources[] = $resource;
if (!in_array($resource, $orphanedResources) && strpos($e->getMessage(), "Resource '$resource' not found") !== false) {
$orphanedResources[] = $resource;
}
} catch (Exception $e) {
if (Mage::getIsDeveloperMode()) {
Expand All @@ -144,11 +143,11 @@ public function loadRules(Mage_Admin_Model_Acl $acl, array $rulesArr)
}
}

if ($this->_orphanedResources !== []) {
if ($orphanedResources !== [] && $acl->isAllowed(Mage::getSingleton('admin/session')->getUser()->getAclRole(), 'admin/system/acl/orphaned_resources')) {
Mage::getSingleton('adminhtml/session')->addNotice(
Mage::helper('adminhtml')->__(
'The following role resources are no longer available in the system: %s. You can delete them by <a href="%s">clicking here</a>.',
implode(', ', $this->_orphanedResources),
implode(', ', $orphanedResources),
Mage::helper("adminhtml")->getUrl('adminhtml/permissions_orphanedResource')
)
);
Expand Down
2 changes: 1 addition & 1 deletion app/code/core/Mage/Admin/Model/Roles.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ protected function _buildResourcesArray(
$level = -1;
} else {
$resourceName = $parentName;
if (!in_array($resource->getName(), ['title', 'sort_order', 'children', 'disabled'])) {
if (!empty($resource->children()) && $resource->getName() !== 'children') {
$resourceName = (is_null($parentName) ? '' : $parentName . '/') . $resource->getName();

//assigning module for its' children nodes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@ public function __construct()
*/
protected function _prepareCollection()
{
/** @var Mage_Admin_Model_Resource_Rules_Collection */
$collection = Mage::getResourceModel('admin/rules_collection')
->addFieldToFilter('resource_id', ['nin' => Mage::getModel('admin/roles')->getResourcesList2D()])
->addFieldToFilter('resource_id', ['nin' => Mage::getSingleton('admin/session')->getAcl()->getResources()])
->addFieldToSelect('resource_id');
$collection->getSelect()->group('resource_id');

Expand Down

0 comments on commit 71d9072

Please sign in to comment.