Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use LazyUser in SetupManager #33582

Closed
wants to merge 5 commits into from
Closed
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
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
1 change: 1 addition & 0 deletions apps/user_ldap/tests/User_LDAPTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,7 @@ public function testUserExistsPublicAPI() {
$this->access->expects($this->any())
->method('getUserMapper')
->willReturn($this->createMock(UserMapping::class));
$this->prepareAccessForGetDisplayName();

//test for existing user
$result = \OC::$server->getUserManager()->userExists('gunslinger');
Expand Down
4 changes: 3 additions & 1 deletion lib/private/Files/Mount/MountPoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,9 @@ private function createStorage() {
$this->invalidStorage = true;
if ($this->mountPoint === '/') {
// the root storage could not be initialized, show the user!
throw new \Exception('The root storage could not be initialized. Please contact your local administrator.', $exception->getCode(), $exception);
throw new \Exception('The root storage could not be initialized. Please contact your local administrator.', (int)$exception->getCode(), $exception);
} elseif (substr_count($this->mountPoint, '/') == 2) { // home mount is `/<userid/`, all extra mounts have more /'s
throw new \Exception('The home storage could not be initialized. Please contact your local administrator.', (int)$exception->getCode(), $exception);
} else {
\OC::$server->get(LoggerInterface::class)->error($exception->getMessage(), ['exception' => $exception]);
}
Expand Down
15 changes: 9 additions & 6 deletions lib/private/Files/SetupManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use OC\Files\Storage\Wrapper\PermissionsMask;
use OC\Files\Storage\Wrapper\Quota;
use OC\Lockdown\Filesystem\NullStorage;
use OC\User\LazyUser;
use OC_App;
use OC_Hook;
use OC_Util;
Expand Down Expand Up @@ -367,7 +368,11 @@ private function getUserForPath(string $path) {
[, $userId] = explode('/', $path);
}

return $this->userManager->get($userId);
if ($this->userManager->userExists($userId)) {
return new LazyUser($userId, $this->userManager);
} else {
return null;
}
}

/**
Expand Down Expand Up @@ -530,11 +535,9 @@ private function listenForNewMountProviders() {
IMountProvider $provider
) {
foreach ($this->setupUsers as $userId) {
$user = $this->userManager->get($userId);
if ($user) {
$mounts = $provider->getMountsForUser($user, Filesystem::getLoader());
array_walk($mounts, [$this->mountManager, 'addMount']);
}
$user = new LazyUser($userId, $this->userManager);
$mounts = $provider->getMountsForUser($user, Filesystem::getLoader());
icewind1991 marked this conversation as resolved.
Show resolved Hide resolved
array_walk($mounts, [$this->mountManager, 'addMount']);
}
});
}
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);
}
}
}
9 changes: 6 additions & 3 deletions 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 All @@ -223,8 +227,7 @@ protected function getUserObject($uid, $backend, $cacheUser = true) {
* @return bool
*/
public function userExists($uid) {
$user = $this->get($uid);
return ($user !== null);
return $this->getDisplayName($uid) !== null;
}

/**
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
2 changes: 1 addition & 1 deletion tests/lib/Files/Mount/MountPointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function testInvalidStorage() {
$mountPoint = new \OC\Files\Mount\MountPoint(
// just use this because a real class is needed
'\Test\Files\Mount\DummyStorage',
'/mountpoint',
'/mountpoint/test',
null,
$loader
);
Expand Down
10 changes: 10 additions & 0 deletions tests/lib/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
use OC\Files\Mount\RootMountProvider;
use OC\Files\SetupManager;
use OC\Template\Base;
use OC\User\DisplayNameCache;
use OC\User\Manager;
use OCP\Command\IBus;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\Defaults;
Expand Down Expand Up @@ -148,6 +150,10 @@ protected function setUp(): void {
call_user_func([$this, $methodName]);
}
}

/** @var Manager $userManager */
$userManager = \OC::$server->get(Manager::class);
$userManager->getDisplayNameCache()->clear();
}

protected function onNotSuccessfulTest(\Throwable $t): void {
Expand Down Expand Up @@ -200,6 +206,10 @@ protected function tearDown(): void {
call_user_func([$this, $methodName]);
}
}

/** @var Manager $userManager */
$userManager = \OC::$server->get(Manager::class);
$userManager->getDisplayNameCache()->clear();
}

/**
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