From 878a1d3d749e24d62e22899cb3b7ebd52f4ab5d3 Mon Sep 17 00:00:00 2001
From: Bertrand Gauthier <bertrand.gauthier@unicaen.fr>
Date: Tue, 28 Aug 2018 09:47:14 +0200
Subject: [PATCH] =?UTF-8?q?Retrait=20des=20storage=20et=20identity=20provi?=
 =?UTF-8?q?der=20Ldap=20lorsque=20l'auth=20LDAP=20est=20d=C3=A9sactiv?=
 =?UTF-8?q?=C3=A9e?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../Storage/ChainServiceFactory.php           |  9 +++++
 .../Provider/Identity/ChainServiceFactory.php | 34 +++++++++++++++----
 2 files changed, 36 insertions(+), 7 deletions(-)

diff --git a/src/UnicaenAuth/Authentication/Storage/ChainServiceFactory.php b/src/UnicaenAuth/Authentication/Storage/ChainServiceFactory.php
index c15c3fc..647d4c4 100644
--- a/src/UnicaenAuth/Authentication/Storage/ChainServiceFactory.php
+++ b/src/UnicaenAuth/Authentication/Storage/ChainServiceFactory.php
@@ -2,6 +2,7 @@
 
 namespace UnicaenAuth\Authentication\Storage;
 
+use UnicaenAuth\Options\ModuleOptions;
 use Zend\ServiceManager\FactoryInterface;
 use Zend\ServiceManager\ServiceLocatorInterface;
 
@@ -31,6 +32,14 @@ class ChainServiceFactory implements FactoryInterface
     {
         $chain = new Chain();
 
+        /** @var ModuleOptions $options */
+        $options = $serviceLocator->get('unicaen-auth_module_options');
+
+        // retrait du fournisseur Ldap si l'auth LDAP est désactivée
+        if (isset($options->getLdap()['enabled']) && ! $options->getLdap()['enabled']) {
+            unset($this->mandatoryStorages[200]);
+        }
+
         $storages = $this->mandatoryStorages + $this->storages;
         krsort($storages);
 
diff --git a/src/UnicaenAuth/Provider/Identity/ChainServiceFactory.php b/src/UnicaenAuth/Provider/Identity/ChainServiceFactory.php
index d435fc2..eca7b94 100644
--- a/src/UnicaenAuth/Provider/Identity/ChainServiceFactory.php
+++ b/src/UnicaenAuth/Provider/Identity/ChainServiceFactory.php
@@ -1,4 +1,5 @@
 <?php
+
 namespace UnicaenAuth\Provider\Identity;
 
 use Zend\ServiceManager\FactoryInterface;
@@ -19,16 +20,11 @@ class ChainServiceFactory implements FactoryInterface
      */
     public function createService(ServiceLocatorInterface $serviceLocator)
     {
-        $config = $serviceLocator->get('Config'); //'unicaen-auth_module_options'
-        if (!isset($config['unicaen-auth']['identity_providers']) || !$config['unicaen-auth']['identity_providers']) {
-            throw new \UnicaenApp\Exception\RuntimeException("Aucun fournisseur d'identité spécifié dans la config.");
-        }
-
-        $providers = (array) $config['unicaen-auth']['identity_providers'];
-
         $chain = new Chain();
         $chain->setServiceLocator($serviceLocator);
 
+        $providers = $this->computeProviders($serviceLocator);
+
         foreach ($providers as $priority => $name) {
             $provider = $serviceLocator->get($name);
             $chain->getEventManager()->attach('getIdentityRoles', [$provider, 'injectIdentityRoles'], $priority);
@@ -36,4 +32,28 @@ class ChainServiceFactory implements FactoryInterface
 
         return $chain;
     }
+
+    /**
+     * @param ServiceLocatorInterface $serviceLocator
+     * @return array
+     */
+    private function computeProviders(ServiceLocatorInterface $serviceLocator)
+    {
+        $config = $serviceLocator->get('Config'); //'unicaen-auth_module_options'
+        if (! isset($config['unicaen-auth']['identity_providers']) || ! $config['unicaen-auth']['identity_providers']) {
+            throw new \UnicaenApp\Exception\RuntimeException("Aucun fournisseur d'identité spécifié dans la config.");
+        }
+
+        $providers = (array) $config['unicaen-auth']['identity_providers'];
+
+        // retrait du fournisseur 'Ldap' si l'auth Ldap est désactivée
+        if (isset($config['unicaen-auth']['ldap']['enabled']) && ! $config['unicaen-auth']['ldap']['enabled']) {
+            $key = array_search('UnicaenAuth\Provider\Identity\Ldap', $providers, true);
+            if ($key !== false) {
+                unset($providers[$key]);
+            }
+        }
+
+        return $providers;
+    }
 }
\ No newline at end of file
-- 
GitLab