Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,12 @@ public function renderPage(IShare $share, string $token, string $path): Template
Util::addInitScript(Application::APP_ID, 'init');
Util::addInitScript(Application::APP_ID, 'init-public');
Util::addScript('files', 'main');
Util::addScript(Application::APP_ID, 'public-nickname-handler');

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

// Load Viewer scripts
if (class_exists(LoadViewer::class)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use OCA\Files_Sharing\AppInfo\Application;
use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Services\IInitialState;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\Share\IManager;
Expand All @@ -19,6 +20,7 @@
class LoadPublicFileRequestAuthListener implements IEventListener {
public function __construct(
private IManager $shareManager,
private IInitialState $initialState,
) {
}

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

if ($isFileRequest) {
// Add the script to the public page
Util::addScript(Application::APP_ID, 'public-file-request');
}
Util::addScript(Application::APP_ID, 'public-nickname-handler');

// Add file-request script if needed
$this->initialState->provideInitialState('isFileRequest', $isFileRequest);
}
}
57 changes: 0 additions & 57 deletions apps/files_sharing/src/public-file-request.ts

This file was deleted.

86 changes: 86 additions & 0 deletions apps/files_sharing/src/public-nickname-handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import { getBuilder } from '@nextcloud/browser-storage'
import { getGuestNickname, type NextcloudUser } from '@nextcloud/auth'
import { getUploader } from '@nextcloud/upload'
import { loadState } from '@nextcloud/initial-state'
import { showGuestUserPrompt } from '@nextcloud/dialogs'
import { t } from '@nextcloud/l10n'

import logger from './services/logger'
import { subscribe } from '@nextcloud/event-bus'

const storage = getBuilder('files_sharing').build()

// Setup file-request nickname header for the uploader
const registerFileRequestHeader = (nickname: string) => {
const uploader = getUploader()
uploader.setCustomHeader('X-NC-Nickname', encodeURIComponent(nickname))
logger.debug('Nickname header registered for uploader', { headers: uploader.customHeaders })
}

// Callback when a nickname was chosen
const onUserInfoChanged = (guest: NextcloudUser) => {
logger.debug('User info changed', { guest })
registerFileRequestHeader(guest.displayName ?? '')
}

// Monitor nickname changes
subscribe('user:info:changed', onUserInfoChanged)

window.addEventListener('DOMContentLoaded', () => {
const nickname = getGuestNickname() ?? ''
const dialogShown = storage.getItem('public-auth-prompt-shown') !== null

// Check if a nickname is mandatory
const isFileRequest = loadState('files_sharing', 'isFileRequest', false)

const owner = loadState('files_sharing', 'owner', '')
const ownerDisplayName = loadState('files_sharing', 'ownerDisplayName', '')
const label = loadState('files_sharing', 'label', '')
const filename = loadState('files_sharing', 'filename', '')

// If the owner provided a custom label, use it instead of the filename
const folder = label || filename

const options = {
nickname,
notice: t('files_sharing', 'To upload files to {folder}, you need to provide your name first.', { folder }),
subtitle: undefined as string | undefined,
title: t('files_sharing', 'Upload files to {folder}', { folder }),
}

// If the guest already has a nickname, we just make them double check
if (nickname) {
options.notice = t('files_sharing', 'Please confirm your name to upload files to {folder}', { folder })
}

// If the account owner set their name as public,
// we show it in the subtitle
if (owner) {
options.subtitle = t('files_sharing', '{ownerDisplayName} shared a folder with you.', { ownerDisplayName })
}

// If this is a file request, then we need a nickname
if (isFileRequest) {
// If we don't have a nickname or the public auth prompt hasn't been shown yet, show it
// We still show the prompt if the user has a nickname to double check
if (!nickname || !dialogShown) {
logger.debug('Showing public auth prompt.', { nickname })
showGuestUserPrompt(options)
}
return
}

if (!dialogShown && !nickname) {
logger.debug('Public auth prompt not shown yet but nickname is not mandatory.', { nickname })
return
}

// Else, we just register the nickname header if any.
logger.debug('Public auth prompt already shown.', { nickname })
registerFileRequestHeader(nickname)
})
2 changes: 1 addition & 1 deletion apps/files_sharing/src/services/GuestNameValidity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { t } from '@nextcloud/l10n'
*/
export function getGuestNameValidity(name: string, escape = false): string {
if (name.trim() === '') {
return t('files', 'Filename must not be empty.')
return t('files', 'Names must not be empty.')
}

if (name.startsWith('.')) {
Expand Down
138 changes: 0 additions & 138 deletions apps/files_sharing/src/views/PublicAuthPrompt.vue

This file was deleted.

2 changes: 2 additions & 0 deletions apps/files_sharing/tests/Controller/ShareControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ public function testShowShare(): void {
'fileId' => 111,
'owner' => 'ownerUID',
'ownerDisplayName' => 'ownerDisplay',
'isFileRequest' => false,
];

$response = $this->shareController->showShare();
Expand Down Expand Up @@ -480,6 +481,7 @@ public function testShowFileDropShare(): void {
'disclaimer' => 'My disclaimer text',
'owner' => 'ownerUID',
'ownerDisplayName' => 'ownerDisplay',
'isFileRequest' => false,
'note' => 'The note',
'label' => 'A label',
];
Expand Down
Loading
Loading