From feb5366a428e25ebb6054c4a3d0bc6ea304bb295 Mon Sep 17 00:00:00 2001
From: Arthur Schiwon <blizzz@arthur-schiwon.de>
Date: Mon, 17 Dec 2018 00:31:27 +0100
Subject: [PATCH] LDAP clear cache on config modification also when done via
 API or CLI

Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
---
 apps/user_ldap/lib/AppInfo/Application.php            | 6 ++++++
 apps/user_ldap/lib/Command/SetConfig.php              | 5 +++++
 apps/user_ldap/lib/Controller/ConfigAPIController.php | 9 ++++++++-
 3 files changed, 19 insertions(+), 1 deletion(-)

diff --git a/apps/user_ldap/lib/AppInfo/Application.php b/apps/user_ldap/lib/AppInfo/Application.php
index 6d5c416f04f..59d7cdb4924 100644
--- a/apps/user_ldap/lib/AppInfo/Application.php
+++ b/apps/user_ldap/lib/AppInfo/Application.php
@@ -24,6 +24,8 @@
 namespace OCA\User_LDAP\AppInfo;
 
 use OCA\User_LDAP\Controller\RenewPasswordController;
+use OCA\User_LDAP\ILDAPWrapper;
+use OCA\User_LDAP\LDAP;
 use OCP\AppFramework\App;
 use OCP\AppFramework\IAppContainer;
 use OCP\IL10N;
@@ -50,5 +52,9 @@ class Application extends App {
 				$server->getURLGenerator()
 			);
 		});
+
+		$container->registerService(ILDAPWrapper::class, function () {
+			return new LDAP();
+		});
 	}
 }
diff --git a/apps/user_ldap/lib/Command/SetConfig.php b/apps/user_ldap/lib/Command/SetConfig.php
index db656558efc..cf73874ade8 100644
--- a/apps/user_ldap/lib/Command/SetConfig.php
+++ b/apps/user_ldap/lib/Command/SetConfig.php
@@ -26,6 +26,8 @@
 
 namespace OCA\User_LDAP\Command;
 
+use OCA\User_LDAP\ConnectionFactory;
+use OCA\User_LDAP\LDAP;
 use Symfony\Component\Console\Command\Command;
 use Symfony\Component\Console\Input\InputArgument;
 use Symfony\Component\Console\Input\InputInterface;
@@ -83,5 +85,8 @@ class SetConfig extends Command {
 		$configHolder = new Configuration($configID);
 		$configHolder->$key = $value;
 		$configHolder->saveConfiguration();
+
+		$connectionFactory = new ConnectionFactory(new LDAP());
+		$connectionFactory->get($configID)->clearCache();
 	}
 }
diff --git a/apps/user_ldap/lib/Controller/ConfigAPIController.php b/apps/user_ldap/lib/Controller/ConfigAPIController.php
index a745e4ce060..ce885b7dabb 100644
--- a/apps/user_ldap/lib/Controller/ConfigAPIController.php
+++ b/apps/user_ldap/lib/Controller/ConfigAPIController.php
@@ -27,6 +27,7 @@ use OC\CapabilitiesManager;
 use OC\Core\Controller\OCSController;
 use OC\Security\IdentityProof\Manager;
 use OCA\User_LDAP\Configuration;
+use OCA\User_LDAP\ConnectionFactory;
 use OCA\User_LDAP\Helper;
 use OCP\AppFramework\Http\DataResponse;
 use OCP\AppFramework\OCS\OCSBadRequestException;
@@ -45,6 +46,9 @@ class ConfigAPIController extends OCSController {
 	/** @var ILogger */
 	private $logger;
 
+	/** @var ConnectionFactory */
+	private $connectionFactory;
+
 	public function __construct(
 		$appName,
 		IRequest $request,
@@ -53,7 +57,8 @@ class ConfigAPIController extends OCSController {
 		IUserManager $userManager,
 		Manager $keyManager,
 		Helper $ldapHelper,
-		ILogger $logger
+		ILogger $logger,
+		ConnectionFactory $connectionFactory
 	) {
 		parent::__construct(
 			$appName,
@@ -67,6 +72,7 @@ class ConfigAPIController extends OCSController {
 
 		$this->ldapHelper = $ldapHelper;
 		$this->logger = $logger;
+		$this->connectionFactory = $connectionFactory;
 	}
 
 	/**
@@ -198,6 +204,7 @@ class ConfigAPIController extends OCSController {
 			}
 
 			$configuration->saveConfiguration();
+			$this->connectionFactory->get($configID)->clearCache();
 		} catch(OCSException $e) {
 			throw $e;
 		} catch (\Exception $e) {
-- 
GitLab