diff --git a/lib/Listener/ShareLinkListener.php b/lib/Listener/ShareLinkListener.php index 4f07ae4084..facdf5593f 100644 --- a/lib/Listener/ShareLinkListener.php +++ b/lib/Listener/ShareLinkListener.php @@ -56,9 +56,12 @@ public function handle(Event $event): void { /** @var IShare $share */ $share = $event->getShare(); $owner = $share->getShareOwner(); + $loggedInUser = $this->permissionManager->loggedInUser(); if ($this->permissionManager->isEnabledForUser($owner)) { + $this->initialStateService->prepareParams(['userId' => $loggedInUser]); $this->initialStateService->provideCapabilities(); + Util::addScript('richdocuments', 'richdocuments-files'); Util::addScript('richdocuments', 'richdocuments-viewer', 'viewer'); Util::addScript('richdocuments', 'richdocuments-public', 'viewer'); diff --git a/lib/PermissionManager.php b/lib/PermissionManager.php index e503dab82e..e40fbe44f6 100644 --- a/lib/PermissionManager.php +++ b/lib/PermissionManager.php @@ -58,20 +58,7 @@ public function __construct( } private function userMatchesGroupList(?string $userId = null, ?array $groupList = []): bool { - if ($userId === null) { - // Share links set the incognito mode so in order to still get the - // user information we need to temporarily switch it off to get the current user - $incognito = false; - if (\OC_User::isIncognitoMode()) { - \OC_User::setIncognitoMode(false); - $incognito = true; - } - $user = $this->userSession->getUser(); - $userId = $user ? $user->getUID() : null; - if ($incognito) { - \OC_User::setIncognitoMode(true); - } - } + $userId = $userId ?? $this->loggedInUser(); if ($userId === null) { // Access for public users will be checked separately based on the share owner @@ -98,6 +85,23 @@ private function userMatchesGroupList(?string $userId = null, ?array $groupList return false; } + public function loggedInUser(): ?string { + $incognito = \OC_User::isIncognitoMode(); + + if ($incognito) { + \OC_User::setIncognitoMode(false); + } + + $user = $this->userSession->getUser(); + $userId = $user?->getUID(); + + if (!$incognito) { + \OC_User::setIncognitoMode(true); + } + + return $userId; + } + public function isEnabledForUser(?string $userId = null): bool { if ($this->userMatchesGroupList($userId, $this->appConfig->getUseGroups())) { return true; diff --git a/lib/Service/InitialStateService.php b/lib/Service/InitialStateService.php index 6726a09dcf..e9d2a78b7c 100644 --- a/lib/Service/InitialStateService.php +++ b/lib/Service/InitialStateService.php @@ -94,14 +94,18 @@ public function prepareParams(array $params): array { } private function provideOptions(): void { + $this->initialState->provideInitialState('loggedInUser', $this->userId ?? false); + $this->initialState->provideInitialState('theme', $this->config->getAppValue(Application::APPNAME, 'theme', 'nextcloud')); $this->initialState->provideInitialState('uiDefaults', [ 'UIMode' => $this->config->getAppValue(Application::APPNAME, 'uiDefaults-UIMode', 'notebookbar') ]); + $logoSet = $this->config->getAppValue('theming', 'logoheaderMime', '') !== ''; if (!$logoSet) { $logoSet = $this->config->getAppValue('theming', 'logoMime', '') !== ''; } + $this->initialState->provideInitialState('theming-customLogo', ($logoSet ? $this->urlGenerator->getAbsoluteURL($this->themingDefaults->getLogo()) : false)); diff --git a/src/helpers/getLoggedInUser.js b/src/helpers/getLoggedInUser.js index 159ac65b48..27b97a0c12 100644 --- a/src/helpers/getLoggedInUser.js +++ b/src/helpers/getLoggedInUser.js @@ -1,30 +1,34 @@ -import axios from '@nextcloud/axios' -import { generateUrl } from '@nextcloud/router' - /** - * Gets the current user's display name if logged in. + * @copyright Copyright (c) 2024 Elizabeth Danzberger + * + * @author Elizabeth Danzberger + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . * - * @return null | string */ -async function getLoggedInUser() { - try { - const res = await axios.get(generateUrl('apps/files/public')) - if (res.status !== 200) { - return null - } +import { loadState } from '@nextcloud/initial-state' - const el = document.createElement('html') - el.innerHTML = res.data - - const userAttributes = el.querySelector('head[data-user]').attributes - - return userAttributes.getNamedItem('data-user-displayname').textContent - } catch (err) { - if (err.status === 401) { - console.error(err.statusText) - } - } +/** + * Gets the current user's display name if logged in. + * + * @return boolean | string + */ +function getLoggedInUser() { + return loadState('richdocuments', 'loggedInUser') } export default getLoggedInUser diff --git a/src/helpers/guestName.js b/src/helpers/guestName.js index 53d24a86be..5e5f0b353d 100644 --- a/src/helpers/guestName.js +++ b/src/helpers/guestName.js @@ -38,11 +38,11 @@ const setGuestNameCookie = (username) => { } const shouldAskForGuestName = async () => { - const loggedInUser = Boolean(await getLoggedInUser()) + const noLoggedInUser = !getLoggedInUser() const noGuestCookie = !cookieAlreadySet('guestUser') const noCurrentUser = !getCurrentUser() || getCurrentUser()?.uid === '' - return !loggedInUser && noGuestCookie && noCurrentUser + return noLoggedInUser && noGuestCookie && noCurrentUser } export {