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

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(
protected readonly IUserSession $userSession,
protected readonly IRequest $request,
Expand All @@ -40,63 +33,46 @@ public function getUser(): ?IUser {
* Get an identifier for the user, session or token
*/
public function getUserIdentifier(): string {
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
*/
public function getUID(): ?string {
if ($this->sessionUser === false) {
$user = $this->userSession->getUser();
if ($user instanceof IUser) {
$this->sessionUser = (string)$user->getUID();
} else {
$this->sessionUser = null;
}
$user = $this->userSession->getUser();
if ($user instanceof IUser) {
return $user->getUID();
}

return $this->sessionUser;
return null;
}

/**
* Get the current user cloud id from the session
*/
public function getCloudId(): ?string {
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 $user->getCloudId();
} else {
return $this->getCloudIDFromToken();
}

return $this->cloudId;
}

/**
Expand Down
11 changes: 4 additions & 7 deletions tests/CurrentUserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,22 +84,19 @@ protected function getInstance(array $methods = []): CurrentUser|MockObject {

public static function dataGetUserIdentifier(): array {
return [
[null, null, null, ''],
[null, 'uid', '-1', 'uid'],
[null, null, 'token', 'token'],
['cached', -1, -1, 'cached'],
[null, null, ''],
['uid', '-1', 'uid'],
[null, 'token', 'token'],
];
}

#[DataProvider('dataGetUserIdentifier')]
public function testGetUserIdentifier(?string $cachedIdentifier, string|int|null $uidResult, ?string $tokenResult, string $expected): void {
public function testGetUserIdentifier(string|int|null $uidResult, ?string $tokenResult, string $expected): void {
$instance = $this->getInstance([
'getUID',
'getCloudIDFromToken',
]);

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

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