diff options
author | Roeland Jago Douma <roeland@famdouma.nl> | 2020-04-21 22:45:35 +0200 |
---|---|---|
committer | Roeland Jago Douma <roeland@famdouma.nl> | 2020-04-22 13:09:25 +0200 |
commit | c870b6ab2eec668813eb5127ffcf19e9b91e9011 (patch) | |
tree | d97b99499fe9606af9f563ba884496586417fbb2 /lib | |
parent | ac57bbcf999b5b8831722d04c2a4e54133eb2498 (diff) | |
download | nextcloud-server-c870b6ab2eec668813eb5127ffcf19e9b91e9011.tar.gz nextcloud-server-c870b6ab2eec668813eb5127ffcf19e9b91e9011.zip |
Fix new routing in settings etc
Also prefix resources
Unify the prefix handling
Handle urls with and without slash
Signed-off-by: Joas Schilling <coding@schilljs.com>
Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/private/AppFramework/Routing/RouteConfig.php | 96 |
1 files changed, 44 insertions, 52 deletions
diff --git a/lib/private/AppFramework/Routing/RouteConfig.php b/lib/private/AppFramework/Routing/RouteConfig.php index 2f2d51f6e1a..957dcba453d 100644 --- a/lib/private/AppFramework/Routing/RouteConfig.php +++ b/lib/private/AppFramework/Routing/RouteConfig.php @@ -60,6 +60,7 @@ class RouteConfig { 'core', 'files_sharing', 'files', + 'settings', 'spreed', ]; @@ -82,10 +83,10 @@ class RouteConfig { public function register() { // parse simple - $this->processSimpleRoutes($this->routes); + $this->processIndexRoutes($this->routes); // parse resources - $this->processResources($this->routes); + $this->processIndexResources($this->routes); /* * OCS routes go into a different collection @@ -114,7 +115,7 @@ class RouteConfig { * @param array $routes * @throws \UnexpectedValueException */ - private function processSimpleRoutes(array $routes): void { + private function processIndexRoutes(array $routes): void { $simpleRoutes = $routes['routes'] ?? []; foreach ($simpleRoutes as $simpleRoute) { $this->processRoute($simpleRoute); @@ -124,14 +125,9 @@ class RouteConfig { protected function processRoute(array $route, string $routeNamePrefix = ''): void { $name = $route['name']; $postfix = $route['postfix'] ?? ''; - $defaultRoot = $this->appName === 'core' ? '' : '/apps/' . $this->appName; - $root = $route['root'] ?? $defaultRoot; - if ($routeNamePrefix === '' && !\in_array($this->appName, $this->rootUrlApps, true)) { - // Only allow root URLS for some apps - $root = $defaultRoot; - } + $root = $this->buildRootPrefix($route, $routeNamePrefix); - $url = $root . $route['url']; + $url = $root . '/' . ltrim($route['url'], '/'); $verb = strtoupper($route['verb'] ?? 'GET'); $split = explode('#', $name, 2); @@ -176,44 +172,7 @@ class RouteConfig { * @param array $routes */ private function processOCSResources(array $routes): void { - // declaration of all restful actions - $actions = [ - ['name' => 'index', 'verb' => 'GET', 'on-collection' => true], - ['name' => 'show', 'verb' => 'GET'], - ['name' => 'create', 'verb' => 'POST', 'on-collection' => true], - ['name' => 'update', 'verb' => 'PUT'], - ['name' => 'destroy', 'verb' => 'DELETE'], - ]; - - $resources = $routes['ocs-resources'] ?? []; - foreach ($resources as $resource => $config) { - $root = $config['root'] ?? '/apps/' . $this->appName; - - // the url parameter used as id to the resource - foreach ($actions as $action) { - $url = $root . $config['url']; - $method = $action['name']; - $verb = strtoupper($action['verb'] ?? 'GET'); - $collectionAction = $action['on-collection'] ?? false; - if (!$collectionAction) { - $url .= '/{id}'; - } - if (isset($action['url-postfix'])) { - $url .= '/' . $action['url-postfix']; - } - - $controller = $resource; - - $controllerName = $this->buildControllerName($controller); - $actionName = $this->buildActionName($method); - - $routeName = 'ocs.' . $this->appName . '.' . strtolower($resource) . '.' . strtolower($method); - - $this->router->create($routeName, $url)->method($verb)->action( - new RouteActionHandler($this->container, $controllerName, $actionName) - ); - } - } + $this->processResources($routes['ocs-resources'] ?? [], 'ocs.'); } /** @@ -226,7 +185,22 @@ class RouteConfig { * * @param array $routes */ - private function processResources(array $routes): void { + private function processIndexResources(array $routes): void { + $this->processResources($routes['resources'] ?? []); + } + + /** + * For a given name and url restful routes are created: + * - index + * - show + * - create + * - update + * - destroy + * + * @param array $resources + * @param string $routeNamePrefix + */ + protected function processResources(array $resources, string $routeNamePrefix = ''): void { // declaration of all restful actions $actions = [ ['name' => 'index', 'verb' => 'GET', 'on-collection' => true], @@ -236,13 +210,14 @@ class RouteConfig { ['name' => 'destroy', 'verb' => 'DELETE'], ]; - $resources = $routes['resources'] ?? []; foreach ($resources as $resource => $config) { + $root = $this->buildRootPrefix($config, $routeNamePrefix); // the url parameter used as id to the resource foreach ($actions as $action) { - $url = $config['url']; + $url = $root . '/' . ltrim($config['url'], '/'); $method = $action['name']; + $verb = strtoupper($action['verb'] ?? 'GET'); $collectionAction = $action['on-collection'] ?? false; if (!$collectionAction) { @@ -257,7 +232,7 @@ class RouteConfig { $controllerName = $this->buildControllerName($controller); $actionName = $this->buildActionName($method); - $routeName = $this->appName . '.' . strtolower($resource) . '.' . strtolower($method); + $routeName = $routeNamePrefix . $this->appName . '.' . strtolower($resource) . '.' . strtolower($method); $this->router->create($routeName, $url)->method($verb)->action( new RouteActionHandler($this->container, $controllerName, $actionName) @@ -266,6 +241,23 @@ class RouteConfig { } } + private function buildRootPrefix(array $route, string $routeNamePrefix): string { + $defaultRoot = $this->appName === 'core' ? '' : '/apps/' . $this->appName; + $root = $route['root'] ?? $defaultRoot; + + if ($routeNamePrefix !== '') { + // In OCS all apps are whitelisted + return $root; + } + + if (!\in_array($this->appName, $this->rootUrlApps, true)) { + // Only allow root URLS for some apps + return $defaultRoot; + } + + return $root; + } + /** * Based on a given route name the controller name is generated * @param string $controller |