Commit 976cee15 authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

Identity provider 'Db' : inutile de tester les 'ldap_filter' de chaque rôle si aucun LDAP activé

parent 857ea856
Loading
Loading
Loading
Loading
+9 −18
Original line number Diff line number Diff line
@@ -26,19 +26,16 @@ class Db extends AuthenticationIdentityProvider implements ChainableProvider, \B
    use RoleServiceAwareTrait;
    use HostLocalizationAwareTrait;


    /**
     * @var Ldap
     */
    private $ldap;
    private $ldap = null;

    /**
     * @var
     */
    private $detectionContexteExecution;



    /**
     * {@inheritDoc}
     */
@@ -47,8 +44,6 @@ class Db extends AuthenticationIdentityProvider implements ChainableProvider, \B
        $event->addRoles($this->getIdentityRoles());
    }



    /**
     * {@inheritDoc}
     */
@@ -83,6 +78,7 @@ class Db extends AuthenticationIdentityProvider implements ChainableProvider, \B
        }

        /* Injection des rôles par filtre LDAP */
        if ($this->ldap !== null) {
            $ldapRoles = $this->getServiceRole()->getList();
            foreach ($ldapRoles as $role) {
                if ($role->getLdapFilter() && !in_array($role, $roles)) {
@@ -91,6 +87,7 @@ class Db extends AuthenticationIdentityProvider implements ChainableProvider, \B
                    }
                }
            }
        }

        $inEtablissement = $this->getHostLocalization()->inEtablissement();
        if (!$inEtablissement) { // Si on n'est pas dans l'établissement, alors on filtre les rôles disponibles
@@ -104,8 +101,6 @@ class Db extends AuthenticationIdentityProvider implements ChainableProvider, \B
        return $roles;
    }



    /**
     * @param AbstractRole $role
     * @param string $dn
@@ -115,14 +110,12 @@ class Db extends AuthenticationIdentityProvider implements ChainableProvider, \B
    protected function roleMatches(AbstractRole $role, $dn)
    {
        try {
            return 1 === $this->getLdap()->count($role->getLdapFilter(), $dn, Ldap::SEARCH_SCOPE_SUB);
            return 1 === $this->ldap->count($role->getLdapFilter(), $dn, Ldap::SEARCH_SCOPE_SUB);
        } catch (\Laminas\Ldap\Exception\LdapException $e) {
            return false;
        }
    }



    /**
     * Returns the LDAP Object
     *
@@ -133,8 +126,6 @@ class Db extends AuthenticationIdentityProvider implements ChainableProvider, \B
        return $this->ldap;
    }



    /**
     * Set an Ldap connection
     *
+16 −2
Original line number Diff line number Diff line
@@ -2,7 +2,9 @@

namespace UnicaenAuth\Provider\Identity;

use Laminas\Ldap\Exception\LdapException;
use Psr\Container\ContainerInterface;
use UnicaenApp\Exception\RuntimeException;
use UnicaenApp\Options\ModuleOptions;
use Laminas\Ldap\Ldap;
use Laminas\ServiceManager\FactoryInterface;
@@ -32,8 +34,20 @@ class DbServiceFactory implements FactoryInterface
        $unicaenAppOptions = $container->get('unicaen-app_module_options');
        /* @var $unicaenAppOptions ModuleOptions */

        $ldap = new Ldap($unicaenAppOptions->getLdap()['connection']['default']['params']);
        $ldapOptions = $unicaenAppOptions->getLdap()['connection']['default']['params'] ?? [];
        if ($ldapOptions) {
            try {
                $ldap = new Ldap($ldapOptions);
                $ldap->connect();
            } catch (LdapException $e) {
                throw new RuntimeException(
                    "Impossible de se connecter à l'annuaire LDAP. Si aucune connexion LDAP n'est nécessaire, veuillez " .
                    "commenter les paramètres de config (connection > default > params).",
                    $e
                );
            }
            $identityProvider->setLdap($ldap);
        }

        $identityProvider->setServiceRole($container->get('UnicaenAuth\Service\Role'));