2013-11-21 16:31:49 +00:00
|
|
|
<?php
|
|
|
|
class Scores_Auth_Adapter_Db implements Zend_Auth_Adapter_Interface
|
|
|
|
{
|
|
|
|
protected $_username;
|
|
|
|
|
|
|
|
protected $_password;
|
|
|
|
|
|
|
|
protected $_hash;
|
|
|
|
|
|
|
|
protected $_timeout = 3600;
|
|
|
|
|
|
|
|
protected $checkWs = false;
|
|
|
|
|
|
|
|
protected $clients = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param string $username
|
|
|
|
* @param string $password
|
|
|
|
* @param boolean $checkWs
|
|
|
|
*/
|
|
|
|
public function __construct($username, $password, $checkWs = false)
|
|
|
|
{
|
|
|
|
$this->_username = $username;
|
|
|
|
$this->_password = $password;
|
|
|
|
$this->_hash = md5($username.'|'.$password);
|
|
|
|
$this->checkWs = $checkWs;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Limit access to only client IDs
|
|
|
|
* @param array $id
|
|
|
|
*/
|
|
|
|
public function limitClient($id = null)
|
|
|
|
{
|
|
|
|
if (is_array($id) && count($id)>0) {
|
|
|
|
$this->clients = $id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Override the timeout
|
|
|
|
* @param integer $seconds
|
|
|
|
*/
|
|
|
|
public function setTimeout($seconds = null)
|
|
|
|
{
|
|
|
|
if ($seconds===null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
$this->_timeout = $seconds;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* (non-PHPdoc)
|
|
|
|
* @see Zend_Auth_Adapter_Interface::authenticate()
|
|
|
|
*/
|
|
|
|
public function authenticate()
|
|
|
|
{
|
|
|
|
$userM = new Application_Model_Sdv1Utilisateurs();
|
|
|
|
|
|
|
|
$sql = $userM->select()
|
|
|
|
->setIntegrityCheck(false)
|
2013-11-29 13:54:52 +00:00
|
|
|
->from(array('u'=>'sdv1.utilisateurs'), array('u.idClient', 'u.id', 'u.login', 'u.password'))
|
|
|
|
->join(array('c'=>'sdv1.clients'), 'u.idClient = c.id', array('c.timeout'))
|
2013-11-21 16:31:49 +00:00
|
|
|
->where('u.login=?', $this->_username)
|
|
|
|
->where('u.actif=?', 1)
|
|
|
|
->where('u.deleted=?', 0)
|
|
|
|
->where('c.actif=?','Oui');
|
|
|
|
|
|
|
|
if ( count($this->clients) > 0 ) {
|
|
|
|
$sql->where('u.idClient IN('.join(',',$this->clients).')');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->checkWs) {
|
|
|
|
$sql->where('u.accesWS=?',1);
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = $userM->fetchRow($sql);
|
|
|
|
|
|
|
|
$identity = new stdClass();
|
|
|
|
$identity->username = $this->_username;
|
|
|
|
$identity->hash = $this->_hash;
|
|
|
|
|
|
|
|
if ( null === $result ) {
|
|
|
|
return new Zend_Auth_Result(Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND, $identity);
|
|
|
|
} else {
|
|
|
|
if ( $this->_password == $result->password
|
|
|
|
|| $this->_password == md5($result->login.'|'.$result->password) ) {
|
|
|
|
|
|
|
|
$identity->id = $result->id;
|
|
|
|
$identity->idClient = $result->idClient;
|
|
|
|
$timeout = (!empty($result->timeout)) ? $result->timeout : $this->_timeout;
|
|
|
|
$identity->timeout = $timeout;
|
|
|
|
$identity->time = time() + $timeout;
|
|
|
|
return new Zend_Auth_Result(Zend_Auth_Result::SUCCESS, $identity);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
return new Zend_Auth_Result(Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID, $identity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|