@@ -40,265 +40,265 @@ |
||
| 40 | 40 | * @package OC\AppFramework\routing |
| 41 | 41 | */ |
| 42 | 42 | class RouteConfig { |
| 43 | - /** @var DIContainer */ |
|
| 44 | - private $container; |
|
| 45 | - |
|
| 46 | - /** @var IRouter */ |
|
| 47 | - private $router; |
|
| 48 | - |
|
| 49 | - /** @var array */ |
|
| 50 | - private $routes; |
|
| 51 | - |
|
| 52 | - /** @var string */ |
|
| 53 | - private $appName; |
|
| 54 | - |
|
| 55 | - /** @var string[] */ |
|
| 56 | - private $controllerNameCache = []; |
|
| 57 | - |
|
| 58 | - protected $rootUrlApps = [ |
|
| 59 | - 'cloud_federation_api', |
|
| 60 | - 'core', |
|
| 61 | - 'files_sharing', |
|
| 62 | - 'files', |
|
| 63 | - 'spreed', |
|
| 64 | - ]; |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * @param \OC\AppFramework\DependencyInjection\DIContainer $container |
|
| 68 | - * @param \OCP\Route\IRouter $router |
|
| 69 | - * @param array $routes |
|
| 70 | - * @internal param $appName |
|
| 71 | - */ |
|
| 72 | - public function __construct(DIContainer $container, IRouter $router, $routes) { |
|
| 73 | - $this->routes = $routes; |
|
| 74 | - $this->container = $container; |
|
| 75 | - $this->router = $router; |
|
| 76 | - $this->appName = $container['AppName']; |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * The routes and resource will be registered to the \OCP\Route\IRouter |
|
| 81 | - */ |
|
| 82 | - public function register() { |
|
| 83 | - |
|
| 84 | - // parse simple |
|
| 85 | - $this->processSimpleRoutes($this->routes); |
|
| 86 | - |
|
| 87 | - // parse resources |
|
| 88 | - $this->processResources($this->routes); |
|
| 89 | - |
|
| 90 | - /* |
|
| 43 | + /** @var DIContainer */ |
|
| 44 | + private $container; |
|
| 45 | + |
|
| 46 | + /** @var IRouter */ |
|
| 47 | + private $router; |
|
| 48 | + |
|
| 49 | + /** @var array */ |
|
| 50 | + private $routes; |
|
| 51 | + |
|
| 52 | + /** @var string */ |
|
| 53 | + private $appName; |
|
| 54 | + |
|
| 55 | + /** @var string[] */ |
|
| 56 | + private $controllerNameCache = []; |
|
| 57 | + |
|
| 58 | + protected $rootUrlApps = [ |
|
| 59 | + 'cloud_federation_api', |
|
| 60 | + 'core', |
|
| 61 | + 'files_sharing', |
|
| 62 | + 'files', |
|
| 63 | + 'spreed', |
|
| 64 | + ]; |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * @param \OC\AppFramework\DependencyInjection\DIContainer $container |
|
| 68 | + * @param \OCP\Route\IRouter $router |
|
| 69 | + * @param array $routes |
|
| 70 | + * @internal param $appName |
|
| 71 | + */ |
|
| 72 | + public function __construct(DIContainer $container, IRouter $router, $routes) { |
|
| 73 | + $this->routes = $routes; |
|
| 74 | + $this->container = $container; |
|
| 75 | + $this->router = $router; |
|
| 76 | + $this->appName = $container['AppName']; |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * The routes and resource will be registered to the \OCP\Route\IRouter |
|
| 81 | + */ |
|
| 82 | + public function register() { |
|
| 83 | + |
|
| 84 | + // parse simple |
|
| 85 | + $this->processSimpleRoutes($this->routes); |
|
| 86 | + |
|
| 87 | + // parse resources |
|
| 88 | + $this->processResources($this->routes); |
|
| 89 | + |
|
| 90 | + /* |
|
| 91 | 91 | * OCS routes go into a different collection |
| 92 | 92 | */ |
| 93 | - $oldCollection = $this->router->getCurrentCollection(); |
|
| 94 | - $this->router->useCollection($oldCollection . '.ocs'); |
|
| 95 | - |
|
| 96 | - // parse ocs simple routes |
|
| 97 | - $this->processOCS($this->routes); |
|
| 98 | - |
|
| 99 | - // parse ocs simple routes |
|
| 100 | - $this->processOCSResources($this->routes); |
|
| 101 | - |
|
| 102 | - $this->router->useCollection($oldCollection); |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - private function processOCS(array $routes): void { |
|
| 106 | - $ocsRoutes = $routes['ocs'] ?? []; |
|
| 107 | - foreach ($ocsRoutes as $ocsRoute) { |
|
| 108 | - $this->processRoute($ocsRoute, 'ocs.'); |
|
| 109 | - } |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * Creates one route base on the give configuration |
|
| 114 | - * @param array $routes |
|
| 115 | - * @throws \UnexpectedValueException |
|
| 116 | - */ |
|
| 117 | - private function processSimpleRoutes(array $routes): void { |
|
| 118 | - $simpleRoutes = $routes['routes'] ?? []; |
|
| 119 | - foreach ($simpleRoutes as $simpleRoute) { |
|
| 120 | - $this->processRoute($simpleRoute); |
|
| 121 | - } |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - protected function processRoute(array $route, string $routeNamePrefix = ''): void { |
|
| 125 | - $name = $route['name']; |
|
| 126 | - $postfix = $route['postfix'] ?? ''; |
|
| 127 | - $defaultRoot = $this->appName === 'core' ? '' : '/apps/' . $this->appName; |
|
| 128 | - $root = $route['root'] ?? $defaultRoot; |
|
| 129 | - if ($routeNamePrefix === '' && !\in_array($this->appName, $this->rootUrlApps, true)) { |
|
| 130 | - // Only allow root URLS for some apps |
|
| 131 | - $root = $defaultRoot; |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - $url = $root . $route['url']; |
|
| 135 | - $verb = strtoupper($route['verb'] ?? 'GET'); |
|
| 136 | - |
|
| 137 | - $split = explode('#', $name, 2); |
|
| 138 | - if (count($split) !== 2) { |
|
| 139 | - throw new \UnexpectedValueException('Invalid route name'); |
|
| 140 | - } |
|
| 141 | - list($controller, $action) = $split; |
|
| 142 | - |
|
| 143 | - $controllerName = $this->buildControllerName($controller); |
|
| 144 | - $actionName = $this->buildActionName($action); |
|
| 145 | - |
|
| 146 | - $routeName = $routeNamePrefix . $this->appName . '.' . $controller . '.' . $action . $postfix; |
|
| 147 | - |
|
| 148 | - // register the route |
|
| 149 | - $handler = new RouteActionHandler($this->container, $controllerName, $actionName); |
|
| 150 | - |
|
| 151 | - $router = $this->router->create($routeName, $url) |
|
| 152 | - ->method($verb) |
|
| 153 | - ->action($handler); |
|
| 154 | - |
|
| 155 | - // optionally register requirements for route. This is used to |
|
| 156 | - // tell the route parser how url parameters should be matched |
|
| 157 | - if (array_key_exists('requirements', $route)) { |
|
| 158 | - $router->requirements($route['requirements']); |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - // optionally register defaults for route. This is used to |
|
| 162 | - // tell the route parser how url parameters should be default valued |
|
| 163 | - if (array_key_exists('defaults', $route)) { |
|
| 164 | - $router->defaults($route['defaults']); |
|
| 165 | - } |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - /** |
|
| 169 | - * For a given name and url restful OCS routes are created: |
|
| 170 | - * - index |
|
| 171 | - * - show |
|
| 172 | - * - create |
|
| 173 | - * - update |
|
| 174 | - * - destroy |
|
| 175 | - * |
|
| 176 | - * @param array $routes |
|
| 177 | - */ |
|
| 178 | - private function processOCSResources(array $routes): void { |
|
| 179 | - // declaration of all restful actions |
|
| 180 | - $actions = [ |
|
| 181 | - ['name' => 'index', 'verb' => 'GET', 'on-collection' => true], |
|
| 182 | - ['name' => 'show', 'verb' => 'GET'], |
|
| 183 | - ['name' => 'create', 'verb' => 'POST', 'on-collection' => true], |
|
| 184 | - ['name' => 'update', 'verb' => 'PUT'], |
|
| 185 | - ['name' => 'destroy', 'verb' => 'DELETE'], |
|
| 186 | - ]; |
|
| 187 | - |
|
| 188 | - $resources = $routes['ocs-resources'] ?? []; |
|
| 189 | - foreach ($resources as $resource => $config) { |
|
| 190 | - $root = $config['root'] ?? '/apps/' . $this->appName; |
|
| 191 | - |
|
| 192 | - // the url parameter used as id to the resource |
|
| 193 | - foreach ($actions as $action) { |
|
| 194 | - $url = $root . $config['url']; |
|
| 195 | - $method = $action['name']; |
|
| 196 | - $verb = strtoupper($action['verb'] ?? 'GET'); |
|
| 197 | - $collectionAction = $action['on-collection'] ?? false; |
|
| 198 | - if (!$collectionAction) { |
|
| 199 | - $url .= '/{id}'; |
|
| 200 | - } |
|
| 201 | - if (isset($action['url-postfix'])) { |
|
| 202 | - $url .= '/' . $action['url-postfix']; |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - $controller = $resource; |
|
| 206 | - |
|
| 207 | - $controllerName = $this->buildControllerName($controller); |
|
| 208 | - $actionName = $this->buildActionName($method); |
|
| 209 | - |
|
| 210 | - $routeName = 'ocs.' . $this->appName . '.' . strtolower($resource) . '.' . strtolower($method); |
|
| 211 | - |
|
| 212 | - $this->router->create($routeName, $url)->method($verb)->action( |
|
| 213 | - new RouteActionHandler($this->container, $controllerName, $actionName) |
|
| 214 | - ); |
|
| 215 | - } |
|
| 216 | - } |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - /** |
|
| 220 | - * For a given name and url restful routes are created: |
|
| 221 | - * - index |
|
| 222 | - * - show |
|
| 223 | - * - create |
|
| 224 | - * - update |
|
| 225 | - * - destroy |
|
| 226 | - * |
|
| 227 | - * @param array $routes |
|
| 228 | - */ |
|
| 229 | - private function processResources(array $routes): void { |
|
| 230 | - // declaration of all restful actions |
|
| 231 | - $actions = [ |
|
| 232 | - ['name' => 'index', 'verb' => 'GET', 'on-collection' => true], |
|
| 233 | - ['name' => 'show', 'verb' => 'GET'], |
|
| 234 | - ['name' => 'create', 'verb' => 'POST', 'on-collection' => true], |
|
| 235 | - ['name' => 'update', 'verb' => 'PUT'], |
|
| 236 | - ['name' => 'destroy', 'verb' => 'DELETE'], |
|
| 237 | - ]; |
|
| 238 | - |
|
| 239 | - $resources = $routes['resources'] ?? []; |
|
| 240 | - foreach ($resources as $resource => $config) { |
|
| 241 | - |
|
| 242 | - // the url parameter used as id to the resource |
|
| 243 | - foreach ($actions as $action) { |
|
| 244 | - $url = $config['url']; |
|
| 245 | - $method = $action['name']; |
|
| 246 | - $verb = strtoupper($action['verb'] ?? 'GET'); |
|
| 247 | - $collectionAction = $action['on-collection'] ?? false; |
|
| 248 | - if (!$collectionAction) { |
|
| 249 | - $url .= '/{id}'; |
|
| 250 | - } |
|
| 251 | - if (isset($action['url-postfix'])) { |
|
| 252 | - $url .= '/' . $action['url-postfix']; |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - $controller = $resource; |
|
| 256 | - |
|
| 257 | - $controllerName = $this->buildControllerName($controller); |
|
| 258 | - $actionName = $this->buildActionName($method); |
|
| 259 | - |
|
| 260 | - $routeName = $this->appName . '.' . strtolower($resource) . '.' . strtolower($method); |
|
| 261 | - |
|
| 262 | - $this->router->create($routeName, $url)->method($verb)->action( |
|
| 263 | - new RouteActionHandler($this->container, $controllerName, $actionName) |
|
| 264 | - ); |
|
| 265 | - } |
|
| 266 | - } |
|
| 267 | - } |
|
| 268 | - |
|
| 269 | - /** |
|
| 270 | - * Based on a given route name the controller name is generated |
|
| 271 | - * @param string $controller |
|
| 272 | - * @return string |
|
| 273 | - */ |
|
| 274 | - private function buildControllerName(string $controller): string { |
|
| 275 | - if (!isset($this->controllerNameCache[$controller])) { |
|
| 276 | - $this->controllerNameCache[$controller] = $this->underScoreToCamelCase(ucfirst($controller)) . 'Controller'; |
|
| 277 | - } |
|
| 278 | - return $this->controllerNameCache[$controller]; |
|
| 279 | - } |
|
| 280 | - |
|
| 281 | - /** |
|
| 282 | - * Based on the action part of the route name the controller method name is generated |
|
| 283 | - * @param string $action |
|
| 284 | - * @return string |
|
| 285 | - */ |
|
| 286 | - private function buildActionName(string $action): string { |
|
| 287 | - return $this->underScoreToCamelCase($action); |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - /** |
|
| 291 | - * Underscored strings are converted to camel case strings |
|
| 292 | - * @param string $str |
|
| 293 | - * @return string |
|
| 294 | - */ |
|
| 295 | - private function underScoreToCamelCase(string $str): string { |
|
| 296 | - $pattern = '/_[a-z]?/'; |
|
| 297 | - return preg_replace_callback( |
|
| 298 | - $pattern, |
|
| 299 | - function ($matches) { |
|
| 300 | - return strtoupper(ltrim($matches[0], '_')); |
|
| 301 | - }, |
|
| 302 | - $str); |
|
| 303 | - } |
|
| 93 | + $oldCollection = $this->router->getCurrentCollection(); |
|
| 94 | + $this->router->useCollection($oldCollection . '.ocs'); |
|
| 95 | + |
|
| 96 | + // parse ocs simple routes |
|
| 97 | + $this->processOCS($this->routes); |
|
| 98 | + |
|
| 99 | + // parse ocs simple routes |
|
| 100 | + $this->processOCSResources($this->routes); |
|
| 101 | + |
|
| 102 | + $this->router->useCollection($oldCollection); |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + private function processOCS(array $routes): void { |
|
| 106 | + $ocsRoutes = $routes['ocs'] ?? []; |
|
| 107 | + foreach ($ocsRoutes as $ocsRoute) { |
|
| 108 | + $this->processRoute($ocsRoute, 'ocs.'); |
|
| 109 | + } |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * Creates one route base on the give configuration |
|
| 114 | + * @param array $routes |
|
| 115 | + * @throws \UnexpectedValueException |
|
| 116 | + */ |
|
| 117 | + private function processSimpleRoutes(array $routes): void { |
|
| 118 | + $simpleRoutes = $routes['routes'] ?? []; |
|
| 119 | + foreach ($simpleRoutes as $simpleRoute) { |
|
| 120 | + $this->processRoute($simpleRoute); |
|
| 121 | + } |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + protected function processRoute(array $route, string $routeNamePrefix = ''): void { |
|
| 125 | + $name = $route['name']; |
|
| 126 | + $postfix = $route['postfix'] ?? ''; |
|
| 127 | + $defaultRoot = $this->appName === 'core' ? '' : '/apps/' . $this->appName; |
|
| 128 | + $root = $route['root'] ?? $defaultRoot; |
|
| 129 | + if ($routeNamePrefix === '' && !\in_array($this->appName, $this->rootUrlApps, true)) { |
|
| 130 | + // Only allow root URLS for some apps |
|
| 131 | + $root = $defaultRoot; |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + $url = $root . $route['url']; |
|
| 135 | + $verb = strtoupper($route['verb'] ?? 'GET'); |
|
| 136 | + |
|
| 137 | + $split = explode('#', $name, 2); |
|
| 138 | + if (count($split) !== 2) { |
|
| 139 | + throw new \UnexpectedValueException('Invalid route name'); |
|
| 140 | + } |
|
| 141 | + list($controller, $action) = $split; |
|
| 142 | + |
|
| 143 | + $controllerName = $this->buildControllerName($controller); |
|
| 144 | + $actionName = $this->buildActionName($action); |
|
| 145 | + |
|
| 146 | + $routeName = $routeNamePrefix . $this->appName . '.' . $controller . '.' . $action . $postfix; |
|
| 147 | + |
|
| 148 | + // register the route |
|
| 149 | + $handler = new RouteActionHandler($this->container, $controllerName, $actionName); |
|
| 150 | + |
|
| 151 | + $router = $this->router->create($routeName, $url) |
|
| 152 | + ->method($verb) |
|
| 153 | + ->action($handler); |
|
| 154 | + |
|
| 155 | + // optionally register requirements for route. This is used to |
|
| 156 | + // tell the route parser how url parameters should be matched |
|
| 157 | + if (array_key_exists('requirements', $route)) { |
|
| 158 | + $router->requirements($route['requirements']); |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + // optionally register defaults for route. This is used to |
|
| 162 | + // tell the route parser how url parameters should be default valued |
|
| 163 | + if (array_key_exists('defaults', $route)) { |
|
| 164 | + $router->defaults($route['defaults']); |
|
| 165 | + } |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + /** |
|
| 169 | + * For a given name and url restful OCS routes are created: |
|
| 170 | + * - index |
|
| 171 | + * - show |
|
| 172 | + * - create |
|
| 173 | + * - update |
|
| 174 | + * - destroy |
|
| 175 | + * |
|
| 176 | + * @param array $routes |
|
| 177 | + */ |
|
| 178 | + private function processOCSResources(array $routes): void { |
|
| 179 | + // declaration of all restful actions |
|
| 180 | + $actions = [ |
|
| 181 | + ['name' => 'index', 'verb' => 'GET', 'on-collection' => true], |
|
| 182 | + ['name' => 'show', 'verb' => 'GET'], |
|
| 183 | + ['name' => 'create', 'verb' => 'POST', 'on-collection' => true], |
|
| 184 | + ['name' => 'update', 'verb' => 'PUT'], |
|
| 185 | + ['name' => 'destroy', 'verb' => 'DELETE'], |
|
| 186 | + ]; |
|
| 187 | + |
|
| 188 | + $resources = $routes['ocs-resources'] ?? []; |
|
| 189 | + foreach ($resources as $resource => $config) { |
|
| 190 | + $root = $config['root'] ?? '/apps/' . $this->appName; |
|
| 191 | + |
|
| 192 | + // the url parameter used as id to the resource |
|
| 193 | + foreach ($actions as $action) { |
|
| 194 | + $url = $root . $config['url']; |
|
| 195 | + $method = $action['name']; |
|
| 196 | + $verb = strtoupper($action['verb'] ?? 'GET'); |
|
| 197 | + $collectionAction = $action['on-collection'] ?? false; |
|
| 198 | + if (!$collectionAction) { |
|
| 199 | + $url .= '/{id}'; |
|
| 200 | + } |
|
| 201 | + if (isset($action['url-postfix'])) { |
|
| 202 | + $url .= '/' . $action['url-postfix']; |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + $controller = $resource; |
|
| 206 | + |
|
| 207 | + $controllerName = $this->buildControllerName($controller); |
|
| 208 | + $actionName = $this->buildActionName($method); |
|
| 209 | + |
|
| 210 | + $routeName = 'ocs.' . $this->appName . '.' . strtolower($resource) . '.' . strtolower($method); |
|
| 211 | + |
|
| 212 | + $this->router->create($routeName, $url)->method($verb)->action( |
|
| 213 | + new RouteActionHandler($this->container, $controllerName, $actionName) |
|
| 214 | + ); |
|
| 215 | + } |
|
| 216 | + } |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + /** |
|
| 220 | + * For a given name and url restful routes are created: |
|
| 221 | + * - index |
|
| 222 | + * - show |
|
| 223 | + * - create |
|
| 224 | + * - update |
|
| 225 | + * - destroy |
|
| 226 | + * |
|
| 227 | + * @param array $routes |
|
| 228 | + */ |
|
| 229 | + private function processResources(array $routes): void { |
|
| 230 | + // declaration of all restful actions |
|
| 231 | + $actions = [ |
|
| 232 | + ['name' => 'index', 'verb' => 'GET', 'on-collection' => true], |
|
| 233 | + ['name' => 'show', 'verb' => 'GET'], |
|
| 234 | + ['name' => 'create', 'verb' => 'POST', 'on-collection' => true], |
|
| 235 | + ['name' => 'update', 'verb' => 'PUT'], |
|
| 236 | + ['name' => 'destroy', 'verb' => 'DELETE'], |
|
| 237 | + ]; |
|
| 238 | + |
|
| 239 | + $resources = $routes['resources'] ?? []; |
|
| 240 | + foreach ($resources as $resource => $config) { |
|
| 241 | + |
|
| 242 | + // the url parameter used as id to the resource |
|
| 243 | + foreach ($actions as $action) { |
|
| 244 | + $url = $config['url']; |
|
| 245 | + $method = $action['name']; |
|
| 246 | + $verb = strtoupper($action['verb'] ?? 'GET'); |
|
| 247 | + $collectionAction = $action['on-collection'] ?? false; |
|
| 248 | + if (!$collectionAction) { |
|
| 249 | + $url .= '/{id}'; |
|
| 250 | + } |
|
| 251 | + if (isset($action['url-postfix'])) { |
|
| 252 | + $url .= '/' . $action['url-postfix']; |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + $controller = $resource; |
|
| 256 | + |
|
| 257 | + $controllerName = $this->buildControllerName($controller); |
|
| 258 | + $actionName = $this->buildActionName($method); |
|
| 259 | + |
|
| 260 | + $routeName = $this->appName . '.' . strtolower($resource) . '.' . strtolower($method); |
|
| 261 | + |
|
| 262 | + $this->router->create($routeName, $url)->method($verb)->action( |
|
| 263 | + new RouteActionHandler($this->container, $controllerName, $actionName) |
|
| 264 | + ); |
|
| 265 | + } |
|
| 266 | + } |
|
| 267 | + } |
|
| 268 | + |
|
| 269 | + /** |
|
| 270 | + * Based on a given route name the controller name is generated |
|
| 271 | + * @param string $controller |
|
| 272 | + * @return string |
|
| 273 | + */ |
|
| 274 | + private function buildControllerName(string $controller): string { |
|
| 275 | + if (!isset($this->controllerNameCache[$controller])) { |
|
| 276 | + $this->controllerNameCache[$controller] = $this->underScoreToCamelCase(ucfirst($controller)) . 'Controller'; |
|
| 277 | + } |
|
| 278 | + return $this->controllerNameCache[$controller]; |
|
| 279 | + } |
|
| 280 | + |
|
| 281 | + /** |
|
| 282 | + * Based on the action part of the route name the controller method name is generated |
|
| 283 | + * @param string $action |
|
| 284 | + * @return string |
|
| 285 | + */ |
|
| 286 | + private function buildActionName(string $action): string { |
|
| 287 | + return $this->underScoreToCamelCase($action); |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + /** |
|
| 291 | + * Underscored strings are converted to camel case strings |
|
| 292 | + * @param string $str |
|
| 293 | + * @return string |
|
| 294 | + */ |
|
| 295 | + private function underScoreToCamelCase(string $str): string { |
|
| 296 | + $pattern = '/_[a-z]?/'; |
|
| 297 | + return preg_replace_callback( |
|
| 298 | + $pattern, |
|
| 299 | + function ($matches) { |
|
| 300 | + return strtoupper(ltrim($matches[0], '_')); |
|
| 301 | + }, |
|
| 302 | + $str); |
|
| 303 | + } |
|
| 304 | 304 | } |
@@ -91,7 +91,7 @@ discard block |
||
| 91 | 91 | * OCS routes go into a different collection |
| 92 | 92 | */ |
| 93 | 93 | $oldCollection = $this->router->getCurrentCollection(); |
| 94 | - $this->router->useCollection($oldCollection . '.ocs'); |
|
| 94 | + $this->router->useCollection($oldCollection.'.ocs'); |
|
| 95 | 95 | |
| 96 | 96 | // parse ocs simple routes |
| 97 | 97 | $this->processOCS($this->routes); |
@@ -124,14 +124,14 @@ discard block |
||
| 124 | 124 | protected function processRoute(array $route, string $routeNamePrefix = ''): void { |
| 125 | 125 | $name = $route['name']; |
| 126 | 126 | $postfix = $route['postfix'] ?? ''; |
| 127 | - $defaultRoot = $this->appName === 'core' ? '' : '/apps/' . $this->appName; |
|
| 127 | + $defaultRoot = $this->appName === 'core' ? '' : '/apps/'.$this->appName; |
|
| 128 | 128 | $root = $route['root'] ?? $defaultRoot; |
| 129 | 129 | if ($routeNamePrefix === '' && !\in_array($this->appName, $this->rootUrlApps, true)) { |
| 130 | 130 | // Only allow root URLS for some apps |
| 131 | 131 | $root = $defaultRoot; |
| 132 | 132 | } |
| 133 | 133 | |
| 134 | - $url = $root . $route['url']; |
|
| 134 | + $url = $root.$route['url']; |
|
| 135 | 135 | $verb = strtoupper($route['verb'] ?? 'GET'); |
| 136 | 136 | |
| 137 | 137 | $split = explode('#', $name, 2); |
@@ -143,7 +143,7 @@ discard block |
||
| 143 | 143 | $controllerName = $this->buildControllerName($controller); |
| 144 | 144 | $actionName = $this->buildActionName($action); |
| 145 | 145 | |
| 146 | - $routeName = $routeNamePrefix . $this->appName . '.' . $controller . '.' . $action . $postfix; |
|
| 146 | + $routeName = $routeNamePrefix.$this->appName.'.'.$controller.'.'.$action.$postfix; |
|
| 147 | 147 | |
| 148 | 148 | // register the route |
| 149 | 149 | $handler = new RouteActionHandler($this->container, $controllerName, $actionName); |
@@ -187,11 +187,11 @@ discard block |
||
| 187 | 187 | |
| 188 | 188 | $resources = $routes['ocs-resources'] ?? []; |
| 189 | 189 | foreach ($resources as $resource => $config) { |
| 190 | - $root = $config['root'] ?? '/apps/' . $this->appName; |
|
| 190 | + $root = $config['root'] ?? '/apps/'.$this->appName; |
|
| 191 | 191 | |
| 192 | 192 | // the url parameter used as id to the resource |
| 193 | 193 | foreach ($actions as $action) { |
| 194 | - $url = $root . $config['url']; |
|
| 194 | + $url = $root.$config['url']; |
|
| 195 | 195 | $method = $action['name']; |
| 196 | 196 | $verb = strtoupper($action['verb'] ?? 'GET'); |
| 197 | 197 | $collectionAction = $action['on-collection'] ?? false; |
@@ -199,7 +199,7 @@ discard block |
||
| 199 | 199 | $url .= '/{id}'; |
| 200 | 200 | } |
| 201 | 201 | if (isset($action['url-postfix'])) { |
| 202 | - $url .= '/' . $action['url-postfix']; |
|
| 202 | + $url .= '/'.$action['url-postfix']; |
|
| 203 | 203 | } |
| 204 | 204 | |
| 205 | 205 | $controller = $resource; |
@@ -207,7 +207,7 @@ discard block |
||
| 207 | 207 | $controllerName = $this->buildControllerName($controller); |
| 208 | 208 | $actionName = $this->buildActionName($method); |
| 209 | 209 | |
| 210 | - $routeName = 'ocs.' . $this->appName . '.' . strtolower($resource) . '.' . strtolower($method); |
|
| 210 | + $routeName = 'ocs.'.$this->appName.'.'.strtolower($resource).'.'.strtolower($method); |
|
| 211 | 211 | |
| 212 | 212 | $this->router->create($routeName, $url)->method($verb)->action( |
| 213 | 213 | new RouteActionHandler($this->container, $controllerName, $actionName) |
@@ -249,7 +249,7 @@ discard block |
||
| 249 | 249 | $url .= '/{id}'; |
| 250 | 250 | } |
| 251 | 251 | if (isset($action['url-postfix'])) { |
| 252 | - $url .= '/' . $action['url-postfix']; |
|
| 252 | + $url .= '/'.$action['url-postfix']; |
|
| 253 | 253 | } |
| 254 | 254 | |
| 255 | 255 | $controller = $resource; |
@@ -257,7 +257,7 @@ discard block |
||
| 257 | 257 | $controllerName = $this->buildControllerName($controller); |
| 258 | 258 | $actionName = $this->buildActionName($method); |
| 259 | 259 | |
| 260 | - $routeName = $this->appName . '.' . strtolower($resource) . '.' . strtolower($method); |
|
| 260 | + $routeName = $this->appName.'.'.strtolower($resource).'.'.strtolower($method); |
|
| 261 | 261 | |
| 262 | 262 | $this->router->create($routeName, $url)->method($verb)->action( |
| 263 | 263 | new RouteActionHandler($this->container, $controllerName, $actionName) |
@@ -273,7 +273,7 @@ discard block |
||
| 273 | 273 | */ |
| 274 | 274 | private function buildControllerName(string $controller): string { |
| 275 | 275 | if (!isset($this->controllerNameCache[$controller])) { |
| 276 | - $this->controllerNameCache[$controller] = $this->underScoreToCamelCase(ucfirst($controller)) . 'Controller'; |
|
| 276 | + $this->controllerNameCache[$controller] = $this->underScoreToCamelCase(ucfirst($controller)).'Controller'; |
|
| 277 | 277 | } |
| 278 | 278 | return $this->controllerNameCache[$controller]; |
| 279 | 279 | } |
@@ -296,7 +296,7 @@ discard block |
||
| 296 | 296 | $pattern = '/_[a-z]?/'; |
| 297 | 297 | return preg_replace_callback( |
| 298 | 298 | $pattern, |
| 299 | - function ($matches) { |
|
| 299 | + function($matches) { |
|
| 300 | 300 | return strtoupper(ltrim($matches[0], '_')); |
| 301 | 301 | }, |
| 302 | 302 | $str); |
@@ -45,167 +45,167 @@ |
||
| 45 | 45 | */ |
| 46 | 46 | abstract class AuthPublicShareController extends PublicShareController { |
| 47 | 47 | |
| 48 | - /** @var IURLGenerator */ |
|
| 49 | - protected $urlGenerator; |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * @since 14.0.0 |
|
| 53 | - */ |
|
| 54 | - public function __construct(string $appName, |
|
| 55 | - IRequest $request, |
|
| 56 | - ISession $session, |
|
| 57 | - IURLGenerator $urlGenerator) { |
|
| 58 | - parent::__construct($appName, $request, $session); |
|
| 59 | - |
|
| 60 | - $this->urlGenerator = $urlGenerator; |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * @PublicPage |
|
| 65 | - * @NoCSRFRequired |
|
| 66 | - * |
|
| 67 | - * Show the authentication page |
|
| 68 | - * The form has to submit to the authenticate method route |
|
| 69 | - * |
|
| 70 | - * @since 14.0.0 |
|
| 71 | - */ |
|
| 72 | - public function showAuthenticate(): TemplateResponse { |
|
| 73 | - return new TemplateResponse('core', 'publicshareauth', [], 'guest'); |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * The template to show when authentication failed |
|
| 78 | - * |
|
| 79 | - * @since 14.0.0 |
|
| 80 | - */ |
|
| 81 | - protected function showAuthFailed(): TemplateResponse { |
|
| 82 | - return new TemplateResponse('core', 'publicshareauth', ['wrongpw' => true], 'guest'); |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * Verify the password |
|
| 87 | - * |
|
| 88 | - * @since 14.0.0 |
|
| 89 | - */ |
|
| 90 | - abstract protected function verifyPassword(string $password): bool; |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * Function called after failed authentication |
|
| 94 | - * |
|
| 95 | - * You can use this to do some logging for example |
|
| 96 | - * |
|
| 97 | - * @since 14.0.0 |
|
| 98 | - */ |
|
| 99 | - protected function authFailed() { |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * Function called after successfull authentication |
|
| 104 | - * |
|
| 105 | - * You can use this to do some logging for example |
|
| 106 | - * |
|
| 107 | - * @since 14.0.0 |
|
| 108 | - */ |
|
| 109 | - protected function authSucceeded() { |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * @UseSession |
|
| 114 | - * @PublicPage |
|
| 115 | - * @BruteForceProtection(action=publicLinkAuth) |
|
| 116 | - * |
|
| 117 | - * Authenticate the share |
|
| 118 | - * |
|
| 119 | - * @since 14.0.0 |
|
| 120 | - */ |
|
| 121 | - final public function authenticate(string $password = '') { |
|
| 122 | - // Already authenticated |
|
| 123 | - if ($this->isAuthenticated()) { |
|
| 124 | - return $this->getRedirect(); |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - if (!$this->verifyPassword($password)) { |
|
| 128 | - $this->authFailed(); |
|
| 129 | - $response = $this->showAuthFailed(); |
|
| 130 | - $response->throttle(); |
|
| 131 | - return $response; |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - $this->session->regenerateId(true, true); |
|
| 135 | - $response = $this->getRedirect(); |
|
| 136 | - |
|
| 137 | - $this->session->set('public_link_authenticated_token', $this->getToken()); |
|
| 138 | - $this->session->set('public_link_authenticated_password_hash', $this->getPasswordHash()); |
|
| 139 | - |
|
| 140 | - $this->authSucceeded(); |
|
| 141 | - |
|
| 142 | - return $response; |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * Default landing page |
|
| 147 | - * |
|
| 148 | - * @since 14.0.0 |
|
| 149 | - */ |
|
| 150 | - abstract public function showShare(): TemplateResponse; |
|
| 151 | - |
|
| 152 | - /** |
|
| 153 | - * @since 14.0.0 |
|
| 154 | - */ |
|
| 155 | - final public function getAuthenticationRedirect(string $redirect): RedirectResponse { |
|
| 156 | - return new RedirectResponse( |
|
| 157 | - $this->urlGenerator->linkToRoute($this->getRoute('showAuthenticate'), ['token' => $this->getToken(), 'redirect' => $redirect]) |
|
| 158 | - ); |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * @since 14.0.0 |
|
| 164 | - */ |
|
| 165 | - private function getRoute(string $function): string { |
|
| 166 | - $app = strtolower($this->appName); |
|
| 167 | - $class = (new \ReflectionClass($this))->getShortName(); |
|
| 168 | - if (substr($class, -10) === 'Controller') { |
|
| 169 | - $class = substr($class, 0, -10); |
|
| 170 | - } |
|
| 171 | - return $app .'.'. $class .'.'. $function; |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - /** |
|
| 175 | - * @since 14.0.0 |
|
| 176 | - */ |
|
| 177 | - private function getRedirect(): RedirectResponse { |
|
| 178 | - //Get all the stored redirect parameters: |
|
| 179 | - $params = $this->session->get('public_link_authenticate_redirect'); |
|
| 180 | - |
|
| 181 | - $route = $this->getRoute('showShare'); |
|
| 182 | - |
|
| 183 | - if ($params === null) { |
|
| 184 | - $params = [ |
|
| 185 | - 'token' => $this->getToken(), |
|
| 186 | - ]; |
|
| 187 | - } else { |
|
| 188 | - $params = json_decode($params, true); |
|
| 189 | - if (isset($params['_route'])) { |
|
| 190 | - $route = $params['_route']; |
|
| 191 | - unset($params['_route']); |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - // If the token doesn't match the rest of the arguments can't be trusted either |
|
| 195 | - if (isset($params['token']) && $params['token'] !== $this->getToken()) { |
|
| 196 | - $params = [ |
|
| 197 | - 'token' => $this->getToken(), |
|
| 198 | - ]; |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - // We need a token |
|
| 202 | - if (!isset($params['token'])) { |
|
| 203 | - $params = [ |
|
| 204 | - 'token' => $this->getToken(), |
|
| 205 | - ]; |
|
| 206 | - } |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - return new RedirectResponse($this->urlGenerator->linkToRoute($route, $params)); |
|
| 210 | - } |
|
| 48 | + /** @var IURLGenerator */ |
|
| 49 | + protected $urlGenerator; |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * @since 14.0.0 |
|
| 53 | + */ |
|
| 54 | + public function __construct(string $appName, |
|
| 55 | + IRequest $request, |
|
| 56 | + ISession $session, |
|
| 57 | + IURLGenerator $urlGenerator) { |
|
| 58 | + parent::__construct($appName, $request, $session); |
|
| 59 | + |
|
| 60 | + $this->urlGenerator = $urlGenerator; |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * @PublicPage |
|
| 65 | + * @NoCSRFRequired |
|
| 66 | + * |
|
| 67 | + * Show the authentication page |
|
| 68 | + * The form has to submit to the authenticate method route |
|
| 69 | + * |
|
| 70 | + * @since 14.0.0 |
|
| 71 | + */ |
|
| 72 | + public function showAuthenticate(): TemplateResponse { |
|
| 73 | + return new TemplateResponse('core', 'publicshareauth', [], 'guest'); |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * The template to show when authentication failed |
|
| 78 | + * |
|
| 79 | + * @since 14.0.0 |
|
| 80 | + */ |
|
| 81 | + protected function showAuthFailed(): TemplateResponse { |
|
| 82 | + return new TemplateResponse('core', 'publicshareauth', ['wrongpw' => true], 'guest'); |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * Verify the password |
|
| 87 | + * |
|
| 88 | + * @since 14.0.0 |
|
| 89 | + */ |
|
| 90 | + abstract protected function verifyPassword(string $password): bool; |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * Function called after failed authentication |
|
| 94 | + * |
|
| 95 | + * You can use this to do some logging for example |
|
| 96 | + * |
|
| 97 | + * @since 14.0.0 |
|
| 98 | + */ |
|
| 99 | + protected function authFailed() { |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * Function called after successfull authentication |
|
| 104 | + * |
|
| 105 | + * You can use this to do some logging for example |
|
| 106 | + * |
|
| 107 | + * @since 14.0.0 |
|
| 108 | + */ |
|
| 109 | + protected function authSucceeded() { |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * @UseSession |
|
| 114 | + * @PublicPage |
|
| 115 | + * @BruteForceProtection(action=publicLinkAuth) |
|
| 116 | + * |
|
| 117 | + * Authenticate the share |
|
| 118 | + * |
|
| 119 | + * @since 14.0.0 |
|
| 120 | + */ |
|
| 121 | + final public function authenticate(string $password = '') { |
|
| 122 | + // Already authenticated |
|
| 123 | + if ($this->isAuthenticated()) { |
|
| 124 | + return $this->getRedirect(); |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + if (!$this->verifyPassword($password)) { |
|
| 128 | + $this->authFailed(); |
|
| 129 | + $response = $this->showAuthFailed(); |
|
| 130 | + $response->throttle(); |
|
| 131 | + return $response; |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + $this->session->regenerateId(true, true); |
|
| 135 | + $response = $this->getRedirect(); |
|
| 136 | + |
|
| 137 | + $this->session->set('public_link_authenticated_token', $this->getToken()); |
|
| 138 | + $this->session->set('public_link_authenticated_password_hash', $this->getPasswordHash()); |
|
| 139 | + |
|
| 140 | + $this->authSucceeded(); |
|
| 141 | + |
|
| 142 | + return $response; |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + /** |
|
| 146 | + * Default landing page |
|
| 147 | + * |
|
| 148 | + * @since 14.0.0 |
|
| 149 | + */ |
|
| 150 | + abstract public function showShare(): TemplateResponse; |
|
| 151 | + |
|
| 152 | + /** |
|
| 153 | + * @since 14.0.0 |
|
| 154 | + */ |
|
| 155 | + final public function getAuthenticationRedirect(string $redirect): RedirectResponse { |
|
| 156 | + return new RedirectResponse( |
|
| 157 | + $this->urlGenerator->linkToRoute($this->getRoute('showAuthenticate'), ['token' => $this->getToken(), 'redirect' => $redirect]) |
|
| 158 | + ); |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * @since 14.0.0 |
|
| 164 | + */ |
|
| 165 | + private function getRoute(string $function): string { |
|
| 166 | + $app = strtolower($this->appName); |
|
| 167 | + $class = (new \ReflectionClass($this))->getShortName(); |
|
| 168 | + if (substr($class, -10) === 'Controller') { |
|
| 169 | + $class = substr($class, 0, -10); |
|
| 170 | + } |
|
| 171 | + return $app .'.'. $class .'.'. $function; |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + /** |
|
| 175 | + * @since 14.0.0 |
|
| 176 | + */ |
|
| 177 | + private function getRedirect(): RedirectResponse { |
|
| 178 | + //Get all the stored redirect parameters: |
|
| 179 | + $params = $this->session->get('public_link_authenticate_redirect'); |
|
| 180 | + |
|
| 181 | + $route = $this->getRoute('showShare'); |
|
| 182 | + |
|
| 183 | + if ($params === null) { |
|
| 184 | + $params = [ |
|
| 185 | + 'token' => $this->getToken(), |
|
| 186 | + ]; |
|
| 187 | + } else { |
|
| 188 | + $params = json_decode($params, true); |
|
| 189 | + if (isset($params['_route'])) { |
|
| 190 | + $route = $params['_route']; |
|
| 191 | + unset($params['_route']); |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + // If the token doesn't match the rest of the arguments can't be trusted either |
|
| 195 | + if (isset($params['token']) && $params['token'] !== $this->getToken()) { |
|
| 196 | + $params = [ |
|
| 197 | + 'token' => $this->getToken(), |
|
| 198 | + ]; |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + // We need a token |
|
| 202 | + if (!isset($params['token'])) { |
|
| 203 | + $params = [ |
|
| 204 | + 'token' => $this->getToken(), |
|
| 205 | + ]; |
|
| 206 | + } |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + return new RedirectResponse($this->urlGenerator->linkToRoute($route, $params)); |
|
| 210 | + } |
|
| 211 | 211 | } |
@@ -168,7 +168,7 @@ |
||
| 168 | 168 | if (substr($class, -10) === 'Controller') { |
| 169 | 169 | $class = substr($class, 0, -10); |
| 170 | 170 | } |
| 171 | - return $app .'.'. $class .'.'. $function; |
|
| 171 | + return $app.'.'.$class.'.'.$function; |
|
| 172 | 172 | } |
| 173 | 173 | |
| 174 | 174 | /** |