Skip to content

Commit

Permalink
Merge pull request #5783 from nextcloud/fix/ldap-factory-injection
Browse files Browse the repository at this point in the history
Fetch LDAP provider factory on demand
  • Loading branch information
ChristophWurst authored Dec 6, 2021
2 parents 68fa55e + 5763ed1 commit 3c8de95
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
32 changes: 20 additions & 12 deletions lib/Service/Provisioning/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@
use OCA\Mail\Exception\ValidationException;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
use OCP\IServerContainer;
use OCP\IUser;
use OCP\IUserManager;
use OCP\LDAP\ILDAPProvider;
use OCP\LDAP\ILDAPProviderFactory;
use OCP\Security\ICrypto;
use Psr\Log\LoggerInterface;
use Throwable;

class Manager {

Expand All @@ -55,8 +57,8 @@ class Manager {
/** @var ICrypto */
private $crypto;

/** @var ILDAPProviderFactory */
private $ldapProviderFactory;
/** @var IServerContainer */
private $serverContainer;

/** @var AliasMapper */
private $aliasMapper;
Expand All @@ -71,15 +73,15 @@ public function __construct(IUserManager $userManager,
ProvisioningMapper $provisioningMapper,
MailAccountMapper $mailAccountMapper,
ICrypto $crypto,
ILDAPProviderFactory $ldapProviderFactory,
IServerContainer $appContainer,
AliasMapper $aliasMapper,
LoggerInterface $logger,
TagMapper $tagMapper) {
$this->userManager = $userManager;
$this->provisioningMapper = $provisioningMapper;
$this->mailAccountMapper = $mailAccountMapper;
$this->crypto = $crypto;
$this->ldapProviderFactory = $ldapProviderFactory;
$this->serverContainer = $appContainer;
$this->aliasMapper = $aliasMapper;
$this->logger = $logger;
$this->tagMapper = $tagMapper;
Expand Down Expand Up @@ -160,16 +162,22 @@ public function ldapAliasesIntegration(Provisioning $provisioning, IUser $user):
return $provisioning;
}

/** @psalm-suppress UndefinedInterfaceMethod */
if ($this->ldapProviderFactory->isAvailable() === false) {
$this->logger->debug('Request to provision mail aliases but LDAP not available');
return $provisioning;
try {
$ldapProviderFactory = $this->serverContainer->get(ILDAPProviderFactory::class);
/** @psalm-suppress UndefinedInterfaceMethod */
if ($ldapProviderFactory->isAvailable() === false) {
$this->logger->debug('Request to provision mail aliases but LDAP not available');
return $provisioning;
}
$ldapProvider = $ldapProviderFactory->getLDAPProvider();
/** @psalm-suppress UndefinedInterfaceMethod */
$provisioning->setAliases($ldapProvider->getMultiValueUserAttribute($user->getUID(), $provisioning->getLdapAliasesAttribute()));
} catch (Throwable $e) {
$this->logger->debug('Request to provision mail aliases but LDAP erros: ' . $e->getMessage(), [
'exception' => $e,
]);
}

$ldapProvider = $this->ldapProviderFactory->getLDAPProvider();
/** @psalm-suppress UndefinedInterfaceMethod */
$provisioning->setAliases($ldapProvider->getMultiValueUserAttribute($user->getUID(), $provisioning->getLdapAliasesAttribute()));

return $provisioning;
}

Expand Down
7 changes: 7 additions & 0 deletions tests/psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,13 @@
<code>SaveDraftEvent</code>
</MissingDependency>
</file>
<file src="lib/Service/Provisioning/Manager.php">
<MissingDependency occurrences="3">
<code>$this-&gt;serverContainer</code>
<code>IServerContainer</code>
<code>IServerContainer</code>
</MissingDependency>
</file>
<file src="lib/Service/Sync/ImapToDbSynchronizer.php">
<MissingDependency occurrences="4">
<code>NewMessagesSynchronized</code>
Expand Down

0 comments on commit 3c8de95

Please sign in to comment.