Created
June 13, 2022 15:44
-
-
Save vbuck/9736f31a157737b8f9c349a6e376fbbb to your computer and use it in GitHub Desktop.
Magento 2 ACL Report Generator: Utility for creating a flat list of all ACL entries in a given installation. Useful for analyzing and comparing permissions structures to other platforms or use cases.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Utility for creating a flat list of all ACL entries in a given installation. Useful for analyzing and comparing permissions structures to other platforms or use cases. | |
* | |
* How to use: | |
* [SEPARATOR="{value}"] php build-acl.php | |
* | |
* Runtime or environment variables: | |
* SEPARATOR - Optional. Specifies the resource hierarchy separator. Default: "." (dot-notation) | |
*/ | |
$envPath = dirname(__DIR__) . '/webroot/app/etc/env.php'; | |
if (file_exists($envPath)) { | |
rename($envPath, $envPath . '.bak'); | |
register_shutdown_function( | |
function () use ($envPath) { | |
rename($envPath . '.bak', $envPath); | |
} | |
); | |
} | |
require dirname(__DIR__) . '/webroot/app/bootstrap.php'; | |
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, []); | |
$om = $bootstrap->getObjectManager(); | |
/** @var \Magento\Framework\Acl\AclResource\Config\Reader\Filesystem $processor */ | |
$processor = $om->get(\Magento\Framework\Acl\AclResource\Config\Reader\Filesystem::class); | |
$resources = $processor->read('global'); | |
$result = array_keys( | |
flatten_tree( | |
$resources['config']['acl']['resources'], | |
'', | |
getenv('SEPARATOR') ?: '.' | |
) | |
); | |
sort($result); | |
echo implode("\n", $result); | |
function flatten_tree(array $tree, string $path = '', string $separator = '.'): array { | |
$result = []; | |
foreach ($tree as $branch) { | |
$name = $branch['title']; | |
if (!empty($branch['children'])) { | |
$result = array_merge( | |
$result, | |
flatten_tree( | |
$branch['children'], | |
trim("{$path}{$separator}{$name}{$separator}", $separator), | |
$separator | |
) | |
); | |
} else { | |
$result[trim("{$path}{$separator}{$name}", $separator)] = true; | |
} | |
} | |
return $result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment