Skip to content

Commit

Permalink
Add function to generate urls for OCS routes
Browse files Browse the repository at this point in the history
fixes #11617

The OCS routes are only absolute for now as they are often exposed to
the outside anyway and are on a different endpoint than index.php in
anyway.

Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
  • Loading branch information
rullzer committed Oct 9, 2018
1 parent 877823e commit c97b427
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/private/URLGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,19 @@ public function linkToRouteAbsolute(string $routeName, array $arguments = array(
return $this->getAbsoluteURL($this->linkToRoute($routeName, $arguments));
}

public function linkToOCSRouteAbsolute(string $routeName, array $arguments = []): string {
$route = \OC::$server->getRouter()->generate('ocs.'.$routeName, $arguments, false);

if (strpos($route, '/index.php') === 0) {
$route = substr($route, 10);
}

$route = substr($route, 7);
$route = '/ocs/v2.php' . $route;

return $this->getAbsoluteURL($route);
}

/**
* Creates an url
* @param string $app app
Expand Down
8 changes: 8 additions & 0 deletions lib/public/IURLGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ public function linkToRoute(string $routeName, array $arguments = array()): stri
*/
public function linkToRouteAbsolute(string $routeName, array $arguments = array()): string;

/**
* @param string $routeName
* @param array $arguments
* @return string
* @since 15.0.0
*/
public function linkToOCSRouteAbsolute(string $routeName, array $arguments = []): string;

/**
* Returns an URL for an image or file
* @param string $appName the name of the app
Expand Down
18 changes: 18 additions & 0 deletions tests/lib/UrlGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,22 @@ public function testGetBaseUrl() {
$this->assertEquals($expected, $actual);
}

/**
* @dataProvider provideOCSRoutes
*/
public function testLinkToOCSRouteAbsolute(string $route, string $expected) {
$this->mockBaseUrl();
\OC::$WEBROOT = '/nextcloud';
$result = $this->urlGenerator->linkToOCSRouteAbsolute($route);
$this->assertEquals($expected, $result);
}

public function provideOCSRoutes() {
return [
['core.OCS.getCapabilities', 'http://localhost/nextcloud/ocs/v2.php/cloud/capabilities'],
['core.WhatsNew.dismiss', 'http://localhost/nextcloud/ocs/v2.php/core/whatsnew'],
];
}


}

0 comments on commit c97b427

Please sign in to comment.