mirror of
https://github.com/atlanticbiomedical/portal-legacy.git
synced 2025-07-02 01:47:28 -04:00
initial commit
This commit is contained in:
255
lib/symfony/user/sfBasicSecurityUser.class.php
Executable file
255
lib/symfony/user/sfBasicSecurityUser.class.php
Executable file
@ -0,0 +1,255 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the symfony package.
|
||||
* (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||
* (c) 2004-2006 Sean Kerr.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
/**
|
||||
* sfBasicSecurityUser will handle any type of data as a credential.
|
||||
*
|
||||
* @package symfony
|
||||
* @subpackage user
|
||||
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||
* @author Sean Kerr <skerr@mojavi.org>
|
||||
* @version SVN: $Id: sfBasicSecurityUser.class.php 5160 2007-09-16 16:05:28Z fabien $
|
||||
*/
|
||||
class sfBasicSecurityUser extends sfUser implements sfSecurityUser
|
||||
{
|
||||
const LAST_REQUEST_NAMESPACE = 'symfony/user/sfUser/lastRequest';
|
||||
const AUTH_NAMESPACE = 'symfony/user/sfUser/authenticated';
|
||||
const CREDENTIAL_NAMESPACE = 'symfony/user/sfUser/credentials';
|
||||
|
||||
protected $lastRequest = null;
|
||||
|
||||
protected $credentials = null;
|
||||
protected $authenticated = null;
|
||||
|
||||
protected $timedout = false;
|
||||
|
||||
/**
|
||||
* Clears all credentials.
|
||||
*
|
||||
*/
|
||||
public function clearCredentials()
|
||||
{
|
||||
$this->credentials = null;
|
||||
$this->credentials = array();
|
||||
}
|
||||
|
||||
/**
|
||||
* returns an array containing the credentials
|
||||
*/
|
||||
public function listCredentials()
|
||||
{
|
||||
return $this->credentials;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a credential.
|
||||
*
|
||||
* @param mixed credential
|
||||
*/
|
||||
public function removeCredential($credential)
|
||||
{
|
||||
if ($this->hasCredential($credential))
|
||||
{
|
||||
foreach ($this->credentials as $key => $value)
|
||||
{
|
||||
if ($credential == $value)
|
||||
{
|
||||
if (sfConfig::get('sf_logging_enabled'))
|
||||
{
|
||||
$this->getContext()->getLogger()->info('{sfUser} remove credential "'.$credential.'"');
|
||||
}
|
||||
|
||||
unset($this->credentials[$key]);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a credential.
|
||||
*
|
||||
* @param mixed credential
|
||||
*/
|
||||
public function addCredential($credential)
|
||||
{
|
||||
$this->addCredentials(func_get_args());
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds several credential at once.
|
||||
*
|
||||
* @param mixed array or list of credentials
|
||||
*/
|
||||
public function addCredentials()
|
||||
{
|
||||
if (func_num_args() == 0) return;
|
||||
|
||||
// Add all credentials
|
||||
$credentials = (is_array(func_get_arg(0))) ? func_get_arg(0) : func_get_args();
|
||||
|
||||
if (sfConfig::get('sf_logging_enabled'))
|
||||
{
|
||||
$this->getContext()->getLogger()->info('{sfUser} add credential(s) "'.implode(', ', $credentials).'"');
|
||||
}
|
||||
|
||||
foreach ($credentials as $aCredential)
|
||||
{
|
||||
if (!in_array($aCredential, $this->credentials))
|
||||
{
|
||||
$this->credentials[] = $aCredential;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns true if user has credential.
|
||||
*
|
||||
* @param mixed credentials
|
||||
* @param boolean useAnd specify the mode, either AND or OR
|
||||
* @return boolean
|
||||
*
|
||||
* @author Olivier Verdier <Olivier.Verdier@free.fr>
|
||||
*/
|
||||
public function hasCredential($credentials, $useAnd = true)
|
||||
{
|
||||
if (!is_array($credentials))
|
||||
{
|
||||
return in_array($credentials, $this->credentials);
|
||||
}
|
||||
|
||||
// now we assume that $credentials is an array
|
||||
$test = false;
|
||||
|
||||
foreach ($credentials as $credential)
|
||||
{
|
||||
// recursively check the credential with a switched AND/OR mode
|
||||
$test = $this->hasCredential($credential, $useAnd ? false : true);
|
||||
|
||||
if ($useAnd)
|
||||
{
|
||||
$test = $test ? false : true;
|
||||
}
|
||||
|
||||
if ($test) // either passed one in OR mode or failed one in AND mode
|
||||
{
|
||||
break; // the matter is settled
|
||||
}
|
||||
}
|
||||
|
||||
if ($useAnd) // in AND mode we succeed if $test is false
|
||||
{
|
||||
$test = $test ? false : true;
|
||||
}
|
||||
|
||||
return $test;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if user is authenticated.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function isAuthenticated()
|
||||
{
|
||||
return $this->authenticated;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets authentication for user.
|
||||
*
|
||||
* @param boolean
|
||||
*/
|
||||
public function setAuthenticated($authenticated)
|
||||
{
|
||||
if (sfConfig::get('sf_logging_enabled'))
|
||||
{
|
||||
$this->getContext()->getLogger()->info('{sfUser} user is '.($authenticated === true ? '' : 'not ').'authenticated');
|
||||
}
|
||||
|
||||
if ($authenticated === true)
|
||||
{
|
||||
$this->authenticated = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->authenticated = false;
|
||||
$this->clearCredentials();
|
||||
}
|
||||
}
|
||||
|
||||
public function setTimedOut()
|
||||
{
|
||||
$this->timedout = true;
|
||||
}
|
||||
|
||||
public function isTimedOut()
|
||||
{
|
||||
return $this->timedout;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the timestamp of the last user request.
|
||||
*
|
||||
* @param integer
|
||||
*/
|
||||
public function getLastRequestTime()
|
||||
{
|
||||
return $this->lastRequest;
|
||||
}
|
||||
|
||||
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->setAuthenticated(false);
|
||||
}
|
||||
|
||||
$this->lastRequest = time();
|
||||
}
|
||||
|
||||
public function shutdown()
|
||||
{
|
||||
$storage = $this->getContext()->getStorage();
|
||||
|
||||
// write the last request time to the storage
|
||||
$storage->write(self::LAST_REQUEST_NAMESPACE, $this->lastRequest);
|
||||
|
||||
$storage->write(self::AUTH_NAMESPACE, $this->authenticated);
|
||||
$storage->write(self::CREDENTIAL_NAMESPACE, $this->credentials);
|
||||
|
||||
// call the parent shutdown method
|
||||
parent::shutdown();
|
||||
}
|
||||
}
|
72
lib/symfony/user/sfSecurityUser.class.php
Executable file
72
lib/symfony/user/sfSecurityUser.class.php
Executable file
@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the symfony package.
|
||||
* (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||
* (c) 2004-2006 Sean Kerr.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
/**
|
||||
* sfSecurityUser interface provides advanced security manipulation methods.
|
||||
*
|
||||
* @package symfony
|
||||
* @subpackage user
|
||||
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||
* @author Sean Kerr <skerr@mojavi.org>
|
||||
* @version SVN: $Id: sfSecurityUser.class.php 2971 2006-12-08 12:14:14Z fabien $
|
||||
*/
|
||||
interface sfSecurityUser
|
||||
{
|
||||
/**
|
||||
* Add a credential to this user.
|
||||
*
|
||||
* @param mixed Credential data.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function addCredential($credential);
|
||||
|
||||
/**
|
||||
* Clear all credentials associated with this user.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function clearCredentials();
|
||||
|
||||
/**
|
||||
* Indicates whether or not this user has a credential.
|
||||
*
|
||||
* @param mixed Credential data.
|
||||
*
|
||||
* @return bool true, if this user has the credential, otherwise false.
|
||||
*/
|
||||
public function hasCredential($credential);
|
||||
|
||||
/**
|
||||
* Indicates whether or not this user is authenticated.
|
||||
*
|
||||
* @return bool true, if this user is authenticated, otherwise false.
|
||||
*/
|
||||
public function isAuthenticated();
|
||||
|
||||
/**
|
||||
* Remove a credential from this user.
|
||||
*
|
||||
* @param mixed Credential data.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function removeCredential($credential);
|
||||
|
||||
/**
|
||||
* Set the authenticated status of this user.
|
||||
*
|
||||
* @param bool A flag indicating the authenticated status of this user.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setAuthenticated($authenticated);
|
||||
}
|
228
lib/symfony/user/sfUser.class.php
Executable file
228
lib/symfony/user/sfUser.class.php
Executable file
@ -0,0 +1,228 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the symfony package.
|
||||
* (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||
* (c) 2004-2006 Sean Kerr.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
/**
|
||||
*
|
||||
* sfUser wraps a client session and provides accessor methods for user
|
||||
* attributes. It also makes storing and retrieving multiple page form data
|
||||
* rather easy by allowing user attributes to be stored in namespaces, which
|
||||
* help organize data.
|
||||
*
|
||||
* @package symfony
|
||||
* @subpackage user
|
||||
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||
* @author Sean Kerr <skerr@mojavi.org>
|
||||
* @version SVN: $Id: sfUser.class.php 2971 2006-12-08 12:14:14Z fabien $
|
||||
*/
|
||||
class sfUser
|
||||
{
|
||||
/**
|
||||
* The namespace under which attributes will be stored.
|
||||
*/
|
||||
const ATTRIBUTE_NAMESPACE = 'symfony/user/sfUser/attributes';
|
||||
|
||||
const CULTURE_NAMESPACE = 'symfony/user/sfUser/culture';
|
||||
|
||||
protected
|
||||
$parameterHolder = null,
|
||||
$attributeHolder = null,
|
||||
$culture = null,
|
||||
$context = null;
|
||||
|
||||
/**
|
||||
* Retrieve the current application context.
|
||||
*
|
||||
* @return Context A Context instance.
|
||||
*/
|
||||
public function getContext()
|
||||
{
|
||||
return $this->context;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize this User.
|
||||
*
|
||||
* @param Context A Context instance.
|
||||
* @param array An associative array of initialization parameters.
|
||||
*
|
||||
* @return bool true, if initialization completes successfully, otherwise
|
||||
* false.
|
||||
*
|
||||
* @throws <b>sfInitializationException</b> If an error occurs while initializing this User.
|
||||
*/
|
||||
public function initialize($context, $parameters = array())
|
||||
{
|
||||
$this->context = $context;
|
||||
|
||||
$this->parameterHolder = new sfParameterHolder();
|
||||
$this->parameterHolder->add($parameters);
|
||||
|
||||
$this->attributeHolder = new sfParameterHolder(self::ATTRIBUTE_NAMESPACE);
|
||||
|
||||
// read attributes from storage
|
||||
$attributes = $context->getStorage()->read(self::ATTRIBUTE_NAMESPACE);
|
||||
if (is_array($attributes))
|
||||
{
|
||||
foreach ($attributes as $namespace => $values)
|
||||
{
|
||||
$this->attributeHolder->add($values, $namespace);
|
||||
}
|
||||
}
|
||||
|
||||
// set the user culture to sf_culture parameter if present in the request
|
||||
// otherwise
|
||||
// - use the culture defined in the user session
|
||||
// - use the default culture set in i18n.yml
|
||||
if (!($culture = $context->getRequest()->getParameter('sf_culture')))
|
||||
{
|
||||
if (null === ($culture = $context->getStorage()->read(self::CULTURE_NAMESPACE)))
|
||||
{
|
||||
$culture = sfConfig::get('sf_i18n_default_culture', 'en');
|
||||
}
|
||||
}
|
||||
|
||||
$this->setCulture($culture);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a new sfUser implementation instance.
|
||||
*
|
||||
* @param string A sfUser implementation name
|
||||
*
|
||||
* @return User A sfUser implementation instance.
|
||||
*
|
||||
* @throws <b>sfFactoryException</b> If a user implementation instance cannot
|
||||
*/
|
||||
public static function newInstance($class)
|
||||
{
|
||||
// the class exists
|
||||
$object = new $class();
|
||||
|
||||
if (!($object instanceof sfUser))
|
||||
{
|
||||
// the class name is of the wrong type
|
||||
$error = 'Class "%s" is not of the type sfUser';
|
||||
$error = sprintf($error, $class);
|
||||
|
||||
throw new sfFactoryException($error);
|
||||
}
|
||||
|
||||
return $object;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets culture.
|
||||
*
|
||||
* @param string culture
|
||||
*/
|
||||
public function setCulture($culture)
|
||||
{
|
||||
if ($this->culture != $culture)
|
||||
{
|
||||
$this->culture = $culture;
|
||||
|
||||
// change the message format object with the new culture
|
||||
if (sfConfig::get('sf_i18n'))
|
||||
{
|
||||
$this->context->getI18N()->setCulture($culture);
|
||||
}
|
||||
|
||||
// add the culture in the routing default parameters
|
||||
sfConfig::set('sf_routing_defaults', array_merge((array) sfConfig::get('sf_routing_defaults'), array('sf_culture' => $culture)));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets culture.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getCulture()
|
||||
{
|
||||
return $this->culture;
|
||||
}
|
||||
|
||||
public function getParameterHolder()
|
||||
{
|
||||
return $this->parameterHolder;
|
||||
}
|
||||
|
||||
public function getAttributeHolder()
|
||||
{
|
||||
return $this->attributeHolder;
|
||||
}
|
||||
|
||||
public function getAttribute($name, $default = null, $ns = null)
|
||||
{
|
||||
return $this->attributeHolder->get($name, $default, $ns);
|
||||
}
|
||||
|
||||
public function hasAttribute($name, $ns = null)
|
||||
{
|
||||
return $this->attributeHolder->has($name, $ns);
|
||||
}
|
||||
|
||||
public function setAttribute($name, $value, $ns = null)
|
||||
{
|
||||
return $this->attributeHolder->set($name, $value, $ns);
|
||||
}
|
||||
|
||||
public function getParameter($name, $default = null, $ns = null)
|
||||
{
|
||||
return $this->parameterHolder->get($name, $default, $ns);
|
||||
}
|
||||
|
||||
public function hasParameter($name, $ns = null)
|
||||
{
|
||||
return $this->parameterHolder->has($name, $ns);
|
||||
}
|
||||
|
||||
public function setParameter($name, $value, $ns = null)
|
||||
{
|
||||
return $this->parameterHolder->set($name, $value, $ns);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the shutdown procedure.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function shutdown()
|
||||
{
|
||||
$storage = $this->getContext()->getStorage();
|
||||
|
||||
$attributes = array();
|
||||
foreach ($this->attributeHolder->getNamespaces() as $namespace)
|
||||
{
|
||||
$attributes[$namespace] = $this->attributeHolder->getAll($namespace);
|
||||
}
|
||||
|
||||
// write attributes to the storage
|
||||
$storage->write(self::ATTRIBUTE_NAMESPACE, $attributes);
|
||||
|
||||
// write culture to the storage
|
||||
$storage->write(self::CULTURE_NAMESPACE, $this->culture);
|
||||
|
||||
session_write_close();
|
||||
}
|
||||
|
||||
public function __call($method, $arguments)
|
||||
{
|
||||
if (!$callable = sfMixer::getCallable('sfUser:'.$method))
|
||||
{
|
||||
throw new sfException(sprintf('Call to undefined method sfUser::%s', $method));
|
||||
}
|
||||
|
||||
array_unshift($arguments, $this);
|
||||
|
||||
return call_user_func_array($callable, $arguments);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user