Skip to content

Commit dd51a56

Browse files
committed
Load files navigation elements from info.xml
1 parent acd7ddf commit dd51a56

6 files changed

Lines changed: 110 additions & 20 deletions

File tree

apps/files/appinfo/app.php

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,8 @@
2727
*/
2828
\OCP\App::registerAdmin('files', 'admin');
2929

30-
31-
\OC::$server->getNavigationManager()->add(function () {
32-
$urlGenerator = \OC::$server->getURLGenerator();
33-
$l = \OC::$server->getL10N('files');
34-
return [
35-
'id' => 'files_index',
36-
'order' => 0,
37-
'href' => $urlGenerator->linkToRoute('files.view.index'),
38-
'icon' => $urlGenerator->imagePath('core', 'places/files.svg'),
39-
'name' => $l->t('Files'),
40-
];
41-
});
30+
// required for translation purpose
31+
// t('Files')
4232

4333
\OC::$server->getSearch()->registerProvider('OC\Search\Provider\File', ['apps' => ['files']]);
4434

apps/files/appinfo/info.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,10 @@
2828
<command>OCA\Files\Command\DeleteOrphanedFiles</command>
2929
<command>OCA\Files\Command\TransferOwnership</command>
3030
</commands>
31+
32+
<navigation>
33+
<route>files.view.index</route>
34+
<order>0</order>
35+
</navigation>
36+
3137
</info>

apps/files/img/app.svg

Lines changed: 6 additions & 0 deletions
Loading

lib/private/NavigationManager.php

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@
2626
namespace OC;
2727

2828
use OCP\App\IAppManager;
29+
use OCP\IGroupManager;
2930
use OCP\INavigationManager;
3031
use OCP\IURLGenerator;
32+
use OCP\IUserSession;
3133
use OCP\L10N\IFactory;
3234

3335
/**
@@ -37,19 +39,29 @@ class NavigationManager implements INavigationManager {
3739
protected $entries = [];
3840
protected $closureEntries = [];
3941
protected $activeEntry;
42+
/** @var bool */
4043
protected $init = false;
4144
/** @var IAppManager */
4245
protected $appManager;
4346
/** @var IURLGenerator */
4447
private $urlGenerator;
4548
/** @var IFactory */
4649
private $l10nFac;
50+
/** @var IUserSession */
51+
private $userSession;
52+
/** @var IGroupManager */
53+
private $groupManager;
4754

48-
function __construct(IAppManager $appManager = null, $urlGenerator = null, $l10nFac = null) {
55+
function __construct(IAppManager $appManager = null,
56+
IURLGenerator $urlGenerator = null,
57+
IFactory $l10nFac = null,
58+
IUserSession $userSession = null,
59+
IGroupManager$groupManager = null) {
4960
$this->appManager = $appManager;
5061
$this->urlGenerator = $urlGenerator;
5162
$this->l10nFac = $l10nFac;
52-
63+
$this->userSession = $userSession;
64+
$this->groupManager = $groupManager;
5365
}
5466

5567
/**
@@ -132,15 +144,16 @@ private function init() {
132144
if (!isset($nav['route'])) {
133145
continue;
134146
}
147+
$role = isset($nav['@attributes']['role']) ? $nav['@attributes']['role'] : 'all';
148+
if ($role === 'admin' && !$this->isAdmin()) {
149+
continue;
150+
}
135151
$l = $this->l10nFac->get($app);
136152
$order = isset($nav['order']) ? $nav['order'] : 100;
137153
$route = $this->urlGenerator->linkToRoute($nav['route']);
138154
$name = isset($nav['name']) ? $nav['name'] : ucfirst($app);
139-
$icon = null;
140-
foreach ([$nav['icon'], 'app.svg', "$app.svg"] as $i) {
141-
if (is_null($i)) {
142-
continue;
143-
}
155+
$icon = isset($nav['icon']) ? $nav['icon'] : 'app.svg';
156+
foreach ([$icon, "$app.svg"] as $i) {
144157
try {
145158
$icon = $this->urlGenerator->imagePath($app, $i);
146159
break;
@@ -160,4 +173,13 @@ private function init() {
160173
]);
161174
}
162175
}
176+
177+
private function isAdmin() {
178+
$user = $this->userSession->getUser();
179+
if ($user !== null) {
180+
return $this->groupManager->isAdmin($user->getUID());
181+
}
182+
return false;
183+
}
184+
163185
}

lib/private/Server.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,9 @@ public function __construct($webRoot, \OC\Config $config) {
292292
$this->registerService('NavigationManager', function (Server $c) {
293293
return new \OC\NavigationManager($c->getAppManager(),
294294
$c->getURLGenerator(),
295-
$c->getL10NFactory());
295+
$c->getL10NFactory(),
296+
$c->getUserSession(),
297+
$c->getGroupManager());
296298
});
297299
$this->registerService('AllConfig', function (Server $c) {
298300
return new \OC\AllConfig(

tests/lib/NavigationManagerTest.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@
1313
namespace Test;
1414

1515
use OC\NavigationManager;
16+
use OCP\App\IAppManager;
17+
use OCP\IGroupManager;
18+
use OCP\IL10N;
19+
use OCP\IURLGenerator;
20+
use OCP\IUser;
21+
use OCP\IUserSession;
22+
use OCP\L10N\IFactory;
1623

1724
class NavigationManagerTest extends TestCase {
1825
/** @var \OC\NavigationManager */
@@ -157,4 +164,61 @@ public function testAddClosureClearGetAll() {
157164
$this->assertEmpty($this->navigationManager->getAll(), 'Expected no navigation entry exists after clear()');
158165
$this->assertEquals(0, $testAddClosureNumberOfCalls, 'Expected that the closure is not called by getAll()');
159166
}
167+
168+
/**
169+
* @dataProvider providesNavigationConfig
170+
*/
171+
public function testWithAppManager($expected, $config, $isAdmin = false) {
172+
173+
$appManager = $this->createMock(IAppManager::class);
174+
$urlGenerator = $this->createMock(IURLGenerator::class);
175+
$l10nFac = $this->createMock(IFactory::class);
176+
$userSession = $this->createMock(IUserSession::class);
177+
$groupManager = $this->createMock(IGroupManager::class);
178+
$l = $this->createMock(IL10N::class);
179+
$l->expects($this->any())->method('t')->willReturnCallback(function($text, $parameters = []) {
180+
return vsprintf($text, $parameters);
181+
});
182+
183+
$appManager->expects($this->once())->method('getInstalledApps')->willReturn(['test']);
184+
$appManager->expects($this->once())->method('getAppInfo')->with('test')->willReturn($config);
185+
$l10nFac->expects($this->exactly(count($expected)))->method('get')->with('test')->willReturn($l);
186+
$urlGenerator->expects($this->any())->method('imagePath')->willReturnCallback(function($appName, $file) {
187+
return "/apps/$appName/img/$file";
188+
});
189+
$urlGenerator->expects($this->exactly(count($expected)))->method('linkToRoute')->willReturnCallback(function($route) {
190+
return "/apps/test/";
191+
});
192+
$user = $this->createMock(IUser::class);
193+
$user->expects($this->any())->method('getUID')->willReturn('user001');
194+
$userSession->expects($this->any())->method('getUser')->willReturn($user);
195+
$groupManager->expects($this->any())->method('isAdmin')->willReturn($isAdmin);
196+
197+
$navigationManager = new NavigationManager($appManager, $urlGenerator, $l10nFac, $userSession, $groupManager);
198+
199+
$entries = $navigationManager->getAll();
200+
$this->assertEquals($expected, $entries);
201+
}
202+
203+
public function providesNavigationConfig() {
204+
return [
205+
'minimalistic' => [[[
206+
'id' => 'test',
207+
'order' => 100,
208+
'href' => '/apps/test/',
209+
'icon' => '/apps/test/img/app.svg',
210+
'name' => 'Test',
211+
'active' => false
212+
]], ['navigation' => ['route' => 'test.page.index']]],
213+
'no admin' => [[[
214+
'id' => 'test',
215+
'order' => 100,
216+
'href' => '/apps/test/',
217+
'icon' => '/apps/test/img/app.svg',
218+
'name' => 'Test',
219+
'active' => false
220+
]], ['navigation' => ['@attributes' => ['role' => 'admin'], 'route' => 'test.page.index']], true],
221+
'admin' => [[], ['navigation' => ['@attributes' => ['role' => 'admin'], 'route' => 'test.page.index']]]
222+
];
223+
}
160224
}

0 commit comments

Comments
 (0)