web/lib/Zend/Auth.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     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-2010 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 20096 2010-01-06 02:05:09Z bkarwin $
       
    20  */
       
    21 
       
    22 
       
    23 /**
       
    24  * @category   Zend
       
    25  * @package    Zend_Auth
       
    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
       
    28  */
       
    29 class Zend_Auth
       
    30 {
       
    31     /**
       
    32      * Singleton instance
       
    33      *
       
    34      * @var Zend_Auth
       
    35      */
       
    36     protected static $_instance = null;
       
    37 
       
    38     /**
       
    39      * Persistent storage handler
       
    40      *
       
    41      * @var Zend_Auth_Storage_Interface
       
    42      */
       
    43     protected $_storage = null;
       
    44 
       
    45     /**
       
    46      * Singleton pattern implementation makes "new" unavailable
       
    47      *
       
    48      * @return void
       
    49      */
       
    50     protected function __construct()
       
    51     {}
       
    52 
       
    53     /**
       
    54      * Singleton pattern implementation makes "clone" unavailable
       
    55      *
       
    56      * @return void
       
    57      */
       
    58     protected function __clone()
       
    59     {}
       
    60 
       
    61     /**
       
    62      * Returns an instance of Zend_Auth
       
    63      *
       
    64      * Singleton pattern implementation
       
    65      *
       
    66      * @return Zend_Auth Provides a fluent interface
       
    67      */
       
    68     public static function getInstance()
       
    69     {
       
    70         if (null === self::$_instance) {
       
    71             self::$_instance = new self();
       
    72         }
       
    73 
       
    74         return self::$_instance;
       
    75     }
       
    76 
       
    77     /**
       
    78      * Returns the persistent storage handler
       
    79      *
       
    80      * Session storage is used by default unless a different storage adapter has been set.
       
    81      *
       
    82      * @return Zend_Auth_Storage_Interface
       
    83      */
       
    84     public function getStorage()
       
    85     {
       
    86         if (null === $this->_storage) {
       
    87             /**
       
    88              * @see Zend_Auth_Storage_Session
       
    89              */
       
    90             require_once 'Zend/Auth/Storage/Session.php';
       
    91             $this->setStorage(new Zend_Auth_Storage_Session());
       
    92         }
       
    93 
       
    94         return $this->_storage;
       
    95     }
       
    96 
       
    97     /**
       
    98      * Sets the persistent storage handler
       
    99      *
       
   100      * @param  Zend_Auth_Storage_Interface $storage
       
   101      * @return Zend_Auth Provides a fluent interface
       
   102      */
       
   103     public function setStorage(Zend_Auth_Storage_Interface $storage)
       
   104     {
       
   105         $this->_storage = $storage;
       
   106         return $this;
       
   107     }
       
   108 
       
   109     /**
       
   110      * Authenticates against the supplied adapter
       
   111      *
       
   112      * @param  Zend_Auth_Adapter_Interface $adapter
       
   113      * @return Zend_Auth_Result
       
   114      */
       
   115     public function authenticate(Zend_Auth_Adapter_Interface $adapter)
       
   116     {
       
   117         $result = $adapter->authenticate();
       
   118 
       
   119         /**
       
   120          * ZF-7546 - prevent multiple succesive calls from storing inconsistent results
       
   121          * Ensure storage has clean state
       
   122          */
       
   123         if ($this->hasIdentity()) {
       
   124             $this->clearIdentity();
       
   125         }
       
   126 
       
   127         if ($result->isValid()) {
       
   128             $this->getStorage()->write($result->getIdentity());
       
   129         }
       
   130 
       
   131         return $result;
       
   132     }
       
   133 
       
   134     /**
       
   135      * Returns true if and only if an identity is available from storage
       
   136      *
       
   137      * @return boolean
       
   138      */
       
   139     public function hasIdentity()
       
   140     {
       
   141         return !$this->getStorage()->isEmpty();
       
   142     }
       
   143 
       
   144     /**
       
   145      * Returns the identity from storage or null if no identity is available
       
   146      *
       
   147      * @return mixed|null
       
   148      */
       
   149     public function getIdentity()
       
   150     {
       
   151         $storage = $this->getStorage();
       
   152 
       
   153         if ($storage->isEmpty()) {
       
   154             return null;
       
   155         }
       
   156 
       
   157         return $storage->read();
       
   158     }
       
   159 
       
   160     /**
       
   161      * Clears the identity from persistent storage
       
   162      *
       
   163      * @return void
       
   164      */
       
   165     public function clearIdentity()
       
   166     {
       
   167         $this->getStorage()->clear();
       
   168     }
       
   169 }