Skip to content

Commit

Permalink
Fetch LDAP provider factory on demand
Browse files Browse the repository at this point in the history
The provider is not injectible on Nextcloud 20. Therefore it needs some
error handling to factor in the possible absence.

Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
  • Loading branch information
ChristophWurst committed Dec 6, 2021
1 parent aae379a commit c4ac466
Showing 1 changed file with 20 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->IServerContainer = $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

0 comments on commit c4ac466

Please sign in to comment.