Skip to content

Commit

Permalink
refactor: get already logged in user via initial state
Browse files Browse the repository at this point in the history
Signed-off-by: Elizabeth Danzberger <[email protected]>
  • Loading branch information
elzody committed Apr 26, 2024
1 parent a3ee201 commit f6ec7bd
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 38 deletions.
3 changes: 3 additions & 0 deletions lib/Listener/ShareLinkListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
32 changes: 18 additions & 14 deletions lib/PermissionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions lib/Service/InitialStateService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
48 changes: 26 additions & 22 deletions src/helpers/getLoggedInUser.js
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
*
* @author Elizabeth Danzberger <[email protected]>
*
* @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 <http://www.gnu.org/licenses/>.
*
* @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
4 changes: 2 additions & 2 deletions src/helpers/guestName.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit f6ec7bd

Please sign in to comment.