Skip to content

Commit f4e79c9

Browse files
come-ncbackportbot[bot]
authored andcommitted
fix: Fix user in Tags class, do not depend upon session
fix: Fix user in Tags class, do not depend upon session Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com> [skip ci]
1 parent 1564846 commit f4e79c9

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

lib/private/TagManager.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use OCP\IDBConnection;
1818
use OCP\ITagManager;
1919
use OCP\ITags;
20+
use OCP\IUserManager;
2021
use OCP\IUserSession;
2122
use OCP\User\Events\UserDeletedEvent;
2223
use Psr\Log\LoggerInterface;
@@ -29,6 +30,7 @@ class TagManager implements ITagManager, IEventListener {
2930
public function __construct(
3031
private TagMapper $mapper,
3132
private IUserSession $userSession,
33+
private IUserManager $userManager,
3234
private IDBConnection $connection,
3335
private LoggerInterface $logger,
3436
private IEventDispatcher $dispatcher,
@@ -59,7 +61,7 @@ public function load($type, $defaultTags = [], $includeShared = false, $userId =
5961
$userId = $this->userSession->getUser()->getUId();
6062
}
6163
$userFolder = $this->rootFolder->getUserFolder($userId);
62-
return new Tags($this->mapper, $userId, $type, $this->logger, $this->connection, $this->dispatcher, $this->userSession, $userFolder, $defaultTags);
64+
return new Tags($this->mapper, $userId, $type, $this->logger, $this->connection, $this->dispatcher, $this->userManager, $userFolder, $defaultTags);
6365
}
6466

6567
/**

lib/private/Tags.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function __construct(
6565
private LoggerInterface $logger,
6666
private IDBConnection $db,
6767
private IEventDispatcher $dispatcher,
68-
private IUserSession $userSession,
68+
private IUserManager $userManager,
6969
private Folder $userFolder,
7070
array $defaultTags = [],
7171
) {
@@ -538,7 +538,7 @@ public function tagAs($objid, $tag, ?string $path = null) {
538538
}
539539
}
540540

541-
$this->dispatcher->dispatchTyped(new NodeAddedToFavorite($this->userSession->getUser(), $objid, $path));
541+
$this->dispatcher->dispatchTyped(new NodeAddedToFavorite($this->userManager->getExistingUser($this->user), $objid, $path));
542542
}
543543
return true;
544544
}
@@ -583,7 +583,7 @@ public function unTag($objid, $tag, ?string $path = null) {
583583
}
584584
}
585585

586-
$this->dispatcher->dispatchTyped(new NodeRemovedFromFavorite($this->userSession->getUser(), $objid, $path));
586+
$this->dispatcher->dispatchTyped(new NodeRemovedFromFavorite($this->userManager->getExistingUser($this->user), $objid, $path));
587587
}
588588
return true;
589589
}

tests/lib/TagsTest.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
use OCP\Files\IRootFolder;
1616
use OCP\Files\Node;
1717
use OCP\IDBConnection;
18-
use OCP\ITagManager;
1918
use OCP\IUser;
2019
use OCP\IUserManager;
2120
use OCP\IUserSession;
2221
use OCP\Server;
22+
use PHPUnit\Framework\MockObject\MockObject;
2323
use Psr\Log\LoggerInterface;
2424

2525
/**
@@ -50,6 +50,11 @@ protected function setUp(): void {
5050
$this->user = $this->createMock(IUser::class);
5151
$this->user->method('getUID')
5252
->willReturn($userId);
53+
$this->userManager = $this->createMock(IUserManager::class);
54+
$this->userManager
55+
->expects($this->any())
56+
->method('getExistingUser')
57+
->willReturn($this->user);
5358
$this->userSession = $this->createMock(IUserSession::class);
5459
$this->userSession
5560
->expects($this->any())
@@ -70,7 +75,15 @@ protected function setUp(): void {
7075

7176
$this->objectType = $this->getUniqueID('type_');
7277
$this->tagMapper = new TagMapper(Server::get(IDBConnection::class));
73-
$this->tagMgr = new TagManager($this->tagMapper, $this->userSession, Server::get(IDBConnection::class), Server::get(LoggerInterface::class), Server::get(IEventDispatcher::class), $this->rootFolder);
78+
$this->tagMgr = new TagManager(
79+
$this->tagMapper,
80+
$this->userSession,
81+
$this->userManager,
82+
Server::get(IDBConnection::class),
83+
Server::get(LoggerInterface::class),
84+
Server::get(IEventDispatcher::class),
85+
$this->rootFolder
86+
);
7487
}
7588

7689
protected function tearDown(): void {
@@ -87,7 +100,15 @@ public function testTagManagerWithoutUserReturnsNull(): void {
87100
->expects($this->any())
88101
->method('getUser')
89102
->willReturn(null);
90-
$this->tagMgr = new TagManager($this->tagMapper, $this->userSession, Server::get(IDBConnection::class), Server::get(LoggerInterface::class), Server::get(IEventDispatcher::class), $this->rootFolder);
103+
$this->tagMgr = new TagManager(
104+
$this->tagMapper,
105+
$this->userSession,
106+
$this->userManager,
107+
Server::get(IDBConnection::class),
108+
Server::get(LoggerInterface::class),
109+
Server::get(IEventDispatcher::class),
110+
$this->rootFolder
111+
);
91112
$this->assertNull($this->tagMgr->load($this->objectType));
92113
}
93114

0 commit comments

Comments
 (0)