File indexing completed on 2024-04-28 06:00:00

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Auth
0017  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0018  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0019  * @version    $Id$
0020  */
0021 
0022 
0023 /**
0024  * @category   Zend
0025  * @package    Zend_Auth
0026  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0027  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0028  */
0029 class Zend_Auth
0030 {
0031     /**
0032      * Singleton instance
0033      *
0034      * @var Zend_Auth
0035      */
0036     protected static $_instance = null;
0037 
0038     /**
0039      * Persistent storage handler
0040      *
0041      * @var Zend_Auth_Storage_Interface
0042      */
0043     protected $_storage = null;
0044 
0045     /**
0046      * Singleton pattern implementation makes "new" unavailable
0047      *
0048      * @return void
0049      */
0050     protected function __construct()
0051     {}
0052 
0053     /**
0054      * Singleton pattern implementation makes "clone" unavailable
0055      *
0056      * @return void
0057      */
0058     protected function __clone()
0059     {}
0060 
0061     /**
0062      * Returns an instance of Zend_Auth
0063      *
0064      * Singleton pattern implementation
0065      *
0066      * @return Zend_Auth Provides a fluent interface
0067      */
0068     public static function getInstance()
0069     {
0070         if (null === self::$_instance) {
0071             self::$_instance = new self();
0072         }
0073 
0074         return self::$_instance;
0075     }
0076 
0077     /**
0078      * Returns the persistent storage handler
0079      *
0080      * Session storage is used by default unless a different storage adapter has been set.
0081      *
0082      * @return Zend_Auth_Storage_Interface
0083      */
0084     public function getStorage()
0085     {
0086         if (null === $this->_storage) {
0087             /**
0088              * @see Zend_Auth_Storage_Session
0089              */
0090             // require_once 'Zend/Auth/Storage/Session.php';
0091             $this->setStorage(new Zend_Auth_Storage_Session());
0092         }
0093 
0094         return $this->_storage;
0095     }
0096 
0097     /**
0098      * Sets the persistent storage handler
0099      *
0100      * @param  Zend_Auth_Storage_Interface $storage
0101      * @return Zend_Auth Provides a fluent interface
0102      */
0103     public function setStorage(Zend_Auth_Storage_Interface $storage)
0104     {
0105         $this->_storage = $storage;
0106         return $this;
0107     }
0108 
0109     /**
0110      * Authenticates against the supplied adapter
0111      *
0112      * @param  Zend_Auth_Adapter_Interface $adapter
0113      * @return Zend_Auth_Result
0114      */
0115     public function authenticate(Zend_Auth_Adapter_Interface $adapter)
0116     {
0117         $result = $adapter->authenticate();
0118 
0119         /**
0120          * ZF-7546 - prevent multiple succesive calls from storing inconsistent results
0121          * Ensure storage has clean state
0122          */
0123         if ($this->hasIdentity()) {
0124             $this->clearIdentity();
0125         }
0126 
0127         if ($result->isValid()) {
0128             $this->getStorage()->write($result->getIdentity());
0129         }
0130 
0131         return $result;
0132     }
0133 
0134     /**
0135      * Returns true if and only if an identity is available from storage
0136      *
0137      * @return boolean
0138      */
0139     public function hasIdentity()
0140     {
0141         return !$this->getStorage()->isEmpty();
0142     }
0143 
0144     /**
0145      * Returns the identity from storage or null if no identity is available
0146      *
0147      * @return mixed|null
0148      */
0149     public function getIdentity()
0150     {
0151         $storage = $this->getStorage();
0152 
0153         if ($storage->isEmpty()) {
0154             return null;
0155         }
0156 
0157         return $storage->read();
0158     }
0159 
0160     /**
0161      * Clears the identity from persistent storage
0162      *
0163      * @return void
0164      */
0165     public function clearIdentity()
0166     {
0167         $this->getStorage()->clear();
0168     }
0169 }