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

[stable10] add list routes command #28907

Merged
merged 1 commit into from
Sep 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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