Skip to content

Commit 8efbcca

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

2 files changed

Lines changed: 34 additions & 29 deletions

File tree

lib/CurrentUser.php

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,26 @@
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;
2120
/** @var string|null */
22-
protected $cloudId;
21+
protected $identifier = null;
2322
/** @var string|false|null */
24-
protected $sessionUser;
23+
protected $cloudId = false;
24+
/** @var string|false|null */
25+
protected $sessionUser = false;
2526

26-
/**
27-
* @param IUserSession $userSession
28-
* @param IRequest $request
29-
* @param IManager $shareManager
30-
*/
3127
public function __construct(
3228
protected IUserSession $userSession,
3329
protected IRequest $request,
3430
protected IManager $shareManager,
31+
protected IFactory $l10nFactory,
3532
) {
36-
$this->cloudId = false;
37-
$this->sessionUser = false;
3833
}
3934

4035
public function getUser(): ?IUser {
@@ -46,19 +41,30 @@ public function getUser(): ?IUser {
4641
* @return string
4742
*/
4843
public function getUserIdentifier() {
49-
if ($this->identifier === null) {
50-
$this->identifier = $this->getUID();
44+
if ($this->identifier !== null) {
45+
return $this->identifier;
46+
}
5147

52-
if ($this->identifier === null) {
53-
$this->identifier = $this->getCloudIDFromToken();
48+
$uid = $this->getUID();
49+
if ($uid !== null) {
50+
$this->identifier = $uid;
51+
return $this->identifier;
52+
}
5453

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

66+
// Nothing worked, fallback to empty string
67+
$this->identifier = '';
6268
return $this->identifier;
6369
}
6470

tests/CurrentUserTest.php

Lines changed: 8 additions & 9 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,14 +43,10 @@ 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();
@@ -68,7 +65,8 @@ protected function getInstance(array $methods = []): CurrentUser {
6865
return new CurrentUser(
6966
$this->userSession,
7067
$this->request,
71-
$this->shareManager
68+
$this->shareManager,
69+
$this->l10nFactory,
7270
);
7371
}
7472

@@ -77,6 +75,7 @@ protected function getInstance(array $methods = []): CurrentUser {
7775
$this->userSession,
7876
$this->request,
7977
$this->shareManager,
78+
$this->l10nFactory,
8079
])
8180
->onlyMethods($methods)
8281
->getMock();

0 commit comments

Comments
 (0)