Skip to content
Open
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
50 changes: 13 additions & 37 deletions lib/CurrentUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,7 @@

class CurrentUser {

/** @var string|null */
protected $identifier = null;
/** @var string|false|null */
protected $cloudId = false;
/** @var string|false|null */
protected $sessionUser = false;

public function __construct(

Check failure on line 20 in lib/CurrentUser.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis

MissingPureAnnotation

lib/CurrentUser.php:20:18: MissingPureAnnotation: __construct must be marked @psalm-mutation-free to aid security analysis, run with --alter --issues=MissingPureAnnotation to fix this (see https://psalm.dev/363)
protected IUserSession $userSession,
protected IRequest $request,
protected IManager $shareManager,
Expand All @@ -41,65 +34,48 @@
* @return string
*/
public function getUserIdentifier() {
if ($this->identifier !== null) {
return $this->identifier;
}

$uid = $this->getUID();
if ($uid !== null) {
$this->identifier = $uid;
return $this->identifier;
return $uid;
}

$cloudId = $this->getCloudIDFromToken();
if ($cloudId !== null) {
$this->identifier = $cloudId;
return $this->identifier;
return $cloudId;
}

$nickname = htmlspecialchars($this->request->getHeader('X-NC-Nickname'));
if ($nickname !== '') {
$this->identifier = $nickname . ' (' . $this->l10nFactory->get('comments')->t('remote user') . ')';
return $this->identifier;
return $nickname . ' (' . $this->l10nFactory->get('comments')->t('remote user') . ')';
}

// Nothing worked, fallback to empty string
$this->identifier = '';
return $this->identifier;
return '';
}

/**
* Get the current user id from the session
* @return string|null
*/
public function getUID() {
if ($this->sessionUser === false) {
$user = $this->userSession->getUser();
if ($user instanceof IUser) {
$this->sessionUser = $user->getUID();
} else {
$this->sessionUser = null;
}
$user = $this->userSession->getUser();
if ($user instanceof IUser) {
return (string)$user->getUID();

Check failure on line 63 in lib/CurrentUser.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis

RedundantCast

lib/CurrentUser.php:63:11: RedundantCast: Redundant cast to string (see https://psalm.dev/262)
}

return $this->sessionUser;
return null;
}

/**
* Get the current user cloud id from the session
* @return string|null
*/
public function getCloudId() {
if ($this->cloudId === false) {
$user = $this->userSession->getUser();
if ($user instanceof IUser) {
$this->cloudId = (string)$user->getCloudId();
} else {
$this->cloudId = $this->getCloudIDFromToken();
}
$user = $this->userSession->getUser();
if ($user instanceof IUser) {
return (string)$user->getCloudId();
} else {
return $this->getCloudIDFromToken();
}

return $this->cloudId;
}

/**
Expand Down
3 changes: 0 additions & 3 deletions tests/CurrentUserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ public static function dataGetUserIdentifier(): array {
[null, null, null, ''],
[null, 'uid', -1, 'uid'],
[null, null, 'token', 'token'],
['cached', -1, -1, 'cached'],
];
}

Expand All @@ -96,8 +95,6 @@ public function testGetUserIdentifier(?string $cachedIdentifier, string|int|null
'getCloudIDFromToken',
]);

self::invokePrivate($instance, 'identifier', [$cachedIdentifier]);

$instance->expects($uidResult !== -1 ? $this->once() : $this->never())
->method('getUID')
->willReturn($uidResult);
Expand Down
Loading