Commit 2991db61 authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

Merge branch 'master' into 6.x

parents 8778519d 339339ef
Loading
Loading
Loading
Loading
Loading
+7 −10
Original line number Diff line number Diff line
@@ -4,11 +4,10 @@ namespace UnicaenAuth\Provider\Identity;

use BjyAuthorize\Provider\Identity\ProviderInterface;
use BjyAuthorize\Service\Authorize;
use UnicaenAuth\Service\UserContext;
use Laminas\EventManager\EventManagerAwareInterface;
use Laminas\EventManager\EventManagerAwareTrait;
use Laminas\Permissions\Acl\Role\Registry;
use Laminas\Permissions\Acl\Role\RoleInterface;
use UnicaenAuth\Service\UserContext;

/**
 * Chaîne de responsabilité permettant à plusieures sources de fournir
@@ -104,20 +103,18 @@ class Chain implements ProviderInterface, EventManagerAwareInterface
        $this->getEventManager()->triggerEvent($e);
        $roles = $e->getRoles(); /** @var RoleInterface[] $roles */

        $registry = new Registry();
        foreach ($roles as $role) {
            // ne retient que les rôles déclarés dans les ACL
            if (!$this->authorizeService->getAcl()->hasRole($role)) {
                continue;
            }
            // évite les doublons
            if (!$registry->has($role)) {
            if (is_string($role)) {
                $role = $this->authorizeService->getAcl()->getRole($role);
            }
                $registry->add($role);
            // collecte en évitant les doublons
            $this->roles[$role->getRoleId()] = $role;
            }
            // NB : À condition que les 'identity providers' soient dans le bon ordre, cela permet à l'appli
            // de substituer son rôle "Authentifié" maison à celui par défaut.
        }

        return $this->roles;
+1 −1
Original line number Diff line number Diff line
@@ -41,7 +41,7 @@ class ChainEvent extends Event
        if ($roles instanceof Traversable) {
            $roles = iterator_to_array($roles);
        }
        $this->roles = array_merge($this->roles, $roles);
        $this->roles = array_unique(array_merge($this->roles, $roles));
        return $this;
    }

+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'));