--- /dev/null
+<?php
+
+declare(strict_types=1);
+
+/**
+ * @copyright 2022 Anna Larch <anna.larch@gmx.net>
+ * @author Anna Larch <anna.larch@gmx.net>
+ *
+ * @license AGPL-3.0-or-later
+ *
+ * This code is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License, version 3,
+ * along with this program. If not, see <http://www.gnu.org/licenses/>
+ *
+ */
+
+
+namespace OC\Group;
+
+use OCP\Cache\CappedMemoryCache;
+use OCP\EventDispatcher\Event;
+use OCP\EventDispatcher\IEventListener;
+use OCP\Group\Events\GroupChangedEvent;
+use OCP\ICache;
+use OCP\ICacheFactory;
+use OCP\IGroupManager;
+
+/**
+ * Class that cache the relation Group ID -> Display name
+ *
+ * This saves fetching the group from the backend for "just" the display name
+ */
+class DisplayNameCache implements IEventListener {
+ private CappedMemoryCache $cache;
+ private ICache $memCache;
+ private IGroupManager $groupManager;
+
+ public function __construct(ICacheFactory $cacheFactory, IGroupManager $groupManager) {
+ $this->cache = new CappedMemoryCache();
+ $this->memCache = $cacheFactory->createDistributed('groupDisplayNameMappingCache');
+ $this->groupManager = $groupManager;
+ }
+
+ public function getDisplayName(string $groupId): ?string {
+ if (isset($this->cache[$groupId])) {
+ return $this->cache[$groupId];
+ }
+ $displayName = $this->memCache->get($groupId);
+ if ($displayName) {
+ $this->cache[$groupId] = $displayName;
+ return $displayName;
+ }
+
+ $group = $this->groupManager->get($groupId);
+ if ($group) {
+ $displayName = $group->getDisplayName();
+ } else {
+ $displayName = null;
+ }
+ $this->cache[$groupId] = $displayName;
+ $this->memCache->set($groupId, $displayName, 60 * 10); // 10 minutes
+
+ return $displayName;
+ }
+
+ public function clear(): void {
+ $this->cache = new CappedMemoryCache();
+ $this->memCache->clear();
+ }
+
+ public function handle(Event $event): void {
+ if ($event instanceof GroupChangedEvent && $event->getFeature() === 'displayName') {
+ $groupId = $event->getGroup()->getGID();
+ $newDisplayName = $event->getValue();
+ $this->cache[$groupId] = $newDisplayName;
+ $this->memCache->set($groupId, $newDisplayName, 60 * 10); // 10 minutes
+ }
+ }
+}
use OC\Group\Database;
use OC\User\Manager;
use OCP\GroupInterface;
+use OCP\ICacheFactory;
use OCP\IUser;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
protected $dispatcher;
/** @var LoggerInterface|MockObject */
protected $logger;
+ /** @var ICacheFactory|MockObject */
+ private $cache;
protected function setUp(): void {
parent::setUp();
$this->userManager = $this->createMock(Manager::class);
$this->dispatcher = $this->createMock(EventDispatcherInterface::class);
$this->logger = $this->createMock(LoggerInterface::class);
+ $this->cache = $this->createMock(ICacheFactory::class);
}
private function getTestUser($userId) {
->with('group1')
->willReturn(true);
- $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
+ $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache);
$manager->addBackend($backend);
$group = $manager->get('group1');
}
public function testGetNoBackend() {
- $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
+ $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache);
$this->assertNull($manager->get('group1'));
}
->with('group1')
->willReturn(false);
- $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
+ $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache);
$manager->addBackend($backend);
$this->assertNull($manager->get('group1'));
$backend = new \Test\Util\Group\Dummy();
$backend->createGroup('group1');
- $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
+ $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache);
$manager->addBackend($backend);
$group = $manager->get('group1');
->with('group1')
->willReturn(true);
- $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
+ $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache);
$manager->addBackend($backend1);
$manager->addBackend($backend2);
return true;
});
- $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
+ $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache);
$manager->addBackend($backend);
$group = $manager->createGroup('group1');
->method('getGroupDetails')
->willReturn([]);
- $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
+ $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache);
$manager->addBackend($backend);
$group = $manager->createGroup('group1');
$backend->expects($this->never())
->method('createGroup');
- $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
+ $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache);
$manager->addBackend($backend);
$group = $manager->createGroup('group1');
->with('group1')
->willReturn(true);
- $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
+ $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache);
$manager->addBackend($backend);
$groups = $manager->search('1');
->method('groupExists')
->willReturn(true);
- $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
+ $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache);
$manager->addBackend($backend1);
$manager->addBackend($backend2);
->method('groupExists')
->willReturn(true);
- $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
+ $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache);
$manager->addBackend($backend1);
$manager->addBackend($backend2);
/** @var \OC\User\Manager $userManager */
$userManager = $this->createMock(Manager::class);
- $manager = new \OC\Group\Manager($userManager, $this->dispatcher, $this->logger);
+ $manager = new \OC\Group\Manager($userManager, $this->dispatcher, $this->logger, $this->cache);
$manager->addBackend($backend);
$groups = $manager->search('1');
->with('group1')
->willReturn(true);
- $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
+ $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache);
$manager->addBackend($backend);
$groups = $manager->getUserGroups($this->getTestUser('user1'));
->with('myUID')
->willReturn(['123', 'abc']);
- $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
+ $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache);
$manager->addBackend($backend);
/** @var \OC\User\User|\PHPUnit\Framework\MockObject\MockObject $user */
->with('group1')
->willReturn(false);
- $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
+ $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache);
$manager->addBackend($backend);
/** @var \OC\User\User|\PHPUnit\Framework\MockObject\MockObject $user */
->method('groupExists')
->willReturn(true);
- $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
+ $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache);
$manager->addBackend($backend);
$this->assertTrue($manager->isInGroup('user1', 'group1'));
->method('groupExists')
->willReturn(true);
- $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
+ $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache);
$manager->addBackend($backend);
$this->assertTrue($manager->isAdmin('user1'));
->method('groupExists')
->willReturn(true);
- $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
+ $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache);
$manager->addBackend($backend);
$this->assertFalse($manager->isAdmin('user1'));
->method('groupExists')
->willReturn(true);
- $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
+ $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache);
$manager->addBackend($backend1);
$manager->addBackend($backend2);
}
});
- $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
+ $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache);
$manager->addBackend($backend);
$users = $manager->displayNamesInGroup('testgroup', 'user3');
}
});
- $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
+ $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache);
$manager->addBackend($backend);
$users = $manager->displayNamesInGroup('testgroup', 'user3', 1);
}
});
- $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
+ $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache);
$manager->addBackend($backend);
$users = $manager->displayNamesInGroup('testgroup', 'user3', 1, 1);
}
});
- $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
+ $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache);
$manager->addBackend($backend);
$users = $manager->displayNamesInGroup('testgroup', '');
}
});
- $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
+ $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache);
$manager->addBackend($backend);
$users = $manager->displayNamesInGroup('testgroup', '', 1);
}
});
- $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
+ $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache);
$manager->addBackend($backend);
$users = $manager->displayNamesInGroup('testgroup', '', 1, 1);
->with('group1')
->willReturn(true);
- $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
+ $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache);
$manager->addBackend($backend);
// prime cache
->method('removeFromGroup')
->willReturn(true);
- $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
+ $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache);
$manager->addBackend($backend);
// prime cache
->with('user1')
->willReturn(null);
- $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
+ $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache);
$manager->addBackend($backend);
$groups = $manager->getUserIdGroups('user1');
['group2', ['gid' => 'group2']],
]);
- $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
+ $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache);
$manager->addBackend($backend);
// group with display name