Source of file AuthenticationService.php
Size: 4,016 Bytes - Last Modified: 2014-03-12T23:21:18+01:00
/home/theseer/Downloads/ZendFramework-2.3.0/library/Zend/Authentication/AuthenticationService.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 | <?php /** * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ namespace Zend\Authentication; class AuthenticationService implements AuthenticationServiceInterface { /** * Persistent storage handler * * @var Storage\StorageInterface */ protected $storage = null; /** * Authentication adapter * * @var Adapter\AdapterInterface */ protected $adapter = null; /** * Constructor * * @param Storage\StorageInterface $storage * @param Adapter\AdapterInterface $adapter */ public function __construct(Storage\StorageInterface $storage = null, Adapter\AdapterInterface $adapter = null) { if (null !== $storage) { $this->setStorage($storage); } if (null !== $adapter) { $this->setAdapter($adapter); } } /** * Returns the authentication adapter * * The adapter does not have a default if the storage adapter has not been set. * * @return Adapter\AdapterInterface|null */ public function getAdapter() { return $this->adapter; } /** * Sets the authentication adapter * * @param Adapter\AdapterInterface $adapter * @return AuthenticationService Provides a fluent interface */ public function setAdapter(Adapter\AdapterInterface $adapter) { $this->adapter = $adapter; return $this; } /** * Returns the persistent storage handler * * Session storage is used by default unless a different storage adapter has been set. * * @return Storage\StorageInterface */ public function getStorage() { if (null === $this->storage) { $this->setStorage(new Storage\Session()); } return $this->storage; } /** * Sets the persistent storage handler * * @param Storage\StorageInterface $storage * @return AuthenticationService Provides a fluent interface */ public function setStorage(Storage\StorageInterface $storage) { $this->storage = $storage; return $this; } /** * Authenticates against the supplied adapter * * @param Adapter\AdapterInterface $adapter * @return Result * @throws Exception\RuntimeException */ public function authenticate(Adapter\AdapterInterface $adapter = null) { if (!$adapter) { if (!$adapter = $this->getAdapter()) { throw new Exception\RuntimeException('An adapter must be set or passed prior to calling authenticate()'); } } $result = $adapter->authenticate(); /** * ZF-7546 - prevent multiple successive calls from storing inconsistent results * Ensure storage has clean state */ if ($this->hasIdentity()) { $this->clearIdentity(); } if ($result->isValid()) { $this->getStorage()->write($result->getIdentity()); } return $result; } /** * Returns true if and only if an identity is available from storage * * @return bool */ public function hasIdentity() { return !$this->getStorage()->isEmpty(); } /** * Returns the identity from storage or null if no identity is available * * @return mixed|null */ public function getIdentity() { $storage = $this->getStorage(); if ($storage->isEmpty()) { return null; } return $storage->read(); } /** * Clears the identity from persistent storage * * @return void */ public function clearIdentity() { $this->getStorage()->clear(); } } |