From 8a416dff70f5cdc93fe58fb0bc9eb16fc4bb332d Mon Sep 17 00:00:00 2001
From: Bertrand GAUTHIER <bertrand.gauthier@unicaen.fr>
Date: Mon, 1 Feb 2021 12:05:22 +0100
Subject: [PATCH] Correction bug : test d'activation manquant dans les
 adapteurs Db et Ldap.

---
 src/UnicaenAuth/Authentication/Adapter/Db.php | 21 +++++++++++++++++++
 .../Adapter/DbAdapterFactory.php              |  6 ++++++
 .../Authentication/Adapter/Ldap.php           | 18 ++++++++++++++++
 3 files changed, 45 insertions(+)

diff --git a/src/UnicaenAuth/Authentication/Adapter/Db.php b/src/UnicaenAuth/Authentication/Adapter/Db.php
index f1e2bf5..a684aee 100644
--- a/src/UnicaenAuth/Authentication/Adapter/Db.php
+++ b/src/UnicaenAuth/Authentication/Adapter/Db.php
@@ -6,6 +6,7 @@ use Interop\Container\ContainerInterface;
 use UnicaenApp\ServiceManager\ServiceLocatorAwareInterface;
 use UnicaenApp\ServiceManager\ServiceLocatorAwareTrait;
 use UnicaenAuth\Options\ModuleOptions;
+use UnicaenAuth\Options\Traits\ModuleOptionsAwareTrait;
 use Zend\Authentication\Result as AuthenticationResult;
 use Zend\Crypt\Password\Bcrypt;
 use Zend\EventManager\EventInterface;
@@ -24,6 +25,8 @@ use ZfcUser\Mapper\UserInterface as UserMapperInterface;
  */
 class Db extends AbstractAdapter implements ServiceLocatorAwareInterface
 {
+    use ModuleOptionsAwareTrait;
+
     const TYPE = 'db';
 
     /**
@@ -106,6 +109,10 @@ class Db extends AbstractAdapter implements ServiceLocatorAwareInterface
             return;
         }
 
+        if (! $this->isEnabled()) {
+            return;
+        }
+
         $identity   = $e->getRequest()->getPost()->get('identity');
         $credential = $e->getRequest()->getPost()->get('credential');
         $credential = $this->preProcessCredential($credential);
@@ -169,6 +176,20 @@ class Db extends AbstractAdapter implements ServiceLocatorAwareInterface
             ->setMessages(array('Authentication successful.'));
     }
 
+    /**
+     * @return bool
+     */
+    protected function isEnabled()
+    {
+        $config = $this->moduleOptions->getDb();
+
+        if (isset($config['enabled'])) {
+            return (bool) $config['enabled'];
+        }
+
+        return false;
+    }
+
     protected function updateUserPasswordHash(UserInterface $userObject, $password, Bcrypt $bcrypt)
     {
         $hash = explode('$', $userObject->getPassword());
diff --git a/src/UnicaenAuth/Authentication/Adapter/DbAdapterFactory.php b/src/UnicaenAuth/Authentication/Adapter/DbAdapterFactory.php
index 63abde5..214247e 100644
--- a/src/UnicaenAuth/Authentication/Adapter/DbAdapterFactory.php
+++ b/src/UnicaenAuth/Authentication/Adapter/DbAdapterFactory.php
@@ -23,6 +23,12 @@ class DbAdapterFactory
         $adapter = new Db();
         $adapter->setStorage(new Session(Db::class));
 
+        $options = array_merge(
+            $container->get('zfcuser_module_options')->toArray(),
+            $container->get('unicaen-auth_module_options')->toArray());
+        $moduleOptions = new ModuleOptions($options);
+        $adapter->setModuleOptions($moduleOptions);
+
         $substitut = $moduleOptions->getDb()['type'] ?? null;
         if ($substitut !== null) {
             $adapter->setType($substitut);
diff --git a/src/UnicaenAuth/Authentication/Adapter/Ldap.php b/src/UnicaenAuth/Authentication/Adapter/Ldap.php
index 934edb2..a503625 100644
--- a/src/UnicaenAuth/Authentication/Adapter/Ldap.php
+++ b/src/UnicaenAuth/Authentication/Adapter/Ldap.php
@@ -114,6 +114,10 @@ class Ldap extends AbstractAdapter implements EventManagerAwareInterface
             return;
         }
 
+        if (! $this->isEnabled()) {
+            return;
+        }
+
         $username   = $e->getRequest()->getPost()->get('identity');
         $credential = $e->getRequest()->getPost()->get('credential');
 
@@ -159,6 +163,20 @@ class Ldap extends AbstractAdapter implements EventManagerAwareInterface
         $this->userService->userAuthenticated($ldapPeople);
     }
 
+    /**
+     * @return bool
+     */
+    protected function isEnabled()
+    {
+        $config = $this->moduleOptions->getLdap();
+
+        if (isset($config['enabled'])) {
+            return (bool) $config['enabled'];
+        }
+
+        return false;
+    }
+
     /**
      * Extrait le loginUsurpateur et le loginUsurpé si l'identifiant spécifé est de la forme
      * "loginUsurpateur=loginUsurpé".
-- 
GitLab