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
46 changes: 26 additions & 20 deletions lib/CurrentUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,26 @@
use OCP\IRequest;
use OCP\IUser;
use OCP\IUserSession;
use OCP\L10N\IFactory;
use OCP\Share\Exceptions\ShareNotFound;
use OCP\Share\IManager;
use OCP\Share\IShare;

class CurrentUser {

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

/**
* @param IUserSession $userSession
* @param IRequest $request
* @param IManager $shareManager
*/
public function __construct(
protected IUserSession $userSession,
protected IRequest $request,
protected IManager $shareManager,
protected IFactory $l10nFactory,
) {
$this->cloudId = false;
$this->sessionUser = false;
}

public function getUser(): ?IUser {
Expand All @@ -46,19 +41,30 @@ public function getUser(): ?IUser {
* @return string
*/
public function getUserIdentifier() {
if ($this->identifier === null) {
$this->identifier = $this->getUID();
if ($this->identifier !== null) {
return $this->identifier;
}

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

if ($this->identifier === null) {
// Nothing worked, fallback to empty string
$this->identifier = '';
}
}
$cloudId = $this->getCloudIDFromToken();
if ($cloudId !== null) {
$this->identifier = $cloudId;
return $this->identifier;
}

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

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

Expand Down
20 changes: 10 additions & 10 deletions tests/CurrentUserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use OCP\IRequest;
use OCP\IUser;
use OCP\IUserSession;
use OCP\L10N\IFactory;
use OCP\Share\Exceptions\ShareNotFound;
use OCP\Share\IManager;
use OCP\Share\IShare;
Expand All @@ -42,33 +43,31 @@ abstract class RequestMock implements IRequest {
* @package OCA\Activity\Tests
*/
class CurrentUserTest extends TestCase {
/** @var IRequest|MockObject */
protected $request;

/** @var IUserSession|MockObject */
protected $userSession;

/** @var IManager|MockObject */
protected $shareManager;
protected IRequest&MockObject $request;
protected IUserSession&MockObject $userSession;
protected IManager&MockObject $shareManager;
protected IFactory&MockObject $l10nFactory;

protected function setUp(): void {
parent::setUp();

$this->request = $this->createMock(RequestMock::class);
$this->userSession = $this->createMock(IUserSession::class);
$this->shareManager = $this->createMock(IManager::class);
$this->l10nFactory = $this->createMock(IFactory::class);
}

/**
* @param array $methods
* @return CurrentUser|MockObject
* @return CurrentUser&MockObject
*/
protected function getInstance(array $methods = []): CurrentUser {
if (empty($methods)) {
return new CurrentUser(
$this->userSession,
$this->request,
$this->shareManager
$this->shareManager,
$this->l10nFactory,
);
}

Expand All @@ -77,6 +76,7 @@ protected function getInstance(array $methods = []): CurrentUser {
$this->userSession,
$this->request,
$this->shareManager,
$this->l10nFactory,
])
->onlyMethods($methods)
->getMock();
Expand Down
Loading