Skip to content

Commit a670dbb

Browse files
committed
feat: Use X-NC-Nickname as user identifier
Signed-off-by: Louis Chemineau <louis@chmn.me>
1 parent 16fa34d commit a670dbb

2 files changed

Lines changed: 37 additions & 45 deletions

File tree

lib/CurrentUser.php

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,22 @@
1010
use OCP\IRequest;
1111
use OCP\IUser;
1212
use OCP\IUserSession;
13+
use OCP\L10N\IFactory;
1314
use OCP\Share\Exceptions\ShareNotFound;
1415
use OCP\Share\IManager;
1516
use OCP\Share\IShare;
1617

1718
class CurrentUser {
1819

19-
/** @var string */
20-
protected $identifier;
21-
/** @var string|null */
22-
protected $cloudId;
23-
/** @var string|false|null */
24-
protected $sessionUser;
20+
protected string|null $identifier;
21+
protected string|null $cloudId;
22+
protected string|false|null $sessionUser;
2523

26-
/**
27-
* @param IUserSession $userSession
28-
* @param IRequest $request
29-
* @param IManager $shareManager
30-
*/
3124
public function __construct(
3225
protected IUserSession $userSession,
3326
protected IRequest $request,
3427
protected IManager $shareManager,
28+
protected IFactory $l10nFactory,
3529
) {
3630
$this->cloudId = false;
3731
$this->sessionUser = false;
@@ -46,19 +40,30 @@ public function getUser(): ?IUser {
4640
* @return string
4741
*/
4842
public function getUserIdentifier() {
49-
if ($this->identifier === null) {
50-
$this->identifier = $this->getUID();
43+
if ($this->identifier !== null) {
44+
return $this->identifier;
45+
}
46+
47+
$uid = $this->getUID();
48+
if ($uid !== null) {
49+
$this->identifier = $uid;
50+
return $this->identifier;
51+
}
5152

52-
if ($this->identifier === null) {
53-
$this->identifier = $this->getCloudIDFromToken();
53+
$cloudId = $this->getCloudIDFromToken();
54+
if ($cloudId !== null) {
55+
$this->identifier = $cloudId;
56+
return $this->identifier;
57+
}
5458

55-
if ($this->identifier === null) {
56-
// Nothing worked, fallback to empty string
57-
$this->identifier = '';
58-
}
59-
}
59+
$nickname = $this->request->getHeader('X-NC-Nickname');
60+
if ($nickname !== '') {
61+
$this->identifier = $nickname . ' (' . $this->l10nFactory->get('comments')->t('remote user') . ')';
62+
return $this->identifier;
6063
}
6164

65+
// Nothing worked, fallback to empty string
66+
$this->identifier = '';
6267
return $this->identifier;
6368
}
6469

tests/CurrentUserTest.php

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use OCP\IRequest;
2727
use OCP\IUser;
2828
use OCP\IUserSession;
29+
use OCP\L10N\IFactory;
2930
use OCP\Share\Exceptions\ShareNotFound;
3031
use OCP\Share\IManager;
3132
use OCP\Share\IShare;
@@ -42,33 +43,27 @@ abstract class RequestMock implements IRequest {
4243
* @package OCA\Activity\Tests
4344
*/
4445
class CurrentUserTest extends TestCase {
45-
/** @var IRequest|MockObject */
46-
protected $request;
47-
48-
/** @var IUserSession|MockObject */
49-
protected $userSession;
50-
51-
/** @var IManager|MockObject */
52-
protected $shareManager;
46+
protected IRequest&MockObject $request;
47+
protected IUserSession&MockObject $userSession;
48+
protected IManager&MockObject $shareManager;
49+
protected IFactory&MockObject $l10nFactory;
5350

5451
protected function setUp(): void {
5552
parent::setUp();
5653

5754
$this->request = $this->createMock(RequestMock::class);
5855
$this->userSession = $this->createMock(IUserSession::class);
5956
$this->shareManager = $this->createMock(IManager::class);
57+
$this->l10nFactory = $this->createMock(IFactory::class);
6058
}
6159

62-
/**
63-
* @param array $methods
64-
* @return CurrentUser|MockObject
65-
*/
66-
protected function getInstance(array $methods = []): CurrentUser {
60+
protected function getInstance(array $methods = []): CurrentUser&MockObject {
6761
if (empty($methods)) {
6862
return new CurrentUser(
6963
$this->userSession,
7064
$this->request,
71-
$this->shareManager
65+
$this->shareManager,
66+
$this->l10nFactory,
7267
);
7368
}
7469

@@ -77,6 +72,7 @@ protected function getInstance(array $methods = []): CurrentUser {
7772
$this->userSession,
7873
$this->request,
7974
$this->shareManager,
75+
$this->l10nFactory,
8076
])
8177
->onlyMethods($methods)
8278
->getMock();
@@ -118,11 +114,7 @@ public function testGetUserIdentifier(?string $cachedIdentifier, $uidResult, $to
118114
$this->assertSame($expected, $instance->getUserIdentifier());
119115
}
120116

121-
/**
122-
* @param string $uid
123-
* @return IUser
124-
*/
125-
protected function getUserMock(string $uid): IUser {
117+
protected function getUserMock(string $uid): IUser&MockObject {
126118
$user = $this->createMock(IUser::class);
127119
$user->expects($this->once())
128120
->method('getUID')
@@ -154,12 +146,7 @@ public function testGetUID(?IUser $user, ?string $expected): void {
154146
$this->assertSame($expected, $instance->getUID());
155147
}
156148

157-
/**
158-
* @param int $type
159-
* @param string $shareWith
160-
* @return IShare
161-
*/
162-
protected function getShareMock(int $type, ?string $shareWith): IShare {
149+
protected function getShareMock(int $type, ?string $shareWith): IShare&MockObject {
163150
$share = $this->createMock(IShare::class);
164151
$share->expects($this->once())
165152
->method('getShareType')

0 commit comments

Comments
 (0)