Skip to content

Commit 52a472d

Browse files
authored
Merge pull request #54020 from nextcloud/feat/42647/hide-app-password-note-without-2fa
feat(files): hide note about app passwords when 2FA not enabled
2 parents 9d04729 + cb29b30 commit 52a472d

File tree

5 files changed

+44
-7
lines changed

5 files changed

+44
-7
lines changed

apps/files/lib/Controller/ViewController.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use OCP\AppFramework\Http\Response;
2727
use OCP\AppFramework\Http\TemplateResponse;
2828
use OCP\AppFramework\Services\IInitialState;
29+
use OCP\Authentication\TwoFactorAuth\IRegistry;
2930
use OCP\Collaboration\Resources\LoadAdditionalScriptsEvent as ResourcesLoadAdditionalScriptsEvent;
3031
use OCP\EventDispatcher\IEventDispatcher;
3132
use OCP\Files\Folder;
@@ -60,6 +61,7 @@ public function __construct(
6061
private UserConfig $userConfig,
6162
private ViewConfig $viewConfig,
6263
private FilenameValidator $filenameValidator,
64+
private IRegistry $twoFactorRegistry,
6365
) {
6466
parent::__construct($appName, $request);
6567
}
@@ -142,7 +144,8 @@ public function index($dir = '', $view = '', $fileid = null) {
142144
Util::addInitScript('files', 'init');
143145
Util::addScript('files', 'main');
144146

145-
$userId = $this->userSession->getUser()->getUID();
147+
$user = $this->userSession->getUser();
148+
$userId = $user->getUID();
146149

147150
// If the file doesn't exists in the folder and
148151
// exists in only one occurrence, redirect to that file
@@ -195,6 +198,15 @@ public function index($dir = '', $view = '', $fileid = null) {
195198
$this->initialState->provideInitialState('templates_path', $this->templateManager->hasTemplateDirectory() ? $this->templateManager->getTemplatePath() : false);
196199
$this->initialState->provideInitialState('templates', $this->templateManager->listCreators());
197200

201+
$isTwoFactorEnabled = false;
202+
foreach ($this->twoFactorRegistry->getProviderStates($user) as $providerId => $providerState) {
203+
if ($providerId !== 'backup_codes' && $providerState === true) {
204+
$isTwoFactorEnabled = true;
205+
}
206+
}
207+
208+
$this->initialState->provideInitialState('isTwoFactorEnabled', $isTwoFactorEnabled);
209+
198210
$response = new TemplateResponse(
199211
Application::APP_ID,
200212
'index',

apps/files/src/views/Settings.vue

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,13 @@
9393
:href="webdavDocs"
9494
target="_blank"
9595
rel="noreferrer noopener">
96-
{{ t('files', 'Use this address to access your Files via WebDAV') }} ↗
96+
{{ t('files', 'Use this address to access your Files via WebDAV.') }} ↗
9797
</a>
9898
</em>
9999
<br>
100-
<em>
100+
<em v-if="isTwoFactorEnabled">
101101
<a class="setting-link" :href="appPasswordUrl">
102-
{{ t('files', 'If you have enabled 2FA, you must create and use a new app password by clicking here.') }} ↗
102+
{{ t('files', 'Two-Factor Authentication is enabled for your account, and therefore you need to use an app password to connect an external WebDAV client.') }} ↗
103103
</a>
104104
</em>
105105
</NcAppSettingsSection>
@@ -334,6 +334,7 @@ export default {
334334
appPasswordUrl: generateUrl('/settings/user/security#generate-app-token-section'),
335335
webdavUrlCopied: false,
336336
enableGridView: (loadState('core', 'config', [])['enable_non-accessible_features'] ?? true),
337+
isTwoFactorEnabled: (loadState('files', 'isTwoFactorEnabled', false)),
337338
}
338339
},
339340

apps/files/tests/Controller/ViewControllerTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use OCP\AppFramework\Http\RedirectResponse;
2020
use OCP\AppFramework\Http\TemplateResponse;
2121
use OCP\AppFramework\Services\IInitialState;
22+
use OCP\Authentication\TwoFactorAuth\IRegistry;
2223
use OCP\Diagnostics\IEventLogger;
2324
use OCP\EventDispatcher\IEventDispatcher;
2425
use OCP\Files\File;
@@ -63,6 +64,7 @@ class ViewControllerTest extends TestCase {
6364
private UserConfig&MockObject $userConfig;
6465
private ViewConfig&MockObject $viewConfig;
6566
private Router $router;
67+
private IRegistry&MockObject $twoFactorRegistry;
6668

6769
private ViewController&MockObject $viewController;
6870

@@ -79,6 +81,7 @@ protected function setUp(): void {
7981
$this->userConfig = $this->createMock(UserConfig::class);
8082
$this->userSession = $this->createMock(IUserSession::class);
8183
$this->viewConfig = $this->createMock(ViewConfig::class);
84+
$this->twoFactorRegistry = $this->createMock(IRegistry::class);
8285

8386
$this->user = $this->getMockBuilder(IUser::class)->getMock();
8487
$this->user->expects($this->any())
@@ -138,6 +141,7 @@ protected function setUp(): void {
138141
$this->userConfig,
139142
$this->viewConfig,
140143
$filenameValidator,
144+
$this->twoFactorRegistry,
141145
])
142146
->onlyMethods([
143147
'getStorageInfo',
@@ -286,4 +290,24 @@ public function testShowFileRouteWithTrashedFile(): void {
286290
$expected = new RedirectResponse('/index.php/apps/files/trashbin/123?dir=/test.d1462861890/sub');
287291
$this->assertEquals($expected, $this->viewController->index('', '', '123'));
288292
}
293+
294+
public function testTwoFactorAuthEnabled(): void {
295+
$this->twoFactorRegistry->method('getProviderStates')
296+
->willReturn([
297+
'totp' => true,
298+
'backup_codes' => true,
299+
]);
300+
301+
$invokedCountProvideInitialState = $this->exactly(9);
302+
$this->initialState->expects($invokedCountProvideInitialState)
303+
->method('provideInitialState')
304+
->willReturnCallback(function ($key, $data) use ($invokedCountProvideInitialState) {
305+
if ($invokedCountProvideInitialState->numberOfInvocations() === 9) {
306+
$this->assertEquals('isTwoFactorEnabled', $key);
307+
$this->assertTrue($data);
308+
}
309+
});
310+
311+
$this->viewController->index('', '', null);
312+
}
289313
}

dist/files-main.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/files-main.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)