Skip to content

Commit

Permalink
Merge pull request #28907 from owncloud/stable10-add-list-routes-command
Browse files Browse the repository at this point in the history
[stable10] add list routes command
  • Loading branch information
Vincent Petry authored Sep 5, 2017
2 parents bdfd05b + 1847648 commit 78f33d5
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 0 deletions.
95 changes: 95 additions & 0 deletions core/Command/Security/ListRoutes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php
/**
* @author Jörn Friedrich Dreyer <[email protected]>
*
* @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 <http://www.gnu.org/licenses/>
*
*/

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();
}
}
}
1 change: 1 addition & 0 deletions core/register_command.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
8 changes: 8 additions & 0 deletions lib/private/Route/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 78f33d5

Please sign in to comment.