Skip to content

Commit d9f7715

Browse files
authored
Merge pull request #1927 from nextcloud/artonge/feat/use_nickname_in_activity
feat: Use X-NC-Nickname as user identifier
2 parents 9b4c2fd + faf2572 commit d9f7715

2 files changed

Lines changed: 36 additions & 30 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 = htmlspecialchars($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: 10 additions & 10 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,31 @@ 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

6260
/**
6361
* @param array $methods
64-
* @return CurrentUser|MockObject
62+
* @return CurrentUser&MockObject
6563
*/
6664
protected function getInstance(array $methods = []): CurrentUser {
6765
if (empty($methods)) {
6866
return new CurrentUser(
6967
$this->userSession,
7068
$this->request,
71-
$this->shareManager
69+
$this->shareManager,
70+
$this->l10nFactory,
7271
);
7372
}
7473

@@ -77,6 +76,7 @@ protected function getInstance(array $methods = []): CurrentUser {
7776
$this->userSession,
7877
$this->request,
7978
$this->shareManager,
79+
$this->l10nFactory,
8080
])
8181
->onlyMethods($methods)
8282
->getMock();

0 commit comments

Comments
 (0)