102 lines
2.5 KiB
PHP
Raw Normal View History

2013-11-05 11:18:30 +00:00
<?php
2013-11-21 16:13:20 +00:00
class Scores_Auth_Adapter_Db implements Zend_Auth_Adapter_Interface
2013-11-05 11:18:30 +00:00
{
protected $_username;
2013-11-21 16:13:20 +00:00
2013-11-05 11:18:30 +00:00
protected $_password;
2013-11-21 16:13:20 +00:00
2013-11-05 11:18:30 +00:00
protected $_hash;
2013-11-21 16:13:20 +00:00
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)
2013-11-05 11:18:30 +00:00
{
$this->_username = $username;
$this->_password = $password;
$this->_hash = md5($username.'|'.$password);
$this->checkWs = $checkWs;
}
2013-11-21 16:13:20 +00:00
/**
* 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()
*/
2013-11-05 11:18:30 +00:00
public function authenticate()
{
$userM = new Application_Model_Sdv1Utilisateurs();
$sql = $userM->select()
->setIntegrityCheck(false)
2013-11-25 15:58:30 +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-05 11:18:30 +00:00
->where('u.login=?', $this->_username)
->where('u.actif=?', 1)
->where('u.deleted=?', 0)
->where('c.actif=?','Oui');
2013-11-21 16:13:20 +00:00
if ( count($this->clients) > 0 ) {
$sql->where('u.idClient IN('.join(',',$this->clients).')');
}
2013-11-05 11:18:30 +00:00
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
2013-11-21 16:13:20 +00:00
|| $this->_password == md5($result->login.'|'.$result->password) ) {
2013-11-05 11:18:30 +00:00
2013-11-21 16:13:20 +00:00
$identity->id = $result->id;
2013-11-05 11:18:30 +00:00
$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);
}
}
}
}