From 184764828a02d8295fd4ca55f62dff79be72c5b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn=20Friedrich=20Dreyer?= Date: Mon, 4 Sep 2017 16:06:45 +0200 Subject: [PATCH] add list routes command --- core/Command/Security/ListRoutes.php | 95 ++++++++++++++++++++++++++++ core/register_command.php | 1 + lib/private/Route/Router.php | 8 +++ 3 files changed, 104 insertions(+) create mode 100644 core/Command/Security/ListRoutes.php diff --git a/core/Command/Security/ListRoutes.php b/core/Command/Security/ListRoutes.php new file mode 100644 index 000000000000..cab72d1f60f1 --- /dev/null +++ b/core/Command/Security/ListRoutes.php @@ -0,0 +1,95 @@ + + * + * @copyright Copyright (c) 2017, ownCloud GmbH + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see + * + */ + +namespace OC\Core\Command\Security; + +use OC\Core\Command\Base; +use OC\Route\Router; +use OCP\ICertificate; +use OCP\Route\IRouter; +use Symfony\Component\Console\Helper\Table; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; + +class ListRoutes extends Base { + + /** + * @var Router + */ + protected $router; + + public function __construct(IRouter $router) { + $this->router = $router; + parent::__construct(); + } + + protected function configure() { + $this + ->setName('security:routes') + ->setDescription('list used routes'); + parent::configure(); + } + + protected function execute(InputInterface $input, OutputInterface $output) { + $outputType = $input->getOption('output'); + + \OC_App::loadApps(); + $this->router->loadRoutes(); + + $rows = []; + + foreach ($this->router->getCollections() as $routeCollection) { + foreach ($routeCollection as $route) { + $path = $route->getPath(); + if (isset($rows[$path])) { + $rows[$path]['methods'] = array_unique(array_merge($rows[$path]['methods'], $route->getMethods())); + } else { + $rows[$path] = [ + 'path' => $path, + 'methods' => $route->getMethods() + ]; + } + sort ($rows[$path]['methods']); + } + } + + usort($rows, function ($a, $b) { + return strcmp($a['path'], $b['path']); + }); + + if ($outputType === self::OUTPUT_FORMAT_JSON ) { + $output->write(json_encode($rows)); + } else if ($outputType === self::OUTPUT_FORMAT_JSON_PRETTY) { + $output->writeln(json_encode($rows, JSON_PRETTY_PRINT)); + } else { + $table = new Table($output); + $table->setHeaders([ + 'Path', + 'Methods', + ]); + + foreach ($rows as $row) { + $table->addRow([$row['path'], join(',', $row['methods'])]); + } + $table->render(); + } + } +} diff --git a/core/register_command.php b/core/register_command.php index e19e160cc187..37fca029912c 100644 --- a/core/register_command.php +++ b/core/register_command.php @@ -158,6 +158,7 @@ $application->add(new OC\Core\Command\Security\ListCertificates(\OC::$server->getCertificateManager(null), \OC::$server->getL10N('core'))); $application->add(new OC\Core\Command\Security\ImportCertificate(\OC::$server->getCertificateManager(null))); $application->add(new OC\Core\Command\Security\RemoveCertificate(\OC::$server->getCertificateManager(null))); + $application->add(new OC\Core\Command\Security\ListRoutes(\OC::$server->getRouter())); } else { $application->add(new OC\Core\Command\Maintenance\Install(\OC::$server->getConfig())); } diff --git a/lib/private/Route/Router.php b/lib/private/Route/Router.php index 258bff72559f..fdaa63695f7e 100644 --- a/lib/private/Route/Router.php +++ b/lib/private/Route/Router.php @@ -219,6 +219,14 @@ public function getCurrentCollection() { return $this->collectionName; } + /** + * returns the current collections + * + * @return RouteCollection[] collections + */ + public function getCollections() { + return $this->collections; + } /** * Create a \OC\Route\Route.