7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@zend.com so we can send you a copy immediately.
17 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
18 * @license http://framework.zend.com/license/new-bsd New BSD License
26 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
27 * @license http://framework.zend.com/license/new-bsd New BSD License
36 protected static $_instance = null;
39 * Persistent storage handler
41 * @var Zend_Auth_Storage_Interface
43 protected $_storage = null;
46 * Singleton pattern implementation makes "new" unavailable
50 protected function __construct()
54 * Singleton pattern implementation makes "clone" unavailable
58 protected function __clone()
62 * Returns an instance of Zend_Auth
64 * Singleton pattern implementation
66 * @return Zend_Auth Provides a fluent interface
68 public static function getInstance()
70 if (null === self
::$_instance) {
71 self
::$_instance = new self();
74 return self
::$_instance;
78 * Returns the persistent storage handler
80 * Session storage is used by default unless a different storage adapter has been set.
82 * @return Zend_Auth_Storage_Interface
84 public function getStorage()
86 if (null === $this->_storage
) {
88 * @see Zend_Auth_Storage_Session
90 require_once 'Zend/Auth/Storage/Session.php';
91 $this->setStorage(new Zend_Auth_Storage_Session());
94 return $this->_storage
;
98 * Sets the persistent storage handler
100 * @param Zend_Auth_Storage_Interface $storage
101 * @return Zend_Auth Provides a fluent interface
103 public function setStorage(Zend_Auth_Storage_Interface
$storage)
105 $this->_storage
= $storage;
110 * Authenticates against the supplied adapter
112 * @param Zend_Auth_Adapter_Interface $adapter
113 * @return Zend_Auth_Result
115 public function authenticate(Zend_Auth_Adapter_Interface
$adapter)
117 $result = $adapter->authenticate();
120 * ZF-7546 - prevent multiple succesive calls from storing inconsistent results
121 * Ensure storage has clean state
123 if ($this->hasIdentity()) {
124 $this->clearIdentity();
127 if ($result->isValid()) {
128 $this->getStorage()->write($result->getIdentity());
135 * Returns true if and only if an identity is available from storage
139 public function hasIdentity()
141 return !$this->getStorage()->isEmpty();
145 * Returns the identity from storage or null if no identity is available
149 public function getIdentity()
151 $storage = $this->getStorage();
153 if ($storage->isEmpty()) {
157 return $storage->read();
161 * Clears the identity from persistent storage
165 public function clearIdentity()
167 $this->getStorage()->clear();