Allow requesting absolute URLs

They might be useful when requesting the navigation from the clients

Signed-off-by: Julius Härtl <jus@bitgrid.net>
This commit is contained in:
Julius Härtl 2018-01-29 12:52:40 +01:00
parent 6211d18dc1
commit 8ecac56543
No known key found for this signature in database
GPG Key ID: 4C614C6ED2CDE6DF
2 changed files with 76 additions and 11 deletions

View File

@ -26,34 +26,62 @@ use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\JSONResponse; use OCP\AppFramework\Http\JSONResponse;
use OCP\INavigationManager; use OCP\INavigationManager;
use OCP\IRequest; use OCP\IRequest;
use OCP\IURLGenerator;
class NavigationController extends Controller { class NavigationController extends Controller {
/** @var INavigationManager */ /** @var INavigationManager */
private $navigationManager; private $navigationManager;
public function __construct(string $appName, IRequest $request, INavigationManager $navigationManager) { /** @var IURLGenerator */
private $urlGenerator;
public function __construct(string $appName, IRequest $request, INavigationManager $navigationManager, IURLGenerator $urlGenerator) {
parent::__construct($appName, $request); parent::__construct($appName, $request);
$this->navigationManager = $navigationManager; $this->navigationManager = $navigationManager;
$this->urlGenerator = $urlGenerator;
} }
/** /**
* @NoAdminRequired * @NoAdminRequired
* @NoCSRFRequired * @NoCSRFRequired
* *
* @param bool $absolute
* @return JSONResponse * @return JSONResponse
*/ */
public function getAppsNavigation($absolute = false) { public function getAppsNavigation(bool $absolute = false) {
return new JSONResponse($this->navigationManager->getAll('link', $absolute)); $navigation = $this->navigationManager->getAll('link');
if ($absolute) {
$this->rewriteToAbsoluteUrls($navigation);
}
return new JSONResponse($navigation);
} }
/** /**
* @NoAdminRequired * @NoAdminRequired
* @NoCSRFRequired * @NoCSRFRequired
* *
* @param bool $absolute
* @return JSONResponse * @return JSONResponse
*/ */
public function getSettingsNavigation($absolute = false) { public function getSettingsNavigation(bool $absolute = false) {
return new JSONResponse($this->navigationManager->getAll('settings', $absolute)); $navigation = $this->navigationManager->getAll('settings');
if ($absolute) {
$this->rewriteToAbsoluteUrls($navigation);
}
return new JSONResponse($navigation);
}
/**
* Rewrite href attribute of navigation entries to an absolute URL
*
* @param array $navigation
*/
private function rewriteToAbsoluteUrls(array &$navigation) {
foreach ($navigation as &$entry) {
if (substr($entry['href'], 0, strlen($this->urlGenerator->getBaseUrl())) !== $this->urlGenerator->getBaseUrl()) {
$entry['href'] = $this->urlGenerator->getAbsoluteURL($entry['href']);
}
}
} }
} }

View File

@ -26,6 +26,7 @@ use OC\Core\Controller\NavigationController;
use OCP\AppFramework\Http\JSONResponse; use OCP\AppFramework\Http\JSONResponse;
use OCP\INavigationManager; use OCP\INavigationManager;
use OCP\IRequest; use OCP\IRequest;
use OCP\IURLGenerator;
use Test\TestCase; use Test\TestCase;
class NavigationControllerTest extends TestCase { class NavigationControllerTest extends TestCase {
@ -36,20 +37,24 @@ class NavigationControllerTest extends TestCase {
/** @var INavigationManager|\PHPUnit_Framework_MockObject_MockObject */ /** @var INavigationManager|\PHPUnit_Framework_MockObject_MockObject */
private $navigationManager; private $navigationManager;
/** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */
private $urlGenerator;
/** @var NavigationController */ /** @var NavigationController */
private $controller; private $controller;
public function setUp() { public function setUp() {
parent::setUp(); parent::setUp();
$this->request = $this->createMock(IRequest::class); $this->request = $this->createMock(IRequest::class);
$this->navigationManager = $this->createMock(INavigationManager::class); $this->navigationManager = $this->createMock(INavigationManager::class);
$this->urlGenerator = $this->createMock(IURLGenerator::class);
$this->controller = new NavigationController( $this->controller = new NavigationController(
'core', 'core',
$this->request, $this->request,
$this->navigationManager $this->navigationManager,
$this->urlGenerator
); );
} }
@ -62,16 +67,48 @@ class NavigationControllerTest extends TestCase {
public function testGetAppNavigation($absolute) { public function testGetAppNavigation($absolute) {
$this->navigationManager->expects($this->once()) $this->navigationManager->expects($this->once())
->method('getAll') ->method('getAll')
->with('link', $absolute); ->with('link')
$this->assertInstanceOf(JSONResponse::class, $this->controller->getAppsNavigation($absolute)); ->willReturn([ ['id' => 'files', 'href' => '/index.php/apps/files'] ]);
if ($absolute) {
$this->urlGenerator->expects($this->any())
->method('getBaseURL')
->willReturn('http://localhost/');
$this->urlGenerator->expects($this->once())
->method('getAbsoluteURL')
->with('/index.php/apps/files')
->willReturn('http://localhost/index.php/apps/files');
$actual = $this->controller->getAppsNavigation($absolute);
$this->assertInstanceOf(JSONResponse::class, $actual);
$this->assertEquals('http://localhost/index.php/apps/files', $actual->getData()[0]['href']);
} else {
$actual = $this->controller->getAppsNavigation($absolute);
$this->assertInstanceOf(JSONResponse::class, $actual);
$this->assertEquals('/index.php/apps/files', $actual->getData()[0]['href']);
}
} }
/** @dataProvider dataGetNavigation */ /** @dataProvider dataGetNavigation */
public function testGetSettingsNavigation($absolute) { public function testGetSettingsNavigation($absolute) {
$this->navigationManager->expects($this->once()) $this->navigationManager->expects($this->once())
->method('getAll') ->method('getAll')
->with('settings', $absolute); ->with('settings')
$this->assertInstanceOf(JSONResponse::class, $this->controller->getSettingsNavigation($absolute)); ->willReturn([ ['id' => 'settings', 'href' => '/index.php/settings/user'] ]);
if ($absolute) {
$this->urlGenerator->expects($this->any())
->method('getBaseURL')
->willReturn('http://localhost/');
$this->urlGenerator->expects($this->once())
->method('getAbsoluteURL')
->with('/index.php/settings/user')
->willReturn('http://localhost/index.php/settings/user');
$actual = $this->controller->getSettingsNavigation($absolute);
$this->assertInstanceOf(JSONResponse::class, $actual);
$this->assertEquals('http://localhost/index.php/settings/user', $actual->getData()[0]['href']);
} else {
$actual = $this->controller->getSettingsNavigation($absolute);
$this->assertInstanceOf(JSONResponse::class, $actual);
$this->assertEquals('/index.php/settings/user', $actual->getData()[0]['href']);
}
} }
} }