Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
remove cached display name on user delete
Signed-off-by: Robin Appelman <[email protected]>
  • Loading branch information
icewind1991 committed Sep 1, 2022
commit ee1a5ec4f66c1e2ca50d619e9e3c146df8cc4bc3
1 change: 0 additions & 1 deletion apps/files_sharing/lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ function () use ($c) {
$context->registerCapability(Capabilities::class);

$context->registerNotifierService(Notifier::class);
$context->registerEventListener(UserChangedEvent::class, DisplayNameCache::class);
}

public function boot(IBootContext $context): void {
Expand Down
6 changes: 6 additions & 0 deletions lib/private/User/DisplayNameCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use OCP\ICacheFactory;
use OCP\IUserManager;
use OCP\User\Events\UserChangedEvent;
use OCP\User\Events\UserDeletedEvent;

/**
* Class that cache the relation UserId -> Display name
Expand Down Expand Up @@ -81,5 +82,10 @@ public function handle(Event $event): void {
$this->cache[$userId] = $newDisplayName;
$this->memCache->set($userId, $newDisplayName, 60 * 10); // 10 minutes
}
if ($event instanceof UserDeletedEvent) {
$userId = $event->getUser()->getUID();
unset($this->cache[$userId]);
$this->memCache->remove($userId);
}
}
}
6 changes: 5 additions & 1 deletion lib/private/User/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@
use OCP\User\Backend\ICheckPasswordBackend;
use OCP\User\Backend\ICountUsersBackend;
use OCP\User\Events\BeforeUserCreatedEvent;
use OCP\User\Events\UserChangedEvent;
use OCP\User\Events\UserCreatedEvent;
use OCP\User\Events\UserDeletedEvent;
use OCP\UserInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

Expand Down Expand Up @@ -111,6 +113,8 @@ public function __construct(IConfig $config,
});
$this->eventDispatcher = $eventDispatcher;
$this->displayNameCache = new DisplayNameCache($cacheFactory, $this);
$this->eventDispatcher->addListener(UserChangedEvent::class, [$this->displayNameCache, 'handle']);
$this->eventDispatcher->addListener(UserDeletedEvent::class, [$this->displayNameCache, 'handle']);
}

/**
Expand Down Expand Up @@ -209,7 +213,7 @@ protected function getUserObject($uid, $backend, $cacheUser = true) {
return $this->cachedUsers[$uid];
}

$user = new User($uid, $backend, $this->dispatcher, $this, $this->config);
$user = new User($uid, $backend, $this->dispatcher, $this, $this->config, null, $this->eventDispatcher);
if ($cacheUser) {
$this->cachedUsers[$uid] = $user;
}
Expand Down
13 changes: 10 additions & 3 deletions lib/private/User/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,15 @@ class User implements IUser {
/** @var IURLGenerator */
private $urlGenerator;

public function __construct(string $uid, ?UserInterface $backend, EventDispatcherInterface $dispatcher, $emitter = null, IConfig $config = null, $urlGenerator = null) {
public function __construct(
string $uid,
?UserInterface $backend,
EventDispatcherInterface $dispatcher,
$emitter = null,
IConfig $config = null,
$urlGenerator = null,
IEventDispatcher $eventDispatcher = null
) {
$this->uid = $uid;
$this->backend = $backend;
$this->legacyDispatcher = $dispatcher;
Expand All @@ -111,8 +119,7 @@ public function __construct(string $uid, ?UserInterface $backend, EventDispatche
if (is_null($this->urlGenerator)) {
$this->urlGenerator = \OC::$server->getURLGenerator();
}
// TODO: inject
$this->dispatcher = \OC::$server->query(IEventDispatcher::class);
$this->dispatcher = $eventDispatcher ?: \OC::$server->query(IEventDispatcher::class);
}

/**
Expand Down
9 changes: 8 additions & 1 deletion tests/lib/User/ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@
namespace Test\User;

use OC\AllConfig;
use OC\EventDispatcher\EventDispatcher;
use OC\User\Database;
use OC\User\Manager;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\ICache;
use OCP\ICacheFactory;
use OCP\IConfig;
use OCP\IServerContainer;
use OCP\IUser;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Test\TestCase;

Expand Down Expand Up @@ -45,7 +48,11 @@ protected function setUp(): void {

$this->config = $this->createMock(IConfig::class);
$this->oldDispatcher = $this->createMock(EventDispatcherInterface::class);
$this->eventDispatcher = $this->createMock(IEventDispatcher::class);
$this->eventDispatcher = new EventDispatcher(
new \Symfony\Component\EventDispatcher\EventDispatcher(),
$this->createMock(IServerContainer::class),
$this->createMock(LoggerInterface::class),
);
$this->cacheFactory = $this->createMock(ICacheFactory::class);
$this->cache = $this->createMock(ICache::class);

Expand Down