]> source.dussan.org Git - nextcloud-server.git/commitdiff
Allow some apps to have root URLs in their own routing file
authorJoas Schilling <coding@schilljs.com>
Mon, 23 Mar 2020 15:33:11 +0000 (16:33 +0100)
committerRoeland Jago Douma <roeland@famdouma.nl>
Sat, 18 Apr 2020 09:21:28 +0000 (11:21 +0200)
Signed-off-by: Joas Schilling <coding@schilljs.com>
apps/cloud_federation_api/appinfo/routes.php [new file with mode: 0644]
apps/files/appinfo/routes.php
apps/files_sharing/appinfo/routes.php
core/routes.php
lib/private/AppFramework/Routing/RouteConfig.php
lib/private/Route/Router.php
lib/public/AppFramework/AuthPublicShareController.php

diff --git a/apps/cloud_federation_api/appinfo/routes.php b/apps/cloud_federation_api/appinfo/routes.php
new file mode 100644 (file)
index 0000000..49ea115
--- /dev/null
@@ -0,0 +1,36 @@
+<?php
+declare(strict_types=1);
+/**
+ * @copyright Copyright (c) 2020 Joas Schilling <coding@schilljs.com>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * 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
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+return [
+       [
+               'name' => 'RequestHandler#addShare',
+               'url' => '/ocm/shares',
+               'verb' => 'POST',
+               'root' => '',
+       ],
+       [
+               'name' => 'RequestHandler#receiveNotification',
+               'url' => '/ocm/notifications',
+               'verb' => 'POST',
+               'root' => '',
+       ],
+];
index 825249b08749d99b269582492f467f131184e89f..01d1e14054a6f08c73bebb0575f519a9e788dda9 100644 (file)
@@ -41,6 +41,13 @@ $application->registerRoutes(
        $this,
        [
                'routes' => [
+                       [
+                               'name' => 'View#showFile',
+                               'url' => '/f/{fileid}',
+                               'verb' => 'GET',
+                               'root' => '',
+                       ],
+
                        [
                                'name' => 'API#getThumbnail',
                                'url' => '/api/v1/thumbnail/{x}/{y}/{file}',
index 1346e0c68991cc48030f1aafac9e168583ffd2c6..a4edada738c7fb49ddc0780bcea9ff6996e03f3c 100644 (file)
@@ -30,6 +30,37 @@ return [
                'ExternalShares' => ['url' => '/api/externalShares'],
        ],
        'routes' => [
+               [
+                       'name' => 'Share#showShare',
+                       'url' => '/s/{token}',
+                       'verb' => 'GET',
+                       'root' => '',
+               ],
+               [
+                       'name' => 'Share#showAuthenticate',
+                       'url' => '/s/{token}/authenticate/{redirect}',
+                       'verb' => 'GET',
+                       'root' => '',
+               ],
+               [
+                       'name' => 'Share#authenticate',
+                       'url' => '/s/{token}/authenticate/{redirect}',
+                       'verb' => 'POST',
+                       'root' => '',
+               ],
+               [
+                       'name' => 'Share#downloadShare',
+                       'url' => '/s/{token}/download',
+                       'verb' => 'GET',
+                       'root' => '',
+               ],
+               [
+                       'name' => 'PublicPreview#directLink',
+                       'url' => '/s/{token}/preview',
+                       'verb' => 'GET',
+                       'root' => '',
+               ],
+
                [
                        'name' => 'externalShares#testRemote',
                        'url' => '/testremote',
index 8d03be05bb3a219757e3e6a542c2ba3fe3c53489..3d30b12e3928bf67e19c9e1ea1ee21a8ace68270 100644 (file)
@@ -89,18 +89,6 @@ $application->registerRoutes($this, [
                // Logins for passwordless auth
                ['name' => 'WebAuthn#startAuthentication', 'url' => 'login/webauthn/start', 'verb' => 'POST'],
                ['name' => 'WebAuthn#finishAuthentication', 'url' => 'login/webauthn/finish', 'verb' => 'POST'],
-
-               // Legacy routes that need to be globally available while they are handled by an app
-               ['name' => 'viewcontroller#showFile', 'url' => '/f/{fileid}', 'verb' => 'GET', 'app' => 'files'],
-               ['name' => 'sharecontroller#showShare', 'url' => '/s/{token}', 'verb' => 'GET', 'app' => 'files_sharing'],
-               ['name' => 'sharecontroller#showAuthenticate', 'url' => '/s/{token}/authenticate/{redirect}', 'verb' => 'GET', 'app' => 'files_sharing'],
-               ['name' => 'sharecontroller#authenticate', 'url' => '/s/{token}/authenticate/{redirect}', 'verb' => 'POST', 'app' => 'files_sharing'],
-               ['name' => 'sharecontroller#downloadShare', 'url' => '/s/{token}/download', 'verb' => 'GET', 'app' => 'files_sharing'],
-               ['name' => 'publicpreview#directLink', 'url' => '/s/{token}/preview', 'verb' => 'GET', 'app' => 'files_sharing'],
-               ['name' => 'requesthandlercontroller#addShare', 'url' => '/ocm/shares', 'verb' => 'POST', 'app' => 'cloud_federation_api'],
-               ['name' => 'requesthandlercontroller#receiveNotification', 'url' => '/ocm/notifications', 'verb' => 'POST', 'app' => 'cloud_federation_api'],
-               ['name' => 'pagecontroller#showCall', 'url' => '/call/{token}', 'verb' => 'GET', 'app' => 'spreed'],
-               ['name' => 'pagecontroller#authenticatePassword', 'url' => '/call/{token}', 'verb' => 'POST', 'app' => 'spreed'],
        ],
        'ocs' => [
                ['root' => '/cloud', 'name' => 'OCS#getCapabilities', 'url' => '/capabilities', 'verb' => 'GET'],
index eb9991fbe6958a981b890e804766c2e7f81047c6..58e677dd01ad9fb52eda97a96d919504fb08c0db 100644 (file)
@@ -56,6 +56,14 @@ class RouteConfig {
        /** @var string[] */
        private $controllerNameCache = [];
 
+       public const ROOT_URL_APPS = [
+               'cloud_federation_api',
+               'core',
+               'files_sharing',
+               'files',
+               'spreed',
+       ];
+
        /**
         * @param \OC\AppFramework\DependencyInjection\DIContainer $container
         * @param \OCP\Route\IRouter $router
@@ -98,42 +106,7 @@ class RouteConfig {
        private function processOCS(array $routes): void {
                $ocsRoutes = $routes['ocs'] ?? [];
                foreach ($ocsRoutes as $ocsRoute) {
-                       $name = $ocsRoute['name'];
-                       $postfix = $ocsRoute['postfix'] ?? '';
-                       $root = $ocsRoute['root'] ?? '/apps/' . $this->appName;
-
-                       $url = $root . $ocsRoute['url'];
-                       $verb = strtoupper($ocsRoute['verb'] ?? 'GET');
-
-                       $split = explode('#', $name, 2);
-                       if (count($split) !== 2) {
-                               throw new \UnexpectedValueException('Invalid route name');
-                       }
-                       list($controller, $action) = $split;
-
-                       $controllerName = $this->buildControllerName($controller);
-                       $actionName = $this->buildActionName($action);
-
-                       $routeName = 'ocs.' . $this->appName . '.' . $controller . '.' . $action . $postfix;
-
-                       // register the route
-                       $handler = new RouteActionHandler($this->container, $controllerName, $actionName);
-
-                       $router = $this->router->create($routeName, $url)
-                               ->method($verb)
-                               ->action($handler);
-
-                       // optionally register requirements for route. This is used to
-                       // tell the route parser how url parameters should be matched
-                       if (array_key_exists('requirements', $ocsRoute)) {
-                               $router->requirements($ocsRoute['requirements']);
-                       }
-
-                       // optionally register defaults for route. This is used to
-                       // tell the route parser how url parameters should be default valued
-                       if (array_key_exists('defaults', $ocsRoute)) {
-                               $router->defaults($ocsRoute['defaults']);
-                       }
+                       $this->processRoute($ocsRoute, 'ocs.');
                }
        }
 
@@ -145,53 +118,51 @@ class RouteConfig {
        private function processSimpleRoutes(array $routes): void {
                $simpleRoutes = $routes['routes'] ?? [];
                foreach ($simpleRoutes as $simpleRoute) {
-                       $name = $simpleRoute['name'];
-                       $postfix = $simpleRoute['postfix'] ?? '';
+                       $this->processRoute($simpleRoute);
+               }
+       }
 
-                       $url = $simpleRoute['url'];
-                       $verb = strtoupper($simpleRoute['verb'] ?? 'GET');
+       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, self::ROOT_URL_APPS, true)) {
+                       // Only allow root URLS for some apps
+                       $root = $defaultRoot;
+               }
 
-                       $split = explode('#', $name, 2);
-                       if (count($split) !== 2) {
-                               throw new \UnexpectedValueException('Invalid route name');
-                       }
-                       list($controller, $action) = $split;
-
-                       $controllerName = $this->buildControllerName($controller);
-                       $actionName = $this->buildActionName($action);
-                       $appName = $simpleRoute['app'] ?? $this->appName;
-
-                       if (isset($simpleRoute['app'])) {
-                               // Legacy routes that need to be globally available while they are handled by an app
-                               // E.g. '/f/{id}', '/s/{token}', '/call/{token}', …
-                               $controllerName = str_replace('controllerController', 'Controller', $controllerName);
-                               if ($controllerName === 'PublicpreviewController') {
-                                       $controllerName = 'PublicPreviewController';
-                               } elseif ($controllerName === 'RequesthandlerController') {
-                                       $controllerName = 'RequestHandlerController';
-                               }
-                               $controllerName = App::buildAppNamespace($appName) . '\\Controller\\' . $controllerName;
-                       }
+               $url = $root . $route['url'];
+               $verb = strtoupper($route['verb'] ?? 'GET');
+
+               $split = explode('#', $name, 2);
+               if (count($split) !== 2) {
+                       throw new \UnexpectedValueException('Invalid route name');
+               }
+               list($controller, $action) = $split;
 
-                       $routeName = $appName . '.' . $controller . '.' . $action . $postfix;
+               $controllerName = $this->buildControllerName($controller);
+               $actionName = $this->buildActionName($action);
 
-                       // register the route
-                       $handler = new RouteActionHandler($this->container, $controllerName, $actionName);
-                       $router = $this->router->create($routeName, $url)
-                                                       ->method($verb)
-                                                       ->action($handler);
+               $routeName = $routeNamePrefix . $this->appName . '.' . $controller . '.' . $action . $postfix;
 
-                       // optionally register requirements for route. This is used to
-                       // tell the route parser how url parameters should be matched
-                       if (array_key_exists('requirements', $simpleRoute)) {
-                               $router->requirements($simpleRoute['requirements']);
-                       }
+               // register the route
+               $handler = new RouteActionHandler($this->container, $controllerName, $actionName);
 
-                       // optionally register defaults for route. This is used to
-                       // tell the route parser how url parameters should be default valued
-                       if (array_key_exists('defaults', $simpleRoute)) {
-                               $router->defaults($simpleRoute['defaults']);
-                       }
+               $router = $this->router->create($routeName, $url)
+                       ->method($verb)
+                       ->action($handler);
+
+               // optionally register requirements for route. This is used to
+               // tell the route parser how url parameters should be matched
+               if(array_key_exists('requirements', $route)) {
+                       $router->requirements($route['requirements']);
+               }
+
+               // optionally register defaults for route. This is used to
+               // tell the route parser how url parameters should be default valued
+               if(array_key_exists('defaults', $route)) {
+                       $router->defaults($route['defaults']);
                }
        }
 
index ccb1578f8d356d533b843a788e301f172c50868d..9c6e69908e77afa1345934598b37049150b6aa3e 100644 (file)
@@ -151,7 +151,6 @@ class Router implements IRouter {
                                $this->useCollection($app);
                                $this->requireRouteFile($file, $app);
                                $collection = $this->getCollection($app);
-                               $collection->addPrefix('/apps/' . $app);
                                $this->root->addCollection($collection);
 
                                // Also add the OCS collection
index 1170819d5ecbc99aeb2ef31c769da6f1a96f9bda..989067bdf43ace1405f571326755b7d8efebce45 100644 (file)
@@ -165,9 +165,7 @@ abstract class AuthPublicShareController extends PublicShareController {
        private function getRoute(string $function): string {
                $app = strtolower($this->appName);
                $class = (new \ReflectionClass($this))->getShortName();
-               if ($this->appName === 'files_sharing') {
-                       $class = strtolower($class);
-               } elseif (substr($class, -10) === 'Controller') {
+               if (substr($class, -10) === 'Controller') {
                        $class = substr($class, 0, -10);
                }
                return $app .'.'. $class .'.'. $function;