Skip to content
Snippets Groups Projects
Select Git revision
  • 846eb460040cab9c61f16a5c44f7324cf7eb29c5
  • master default protected
  • main
  • update_github_actions
  • 144_rocky8_support
  • 195-update-pdk-to-300
  • 144-rocky8
  • add_test_github_test_workflow
  • pdk_2.4.0
  • fix_unclosed_let_block_in_defines_client_spec
  • validation_fixes
  • freeradius_3_0_21_config_updates
  • data_types
  • PrepareBuster
  • travis
  • 4.0.1
  • 4.0.0
  • 3.9.2
  • 3.9.1
  • 3.9.0
  • 3.8.2
  • 3.8.1
  • 3.8.0
  • 3.7.0
  • 3.6.0
  • 3.5.0
  • 3.4.3
  • 3.4.2
  • 3.4.1
  • 3.4.0
  • 3.3.0
  • 3.2.0
  • 3.1.0
  • 3.0.0
  • 2.3.1
35 results

script.pp

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    AbstractDb.php 3.76 KiB
    <?php
    
    namespace UnicaenAuthentification\Authentication\Adapter;
    
    use UnicaenAuthentification\Options\Traits\ModuleOptionsAwareTrait;
    use Zend\Authentication\Result as AuthenticationResult;
    use Zend\EventManager\EventInterface;
    use Zend\Session\Container as SessionContainer;
    use ZfcUser\Authentication\Adapter\AdapterChainEvent;
    use ZfcUser\Entity\UserInterface;
    use ZfcUser\Mapper\UserInterface as UserMapperInterface;
    
    /**
     * Classe abstraite des adpater d'authentification à partir de la base de données.
     * 
     * Ajout par rapport à la classe mère : si aucune base de données ou table n'existe,
     * l'authentification ne plante pas (i.e. renvoit false).
     *
     * @author Bertrand GAUTHIER <bertrand.gauthier@unicaen.fr>
     */
    abstract class AbstractDb extends AbstractAdapter
    {
        use ModuleOptionsAwareTrait;
    
        /**
         * @var string
         */
        protected $type;
    
        /**
         * @var AdapterChainEvent
         */
        protected $event;
    
        /**
         * @var UserMapperInterface
         */
        protected $mapper;
    
        /**
         * @inheritDoc
         */
    //    public function authenticate(EventInterface $e): bool
        public function authenticate( $e): bool
        {
            // NB: Dans la version 3.0.0 de zf-commons/zfc-user, cette méthode prend un EventInterface.
            // Mais dans la branche 3.x, c'est un AdapterChainEvent !
            // Si un jour c'est un AdapterChainEvent qui est attendu, plus besoin de faire $e->getTarget().
            $this->event = $e->getTarget();
    
            if ($this->event->getIdentity()) {
                return true;
            }
    
            if ($this->isSatisfied()) {
                $storage = $this->getStorage()->read();
                $this->event
                    ->setIdentity($storage['identity'])
                    ->setCode(AuthenticationResult::SUCCESS)
                    ->setMessages(array('Authentication successful.'));
                return true;
            }
    
            $userObject = $this->fetchUserObject();
            if ($userObject === null) {
                return false;
            }
    
            if ($this->moduleOptions->getEnableUserState()) {
                // Don't allow user to login if state is not in allowed list
                if (!in_array($userObject->getState(), $this->moduleOptions->getAllowedLoginStates())) {
                    $this->event
                        ->setCode(AuthenticationResult::FAILURE_UNCATEGORIZED)
                        ->setMessages(["Ce compte utilisateur a été désactivé"]);
                    $this->setSatisfied(false);
                    return false;
                }
            }
    
            $result = $this->authenticateUserObject($userObject);
            if ($result === false) {
                return false;
            }
    
            // regen the id
            $session = new SessionContainer($this->getStorage()->getNamespace());
            $session->getManager()->regenerateId();
    
            // Success!
            $identity = $this->createSessionIdentity($userObject->getUsername());
            $this->event->setIdentity($identity);
            $this->setSatisfied(true);
            $storage = $this->getStorage()->read();
            $storage['identity'] = $this->event->getIdentity();
            $this->getStorage()->write($storage);
            $this->event
                ->setCode(AuthenticationResult::SUCCESS)
                ->setMessages(array('Authentication successful.'));
    
            return true;
        }
    
        /**
         * @return \ZfcUser\Entity\UserInterface|null
         */
        abstract protected function fetchUserObject(): ?UserInterface;
    
        /**
         * @param \ZfcUser\Entity\UserInterface $userObject
         * @return bool
         */
        abstract protected function authenticateUserObject(UserInterface $userObject): bool;
    
        /**
         * setMapper
         *
         * @param UserMapperInterface $mapper
         * @return self
         */
        public function setMapper(UserMapperInterface $mapper): self
        {
            $this->mapper = $mapper;
    
            return $this;
        }
    }