Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New feature: UI to list and remove orphaned ACL resources in backend. #3647

Merged
merged 32 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
9b2dec7
New feature: UI to list and remove orphaned ACL resources in backend.
kiatng Nov 12, 2023
b30afd0
Fixed bug on add button removal.
kiatng Nov 12, 2023
f3b94ad
Fixed phpcs and phpstan complains.
kiatng Nov 12, 2023
b7a21ca
Avoid duplicates in notice messages.
kiatng Nov 12, 2023
fa65743
Fixed notice message.
kiatng Nov 12, 2023
2c2c562
Update app/code/core/Mage/Admin/Model/Resource/Rules.php
kiatng Nov 12, 2023
66baaf5
Update app/code/core/Mage/Admin/Model/Resource/Rules.php
kiatng Nov 12, 2023
533d261
Update app/code/core/Mage/Admin/Model/Resource/Rules.php
kiatng Nov 12, 2023
bf96a9f
Update app/code/core/Mage/Adminhtml/Block/Permissions/OrphanedResourc…
kiatng Nov 12, 2023
1b86eed
Update app/code/core/Mage/Adminhtml/Block/Permissions/OrphanedResourc…
kiatng Nov 12, 2023
7aea5c4
Update app/code/core/Mage/Adminhtml/Block/Permissions/OrphanedResourc…
kiatng Nov 13, 2023
e7c4ad2
Update app/code/core/Mage/Adminhtml/Block/Permissions/OrphanedResourc…
kiatng Nov 13, 2023
2b62e78
Fixed PSR2 CS.
kiatng Nov 13, 2023
cb2caa8
Fixed bug on multiple notices.
kiatng Nov 13, 2023
bf85ada
Merge branch 'main' into 3625_orphaned_resources
kiatng Nov 13, 2023
a9dfee4
Update app/code/core/Mage/Adminhtml/Block/Permissions/OrphanedResourc…
kiatng Nov 13, 2023
85a1404
Update app/code/core/Mage/Adminhtml/controllers/Permissions/OrphanedR…
kiatng Nov 13, 2023
feffec7
Added strict type for new class.
kiatng Nov 13, 2023
e77bbd2
CS fixer.
kiatng Nov 13, 2023
0a6db0b
Merge branch 'main' into 3625_orphaned_resources
kiatng Jan 31, 2024
73c01e2
Merge branch 'main' into 3625_orphaned_resources
fballiano Feb 6, 2024
d0aff0d
Merge branch 'main' into 3625_orphaned_resources
kiatng Feb 7, 2024
93e62ac
Merge branch 'main' into 3625_orphaned_resources
kiatng Feb 8, 2024
5517fd5
Change label Resources to Role Resources.
kiatng Feb 8, 2024
ab10363
Merge branch '3625_orphaned_resources' of https://github.com/kiatng/m…
kiatng Feb 8, 2024
5addc14
Merge branch 'main' into 3625_orphaned_resources
fballiano Feb 8, 2024
868676f
Update app/code/core/Mage/Adminhtml/controllers/Permissions/OrphanedR…
kiatng Feb 8, 2024
33b5d5e
Update app/code/core/Mage/Adminhtml/Block/Permissions/OrphanedResourc…
kiatng Feb 8, 2024
fc7f02b
Update app/code/core/Mage/Adminhtml/Block/Permissions/OrphanedResourc…
kiatng Feb 8, 2024
4f6d8e2
Update OrphanedResourceController.php
kiatng Feb 8, 2024
cdd5322
Merge branch 'main' into 3625_orphaned_resources
kiatng Feb 8, 2024
4aeb8e3
Merge branch 'main' into 3625_orphaned_resources
fballiano Feb 13, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .phpstorm.meta.php/magento_blocks.meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@
'adminhtml/permissions_role' => \Mage_Adminhtml_Block_Permissions_Role::class,
'adminhtml/permissions_role_grid_user' => \Mage_Adminhtml_Block_Permissions_Role_Grid_User::class,
'adminhtml/permissions_roles' => \Mage_Adminhtml_Block_Permissions_Roles::class,
'adminhtml/permissions_orphanedResource' => \Mage_Adminhtml_Block_Permissions_OrphanedResource::class,
'adminhtml/permissions_tab_roleinfo' => \Mage_Adminhtml_Block_Permissions_Tab_Roleinfo::class,
'adminhtml/permissions_tab_rolesedit' => \Mage_Adminhtml_Block_Permissions_Tab_Rolesedit::class,
'adminhtml/permissions_tab_rolesusers' => \Mage_Adminhtml_Block_Permissions_Tab_Rolesusers::class,
Expand Down
2 changes: 2 additions & 0 deletions .phpstorm.meta.php/magento_blocks_methods.meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@
'adminhtml/permissions_role' => \Mage_Adminhtml_Block_Permissions_Role::class,
'adminhtml/permissions_role_grid_user' => \Mage_Adminhtml_Block_Permissions_Role_Grid_User::class,
'adminhtml/permissions_roles' => \Mage_Adminhtml_Block_Permissions_Roles::class,
'adminhtml/permissions_orphanedResource' => \Mage_Adminhtml_Block_Permissions_OrphanedResource::class,
'adminhtml/permissions_tab_roleinfo' => \Mage_Adminhtml_Block_Permissions_Tab_Roleinfo::class,
'adminhtml/permissions_tab_rolesedit' => \Mage_Adminhtml_Block_Permissions_Tab_Rolesedit::class,
'adminhtml/permissions_tab_rolesusers' => \Mage_Adminhtml_Block_Permissions_Tab_Rolesusers::class,
Expand Down Expand Up @@ -554,6 +555,7 @@
'adminhtml/sales_order_view_tab_history' => \Mage_Adminhtml_Block_Sales_Order_View_Tab_History::class,
'adminhtml/sales_order_view_tab_info' => \Mage_Adminhtml_Block_Sales_Order_View_Tab_Info::class,
'adminhtml/sales_order_view_tab_invoices' => \Mage_Adminhtml_Block_Sales_Order_View_Tab_Invoices::class,
'adminhtml/sales_order_view_tab_prova' => \Mage_Adminhtml_Block_Sales_Order_View_Tab_Prova::class,
'adminhtml/sales_order_view_tab_shipments' => \Mage_Adminhtml_Block_Sales_Order_View_Tab_Shipments::class,
'adminhtml/sales_order_view_tab_transactions' => \Mage_Adminhtml_Block_Sales_Order_View_Tab_Transactions::class,
'adminhtml/sales_order_view_tabs' => \Mage_Adminhtml_Block_Sales_Order_View_Tabs::class,
Expand Down
19 changes: 19 additions & 0 deletions app/code/core/Mage/Admin/Model/Resource/Acl.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ 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 @@ -131,9 +133,26 @@ public function loadRules(Mage_Admin_Model_Acl $acl, array $rulesArr)
} elseif ($rule['permission'] == 'deny') {
$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
) {
kiatng marked this conversation as resolved.
Show resolved Hide resolved
$this->_orphanedResources[] = $resource;
}
} catch (Exception $e) {
Mage::logException($e);
}

if ($this->_orphanedResources) {
Mage::getSingleton('adminhtml/session')->addNotice(
Mage::helper('adminhtml')->__(
'The following resources are no longer available in the system: %s. You can delete them by <a href="%s">clicking here</a>.',
implode(', ', $this->_orphanedResources),
Mage::helper("adminhtml")->getUrl('adminhtml/permissions_orphanedResource')
)
);
}
}
return $this;
}
Expand Down
41 changes: 41 additions & 0 deletions app/code/core/Mage/Admin/Model/Resource/Rules.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,45 @@ public function saveRel(Mage_Admin_Model_Rules $rule)
Mage::logException($e);
}
}

/**
* Set resource ID as ID field name
* @see Mage_Adminhtml_Block_Permissions_OrphanedResource_Grid::_prepareCollection()
*
* @return $this
*/
public function setResourceIdAsIdFieldName()
{
$this->_idFieldName = 'resource_id';
return $this;
}

/**
* Delete orphaned resources
*
* @param array $orphanedIds
* @return int
* @throws Mage_Core_Exception
*/
public function deleteOrphanedResources(array $orphanedIds)
kiatng marked this conversation as resolved.
Show resolved Hide resolved
{
if (empty($orphanedIds)) {
kiatng marked this conversation as resolved.
Show resolved Hide resolved
return 0;
}

$resourceIds = Mage::getModel('admin/roles')->getResourcesList2D();
// Validate orphaned IDs are not in the list of valid resource IDs.
$validIds = array_intersect($orphanedIds, $resourceIds);
if (!empty($validIds)) {
kiatng marked this conversation as resolved.
Show resolved Hide resolved
throw new Mage_Core_Exception(
Mage::helper('adminhtml')->__(
'The following resource(s) are not orphaned: %s',
implode(', ', $validIds)
)
);
}

return $this->_getWriteAdapter()
->delete($this->getMainTable(), ['resource_id IN (?)' => $orphanedIds]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
kiatng marked this conversation as resolved.
Show resolved Hide resolved
/**
* OpenMage
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available at https://opensource.org/license/osl-3-0-php
*
* @category Mage
* @package Mage_Adminhtml
* @copyright Copyright (c) 2022-2023 The OpenMage Contributors (https://www.openmage.org)
kiatng marked this conversation as resolved.
Show resolved Hide resolved
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/

/**
* Adminhtml permissions orphaned resource block
*
* @category Mage
* @package Mage_Adminhtml
*/
class Mage_Adminhtml_Block_Permissions_OrphanedResource extends Mage_Adminhtml_Block_Widget_Grid_Container
{
public function __construct()
{
$this->_controller = 'permissions_orphanedResource';
$this->_headerText = Mage::helper('adminhtml')->__('Orphaned Resources');
parent::__construct();
$this->_removeButton('add');
}

/**
* @return string
*/
protected function _toHtml()
kiatng marked this conversation as resolved.
Show resolved Hide resolved
{
Mage::dispatchEvent('permissions_orphanedresource_html_before', ['block' => $this]);
return parent::_toHtml();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php
kiatng marked this conversation as resolved.
Show resolved Hide resolved
/**
* OpenMage
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available at https://opensource.org/license/osl-3-0-php
*
* @category Mage
* @package Mage_Adminhtml
* @copyright Copyright (c) 2022-2023 The OpenMage Contributors (https://www.openmage.org)
kiatng marked this conversation as resolved.
Show resolved Hide resolved
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/

/**
* Adminhtml permissions orphanedResource grid
*
* @category Mage
* @package Mage_Adminhtml
*/
class Mage_Adminhtml_Block_Permissions_OrphanedResource_Grid extends Mage_Adminhtml_Block_Widget_Grid
{
public function __construct()
{
parent::__construct();
$this->setId('permissionsOrphanedResourceGrid');
$this->setDefaultSort('resource_id');
$this->setDefaultDir('asc');
}

/**
* @inheritdoc
*/
protected function _prepareCollection()
fballiano marked this conversation as resolved.
Show resolved Hide resolved
{
/** @var Mage_Admin_Model_Resource_Rules_Collection */
$collection = Mage::getResourceModel('admin/rules_collection')
->addFieldToFilter('resource_id', ['nin' => Mage::getModel('admin/roles')->getResourcesList2D()])
->addFieldToSelect('resource_id');
$collection->getSelect()->group('resource_id');

/**
* In order for mass action selection to work properly, we need to overwrite
* the model resource $_idFieldName, from the default 'rule_id' to 'resource_id'.
* @see Mage_Adminhtml_Block_Widget_Grid_Massaction_Abstract::getGridIdsJson()
* @var Mage_Admin_Model_Resource_Rules $resource
*/
$resource = $collection->getResource();
$resource->setResourceIdAsIdFieldName();

$this->setCollection($collection);
return parent::_prepareCollection();
}

/**
* @inheritdoc
*/
protected function _prepareColumns()
fballiano marked this conversation as resolved.
Show resolved Hide resolved
{
$this->addColumn('resource_id', [
'header' => Mage::helper('adminhtml')->__('Orphaned Resource'),
'index' => 'resource_id'
]);

return parent::_prepareColumns();
}

/**
* @inheritdoc
*/
protected function _prepareMassaction()
fballiano marked this conversation as resolved.
Show resolved Hide resolved
{
$this->setMassactionIdField('resource_id');
$this->getMassactionBlock()->setFormFieldName('resource_id');

$this->getMassactionBlock()->addItem('delete', [
'label' => Mage::helper('adminhtml')->__('Delete'),
'url' => $this->getUrl('*/*/massDelete'),
'confirm' => Mage::helper('adminhtml')->__('Are you sure you want to do this?')
]);

return $this;
}

public function getRowUrl($row)
kiatng marked this conversation as resolved.
Show resolved Hide resolved
{
return '';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php
kiatng marked this conversation as resolved.
Show resolved Hide resolved
/**
* OpenMage
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available at https://opensource.org/license/osl-3-0-php
*
* @category Mage
* @package Mage_Adminhtml
* @copyright Copyright (c) 2023 The OpenMage Contributors (https://www.openmage.org)
kiatng marked this conversation as resolved.
Show resolved Hide resolved
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/

/**
* Class Mage_Adminhtml_Permissions_OrphanedResourceController
*
kiatng marked this conversation as resolved.
Show resolved Hide resolved
* @category Mage
* @package Mage_Adminhtml
*/
class Mage_Adminhtml_Permissions_OrphanedResourceController extends Mage_Adminhtml_Controller_Action
{
/**
* ACL resource
* @see Mage_Adminhtml_Controller_Action::_isAllowed()
*/
public const ADMIN_RESOURCE = 'system/acl/orphaned_resources';

/**
* @return $this
*/
protected function _initAction()
{
$this->loadLayout()
->_setActiveMenu('system/acl')
->_addBreadcrumb($this->__('System'), $this->__('System'))
->_addBreadcrumb($this->__('Permissions'), $this->__('Permissions'))
->_addBreadcrumb($this->__('Orphaned Resources'), $this->__('Orphaned Resources'));
return $this;
}

/**
* Index action
*/
public function indexAction()
{
$this->_title($this->__('System'))
->_title($this->__('Permissions'))
->_title($this->__('Orphaned Resources'));

/** @var Mage_Adminhtml_Block_Permissions_OrphanedResource $block */
$block = $this->getLayout()->createBlock('adminhtml/permissions_orphanedResource');
$this->_initAction()
->_addContent($block)
->renderLayout();
}

/**
* Mass delete action
*/
public function massDeleteAction()
{
$resourceIds = $this->getRequest()->getParam('resource_id');
try {
$deletedRows = Mage::getResourceSingleton('admin/rules')->deleteOrphanedResources($resourceIds);
$this->_getSession()->addSuccess($this->__('Total of %d record(s) have been deleted.', $deletedRows));
} catch (Mage_Core_Exception $e) {
$this->_getSession()->addError($e->getMessage());
} catch (Exception $e) {
$error = Mage::getIsDeveloperMode()
? $e->getMessage()
: $this->__('An error occurred while deleting record(s).');
$this->_getSession()->addError($error);
Mage::logException($e);
}

$this->_redirect('*/*/');
}

/**
* @inheritdoc
*/
public function preDispatch()
{
$this->_setForcedFormKeyActions('massDelete');
return parent::preDispatch();
}
}
7 changes: 7 additions & 0 deletions app/code/core/Mage/Adminhtml/etc/adminhtml.xml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@
<title>Blocks</title>
<action>adminhtml/permissions_block</action>
</blocks>
<orphaned_resources translate="title">
<title>Orphaned Resources</title>
<action>adminhtml/permissions_orphanedResource</action>
</orphaned_resources>
</children>
</acl>
<cache translate="title">
Expand Down Expand Up @@ -145,6 +149,9 @@
<blocks translate="title">
<title>Blocks</title>
</blocks>
<orphaned_resources translate="title">
<title>Orphaned Resources</title>
kiatng marked this conversation as resolved.
Show resolved Hide resolved
</orphaned_resources>
</children>
</acl>
<store translate="title">
Expand Down
4 changes: 4 additions & 0 deletions app/locale/en_US/Mage_Adminhtml.csv
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,8 @@
"Order Updated Date report is real-time, does not need statistics refreshing.","Order Updated Date report is real-time, does not need statistics refreshing."
"Orders","Orders"
"Original Magento attribute names in first row:","Original Magento attribute names in first row:"
"Orphaned Resource","Orphaned Resource"
"Orphaned Resources","Orphaned Resources"
"Out of stock","Out of stock"
"PDT (Payment Data Transfer) Only","PDT (Payment Data Transfer) Only"
"Pages","Pages"
Expand Down Expand Up @@ -1025,6 +1027,7 @@
"The CatalogInventory Stock Status has been rebuilt.","The CatalogInventory Stock Status has been rebuilt."
"The Comment Text field cannot be empty.","The Comment Text field cannot be empty."
"The Flat Catalog Product was rebuilt","The Flat Catalog Product was rebuilt"
"The following resource(s) are not orphaned: %s","The following resource(s) are not orphaned: %s"
"The JavaScript/CSS cache has been cleaned.","The JavaScript/CSS cache has been cleaned."
"The JavaScript/CSS cache has been cleared.","The JavaScript/CSS cache has been cleared."
"The Layered Navigation indexing has been queued.","The Layered Navigation indexing has been queued."
Expand Down Expand Up @@ -1061,6 +1064,7 @@
"The email template has been deleted.","The email template has been deleted."
"The email template has been saved.","The email template has been saved."
"The flat catalog category has been rebuilt.","The flat catalog category has been rebuilt."
"The following resources are no longer available in the system: %s. You can delete them by <a href="%s">clicking here</a>.","The following resources are no longer available in the system: %s. You can delete them by <a href="%s">clicking here</a>."
"The group node name must be specified with field node name.","The group node name must be specified with field node name."
"The image cache was cleaned.","The image cache was cleaned."
"The image cache was cleared.","The image cache was cleared."
Expand Down
1 change: 1 addition & 0 deletions docs/EVENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@
| paypal_prepare_line_items | 1.9.4.5 |
| pdf_item_draw_after | 1.9.4.5 |
| permissions_block_html_before | 1.9.4.5 |
| permissions_orphanedresource_html_before | 20.2.1 |
| permissions_user_html_before | 1.9.4.5 |
| permissions_variable_html_before | 1.9.4.5 |
| poll_vote_add | 1.9.4.5 |
Expand Down
Loading