public function getUser() { static $user; if (!$user) { $user = new sfUser(); $user->initialize($this); } return $user; }
* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ require_once dirname(__FILE__) . '/../../bootstrap/unit.php'; $t = new lime_test(32); $_SERVER['session_id'] = 'test'; $dispatcher = new sfEventDispatcher(); $sessionPath = sys_get_temp_dir() . '/sessions_' . rand(11111, 99999); $storage = new sfSessionTestStorage(array('session_path' => $sessionPath)); $user = new sfUser($dispatcher, $storage); // ->initialize() $t->diag('->initialize()'); $t->is($user->getCulture(), 'en', '->initialize() sets the culture to "en" by default'); $user->setCulture(null); $user->initialize($dispatcher, $storage, array('default_culture' => 'de')); user_flush($dispatcher, $user, $storage); $t->is($user->getCulture(), 'de', '->initialize() sets the culture to the value of default_culture if available'); user_flush($dispatcher, $user, $storage); $t->is($user->getCulture(), 'de', '->initialize() reads the culture from the session data if available'); $userBis = new sfUser($dispatcher, $storage); $t->is($userBis->getCulture(), 'de', '->initialize() serializes the culture to the session data'); // ->setCulture() ->getCulture() $t->diag('->setCulture() ->getCulture()'); $user->setCulture('fr'); $t->is($user->getCulture(), 'fr', '->setCulture() changes the current user culture'); // ->setFlash() ->getFlash() ->hasFlash() $t->diag('->setFlash() ->getFlash() ->hasFlash()'); $user->initialize($dispatcher, $storage, array('use_flash' => true)); $user->setFlash('foo', 'bar'); $t->is($user->getFlash('foo'), 'bar', '->setFlash() sets a flash variable');
public function initialize($context, $parameters = null) { // initialize parent parent::initialize($context, $parameters); // read data from storage $storage = $this->getContext()->getStorage(); $this->authenticated = $storage->read(self::AUTH_NAMESPACE); $this->credentials = $storage->read(self::CREDENTIAL_NAMESPACE); $this->lastRequest = $storage->read(self::LAST_REQUEST_NAMESPACE); if ($this->authenticated == null) { $this->authenticated = false; $this->credentials = array(); } // Automatic logout if no request for more than [sf_timeout] if (null !== $this->lastRequest && time() - $this->lastRequest > sfConfig::get('sf_timeout')) { if (sfConfig::get('sf_logging_enabled')) { $this->getContext()->getLogger()->info('{sfUser} automatic user logout'); } $this->setTimedOut(); $this->clearCredentials(); $this->setAuthenticated(false); } $this->lastRequest = time(); }
/** * Available options: * * * timeout: Timeout to automatically log out the user in seconds (1800 by default) * Set to false to disable * * @param sfEventDispatcher $dispatcher An sfEventDispatcher instance. * @param sfStorage $storage An sfStorage instance. * @param array $options An associative array of options. * * @see sfUser */ public function initialize(sfEventDispatcher $dispatcher, sfStorage $storage, $options = array()) { // initialize parent parent::initialize($dispatcher, $storage, $options); if (!array_key_exists('timeout', $this->options)) { $this->options['timeout'] = 1800; } // force the max lifetime for session garbage collector to be greater than timeout if (ini_get('session.gc_maxlifetime') < $this->options['timeout']) { ini_set('session.gc_maxlifetime', $this->options['timeout']); } // read data from storage $this->authenticated = $storage->read(self::AUTH_NAMESPACE); $this->credentials = $storage->read(self::CREDENTIAL_NAMESPACE); $this->lastRequest = $storage->read(self::LAST_REQUEST_NAMESPACE); if (null === $this->authenticated) { $this->authenticated = false; $this->credentials = array(); } else { // Automatic logout logged in user if no request within timeout parameter seconds $timeout = $this->options['timeout']; if (false !== $timeout && null !== $this->lastRequest && time() - $this->lastRequest >= $timeout) { if ($this->options['logging']) { $this->dispatcher->notify(new sfEvent($this, 'application.log', array('Automatic user logout due to timeout'))); } $this->setTimedOut(); $this->setAuthenticated(false); } } $this->lastRequest = time(); }
$context->storage = $storage; $user = new sfUser(); $user->initialize($context); $context->user = $user; // ->initialize() $t->diag('->initialize()'); $t->is($user->getCulture(), 'en', '->initialize() sets the culture to "en" by default'); sfConfig::set('sf_i18n_default_culture', 'de'); $user->setCulture(null); user_flush($context); $t->is($user->getCulture(), 'de', '->initialize() sets the culture to the value of sf_i18n_default_culture if available'); sfConfig::set('sf_i18n_default_culture', 'fr'); user_flush($context); $t->is($user->getCulture(), 'de', '->initialize() reads the culture from the session data if available'); $userBis = new sfUser(); $userBis->initialize($context); $t->is($userBis->getCulture(), 'de', '->intialize() serializes the culture to the session data'); // ->setCulture() ->getCulture() $t->diag('->setCulture() ->getCulture()'); $user->setCulture('fr'); $t->is($user->getCulture(), 'fr', '->setCulture() changes the current user culture'); // parameter holder proxy require_once $_test_dir . '/unit/sfParameterHolderTest.class.php'; $pht = new sfParameterHolderProxyTest($t); $pht->launchTests($user, 'parameter'); // attribute holder proxy require_once $_test_dir . '/unit/sfParameterHolderTest.class.php'; $pht = new sfParameterHolderProxyTest($t); $pht->launchTests($user, 'attribute'); // mixins require_once $_test_dir . '/unit/sfMixerTest.class.php';