Skip to content

Commit 66e5ea1

Browse files
authored
Merge pull request #52537 from nextcloud/fix/loading-account-menu
feat(files_sharing): show Account menu on public pages
2 parents ebaccf6 + 20e4984 commit 66e5ea1

File tree

217 files changed

+956
-533
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

217 files changed

+956
-533
lines changed

apps/files_sharing/lib/DefaultPublicShareTemplateProvider.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,12 @@ public function renderPage(IShare $share, string $token, string $path): Template
107107
Util::addInitScript(Application::APP_ID, 'init');
108108
Util::addInitScript(Application::APP_ID, 'init-public');
109109
Util::addScript('files', 'main');
110+
Util::addScript(Application::APP_ID, 'public-nickname-handler');
110111

111112
// Add file-request script if needed
112113
$attributes = $share->getAttributes();
113114
$isFileRequest = $attributes?->getAttribute('fileRequest', 'enabled') === true;
114-
if ($isFileRequest) {
115-
Util::addScript(Application::APP_ID, 'public-file-request');
116-
}
115+
$this->initialState->provideInitialState('isFileRequest', $isFileRequest);
117116

118117
// Load Viewer scripts
119118
if (class_exists(LoadViewer::class)) {

apps/files_sharing/lib/Listener/LoadPublicFileRequestAuthListener.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use OCA\Files_Sharing\AppInfo\Application;
1111
use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent;
1212
use OCP\AppFramework\Http\TemplateResponse;
13+
use OCP\AppFramework\Services\IInitialState;
1314
use OCP\EventDispatcher\Event;
1415
use OCP\EventDispatcher\IEventListener;
1516
use OCP\Share\IManager;
@@ -19,6 +20,7 @@
1920
class LoadPublicFileRequestAuthListener implements IEventListener {
2021
public function __construct(
2122
private IManager $shareManager,
23+
private IInitialState $initialState,
2224
) {
2325
}
2426

@@ -51,9 +53,9 @@ public function handle(Event $event): void {
5153
// Ignore, this is not a file request or the share does not exist
5254
}
5355

54-
if ($isFileRequest) {
55-
// Add the script to the public page
56-
Util::addScript(Application::APP_ID, 'public-file-request');
57-
}
56+
Util::addScript(Application::APP_ID, 'public-nickname-handler');
57+
58+
// Add file-request script if needed
59+
$this->initialState->provideInitialState('isFileRequest', $isFileRequest);
5860
}
5961
}

apps/files_sharing/src/public-file-request.ts

Lines changed: 0 additions & 57 deletions
This file was deleted.
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/**
2+
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
3+
* SPDX-License-Identifier: AGPL-3.0-or-later
4+
*/
5+
6+
import { getBuilder } from '@nextcloud/browser-storage'
7+
import { getGuestNickname, type NextcloudUser } from '@nextcloud/auth'
8+
import { getUploader } from '@nextcloud/upload'
9+
import { loadState } from '@nextcloud/initial-state'
10+
import { showGuestUserPrompt } from '@nextcloud/dialogs'
11+
import { t } from '@nextcloud/l10n'
12+
13+
import logger from './services/logger'
14+
import { subscribe } from '@nextcloud/event-bus'
15+
16+
const storage = getBuilder('files_sharing').build()
17+
18+
// Setup file-request nickname header for the uploader
19+
const registerFileRequestHeader = (nickname: string) => {
20+
const uploader = getUploader()
21+
uploader.setCustomHeader('X-NC-Nickname', encodeURIComponent(nickname))
22+
logger.debug('Nickname header registered for uploader', { headers: uploader.customHeaders })
23+
}
24+
25+
// Callback when a nickname was chosen
26+
const onUserInfoChanged = (guest: NextcloudUser) => {
27+
logger.debug('User info changed', { guest })
28+
registerFileRequestHeader(guest.displayName ?? '')
29+
}
30+
31+
// Monitor nickname changes
32+
subscribe('user:info:changed', onUserInfoChanged)
33+
34+
window.addEventListener('DOMContentLoaded', () => {
35+
const nickname = getGuestNickname() ?? ''
36+
const dialogShown = storage.getItem('public-auth-prompt-shown') !== null
37+
38+
// Check if a nickname is mandatory
39+
const isFileRequest = loadState('files_sharing', 'isFileRequest', false)
40+
41+
const owner = loadState('files_sharing', 'owner', '')
42+
const ownerDisplayName = loadState('files_sharing', 'ownerDisplayName', '')
43+
const label = loadState('files_sharing', 'label', '')
44+
const filename = loadState('files_sharing', 'filename', '')
45+
46+
// If the owner provided a custom label, use it instead of the filename
47+
const folder = label || filename
48+
49+
const options = {
50+
nickname,
51+
notice: t('files_sharing', 'To upload files to {folder}, you need to provide your name first.', { folder }),
52+
subtitle: undefined as string | undefined,
53+
title: t('files_sharing', 'Upload files to {folder}', { folder }),
54+
}
55+
56+
// If the guest already has a nickname, we just make them double check
57+
if (nickname) {
58+
options.notice = t('files_sharing', 'Please confirm your name to upload files to {folder}', { folder })
59+
}
60+
61+
// If the account owner set their name as public,
62+
// we show it in the subtitle
63+
if (owner) {
64+
options.subtitle = t('files_sharing', '{ownerDisplayName} shared a folder with you.', { ownerDisplayName })
65+
}
66+
67+
// If this is a file request, then we need a nickname
68+
if (isFileRequest) {
69+
// If we don't have a nickname or the public auth prompt hasn't been shown yet, show it
70+
// We still show the prompt if the user has a nickname to double check
71+
if (!nickname || !dialogShown) {
72+
logger.debug('Showing public auth prompt.', { nickname })
73+
showGuestUserPrompt(options)
74+
}
75+
return
76+
}
77+
78+
if (!dialogShown && !nickname) {
79+
logger.debug('Public auth prompt not shown yet but nickname is not mandatory.', { nickname })
80+
return
81+
}
82+
83+
// Else, we just register the nickname header if any.
84+
logger.debug('Public auth prompt already shown.', { nickname })
85+
registerFileRequestHeader(nickname)
86+
})

apps/files_sharing/src/services/GuestNameValidity.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { t } from '@nextcloud/l10n'
1313
*/
1414
export function getGuestNameValidity(name: string, escape = false): string {
1515
if (name.trim() === '') {
16-
return t('files', 'Filename must not be empty.')
16+
return t('files', 'Names must not be empty.')
1717
}
1818

1919
if (name.startsWith('.')) {

apps/files_sharing/src/views/PublicAuthPrompt.vue

Lines changed: 0 additions & 138 deletions
This file was deleted.

apps/files_sharing/tests/Controller/ShareControllerTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ public function testShowShare(): void {
336336
'fileId' => 111,
337337
'owner' => 'ownerUID',
338338
'ownerDisplayName' => 'ownerDisplay',
339+
'isFileRequest' => false,
339340
];
340341

341342
$response = $this->shareController->showShare();
@@ -480,6 +481,7 @@ public function testShowFileDropShare(): void {
480481
'disclaimer' => 'My disclaimer text',
481482
'owner' => 'ownerUID',
482483
'ownerDisplayName' => 'ownerDisplay',
484+
'isFileRequest' => false,
483485
'note' => 'The note',
484486
'label' => 'A label',
485487
];

0 commit comments

Comments
 (0)