Skip to content
Snippets Groups Projects
Select Git revision
  • b8eb36a84e49778eb93a4a998d173760b4ad80bd
  • master default protected
  • subtemplate
  • release_7.0.6
  • php84
  • 6.x
  • v5.x-test
  • 5x
  • 7.1.1
  • 7.1.0
  • 7.0.6
  • 7.0.5
  • 7.0.4
  • 7.0.3
  • 7.0.2
  • 7.0.1
  • 7.0.0
  • 6.1.7
  • 6.1.6
  • 6.1.5
  • 6.1.4
  • 6.1.3
  • 6.1.2
  • 6.1.1
  • 6.1.0
  • 6.0.3
  • 6.0.2
  • 5.0.6
28 results

002_privileges.sql

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    AdapterChain.php 3.68 KiB
    <?php
    
    namespace ZfcUser\Authentication\Adapter;
    
    use Laminas\Authentication\Adapter\AdapterInterface;
    use Laminas\Authentication\Result as AuthenticationResult;
    use Laminas\EventManager\Event;
    use Laminas\EventManager\EventManagerAwareTrait;
    use Laminas\Stdlib\RequestInterface as Request;
    use Laminas\Stdlib\ResponseInterface as Response;
    use ZfcUser\Exception;
    
    class AdapterChain implements AdapterInterface
    {
        use EventManagerAwareTrait;
    
        /**
         * @var AdapterChainEvent
         */
        protected $event;
    
        /**
         * Returns the authentication result
         *
         * @return AuthenticationResult
         */
        public function authenticate()
        {
            $e = $this->getEvent();
    
            $result = new AuthenticationResult(
                $e->getCode(),
                $e->getIdentity(),
                $e->getMessages()
            );
    
            $this->resetAdapters();
    
            return $result;
        }
    
        /**
         * prepareForAuthentication
         *
         * @param  Request $request
         * @return Response|bool
         * @throws Exception\AuthenticationEventException
         */
        public function prepareForAuthentication(Request $request)
        {
            $e = $this->getEvent();
            $e->setRequest($request);
    
            $this->getEventManager()->trigger('authenticate.pre', $e);
    
            $result = $this->getEventManager()->trigger('authenticate', $e, function ($test) {
                return ($test instanceof Response);
            });
    
            if ($result->stopped()) {
                if ($result->last() instanceof Response) {
                    return $result->last();
                }
    
                throw new Exception\AuthenticationEventException(
                    sprintf(
                        'Auth event was stopped without a response. Got "%s" instead',
                        is_object($result->last()) ? get_class($result->last()) : gettype($result->last())
                    )
                );
            }
    
            if ($e->getIdentity()) {
                $this->getEventManager()->trigger('authenticate.success', $e);
                return true;
            }
    
            $this->getEventManager()->trigger('authenticate.fail', $e);
    
            return false;
        }
    
        /**
         * resetAdapters
         *
         * @return AdapterChain
         */
        public function resetAdapters()
        {
            $sharedManager = $this->getEventManager()->getSharedManager();
    
            if ($sharedManager) {
                $listeners = $sharedManager->getListeners(['authenticate'], 'authenticate');
                foreach ($listeners as $listener) {
                    if (is_array($listener) && $listener[0] instanceof ChainableAdapter) {
                        $listener[0]->getStorage()->clear();
                    }
                }
            }
    
            return $this;
        }
    
        /**
         * logoutAdapters
         *
         * @return AdapterChain
         */
        public function logoutAdapters()
        {
            //Adapters might need to perform additional cleanup after logout
            $this->getEventManager()->trigger('logout', $this->getEvent());
        }
    
        /**
         * Get the auth event
         *
         * @return AdapterChainEvent
         */
        public function getEvent()
        {
            if (null === $this->event) {
                $this->setEvent(new AdapterChainEvent);
                $this->event->setTarget($this);
            }
    
            return $this->event;
        }
    
        /**
         * Set an event to use during dispatch
         *
         * By default, will re-cast to AdapterChainEvent if another event type is provided.
         *
         * @param  Event $e
         * @return AdapterChain
         */
        public function setEvent(Event $e)
        {
            if (!$e instanceof AdapterChainEvent) {
                $eventParams = $e->getParams();
                $e = new AdapterChainEvent();
                $e->setParams($eventParams);
            }
    
            $this->event = $e;
    
            return $this;
        }
    }