Auth.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  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.
  14. *
  15. * @category Zend
  16. * @package Zend_Auth
  17. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: Auth.php 2504 2011-12-28 07:35:29Z liu21st $
  20. */
  21. /**
  22. * @category Zend
  23. * @package Zend_Auth
  24. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  25. * @license http://framework.zend.com/license/new-bsd New BSD License
  26. */
  27. class Zend_Auth
  28. {
  29. /**
  30. * Singleton instance
  31. *
  32. * @var Zend_Auth
  33. */
  34. protected static $_instance = null;
  35. /**
  36. * Persistent storage handler
  37. *
  38. * @var Zend_Auth_Storage_Interface
  39. */
  40. protected $_storage = null;
  41. /**
  42. * Singleton pattern implementation makes "new" unavailable
  43. *
  44. * @return void
  45. */
  46. protected function __construct()
  47. {}
  48. /**
  49. * Singleton pattern implementation makes "clone" unavailable
  50. *
  51. * @return void
  52. */
  53. protected function __clone()
  54. {}
  55. /**
  56. * Returns an instance of Zend_Auth
  57. *
  58. * Singleton pattern implementation
  59. *
  60. * @return Zend_Auth Provides a fluent interface
  61. */
  62. public static function getInstance()
  63. {
  64. if (null === self::$_instance) {
  65. self::$_instance = new self();
  66. }
  67. return self::$_instance;
  68. }
  69. /**
  70. * Returns the persistent storage handler
  71. *
  72. * Session storage is used by default unless a different storage adapter has been set.
  73. *
  74. * @return Zend_Auth_Storage_Interface
  75. */
  76. public function getStorage()
  77. {
  78. if (null === $this->_storage) {
  79. /**
  80. * @see Zend_Auth_Storage_Session
  81. */
  82. require_once 'Zend/Auth/Storage/Session.php';
  83. $this->setStorage(new Zend_Auth_Storage_Session());
  84. }
  85. return $this->_storage;
  86. }
  87. /**
  88. * Sets the persistent storage handler
  89. *
  90. * @param Zend_Auth_Storage_Interface $storage
  91. * @return Zend_Auth Provides a fluent interface
  92. */
  93. public function setStorage(Zend_Auth_Storage_Interface $storage)
  94. {
  95. $this->_storage = $storage;
  96. return $this;
  97. }
  98. /**
  99. * Authenticates against the supplied adapter
  100. *
  101. * @param Zend_Auth_Adapter_Interface $adapter
  102. * @return Zend_Auth_Result
  103. */
  104. public function authenticate(Zend_Auth_Adapter_Interface $adapter)
  105. {
  106. $result = $adapter->authenticate();
  107. /**
  108. * ZF-7546 - prevent multiple succesive calls from storing inconsistent results
  109. * Ensure storage has clean state
  110. */
  111. if ($this->hasIdentity()) {
  112. $this->clearIdentity();
  113. }
  114. if ($result->isValid()) {
  115. $this->getStorage()->write($result->getIdentity());
  116. }
  117. return $result;
  118. }
  119. /**
  120. * Returns true if and only if an identity is available from storage
  121. *
  122. * @return boolean
  123. */
  124. public function hasIdentity()
  125. {
  126. return !$this->getStorage()->isEmpty();
  127. }
  128. /**
  129. * Returns the identity from storage or null if no identity is available
  130. *
  131. * @return mixed|null
  132. */
  133. public function getIdentity()
  134. {
  135. $storage = $this->getStorage();
  136. if ($storage->isEmpty()) {
  137. return null;
  138. }
  139. return $storage->read();
  140. }
  141. /**
  142. * Clears the identity from persistent storage
  143. *
  144. * @return void
  145. */
  146. public function clearIdentity()
  147. {
  148. $this->getStorage()->clear();
  149. }
  150. }