Incam_PROFIND_Web/www/protected/components/IdentificacionUsuario.php
2012-10-04 14:54:02 +00:00

89 lines
3.0 KiB
PHP

<?php
/**
* Sirve para comprobar si un usuario en válido en el sistema a partir
* del email y de la contraseña.
*/
class IdentificacionUsuario extends CBaseUserIdentity {
private $_id;
public $email;
public $password;
// Constantes de error declaradas en la clase padre
//const ERROR_NONE=0;
//const ERROR_USERNAME_INVALID=1;
//const ERROR_PASSWORD_INVALID=2;
//const ERROR_UNKNOWN_IDENTITY=100;
const ERROR_ESTADO_NOACTIVO=3;
const ERROR_ESTADO_BORRADO=4;
const ERROR_ESTADO_DENEGADO=5;
/**
* Contructor.
* @param string $email email
* @param string $password password
*/
public function __construct($email, $password) {
$this->email = $email;
$this->password = $password;
}
/**
* Comprueba la identificación de un usuario
* @return boolean whether authentication succeeds.
*/
public function authenticate() {
$usuario = Usuario::model()->findByAttributes(array('email' => $this->email));
$ph = new PasswordHash(
Yii::app()->params['phpass']['iteration_count_log2'],
Yii::app()->params['phpass']['portable_hashes']
);
if ($usuario === NULL) {
$this->errorCode = self::ERROR_USERNAME_INVALID;
} else {
if (($usuario->password !== md5($this->password)) && !$ph->checkPassword($this->password, $usuario->password))
$this->errorCode = self::ERROR_PASSWORD_INVALID;
else if($usuario->estado == Usuario::ESTADO_NOACTIVO)
$this->errorCode = self::ERROR_ESTADO_NOACTIVO;
else if($usuario->estado == Usuario::ESTADO_BORRADO)
$this->errorCode = self::ERROR_ESTADO_BORRADO;
else if($usuario->estado == Usuario::ESTADO_DENEGADO)
$this->errorCode = self::ERROR_ESTADO_DENEGADO;
else {
// Si el usuario tiene cifrada la contraseña con md5()
// pasarla al nuevo método.
if ($usuario->password{0} !== '$') {
$usuario->password = $ph->HashPassword($this->password);
$usuario->save();
}
$this->_id = $usuario->id;
$this->errorCode = self::ERROR_NONE;
}
}
return !$this->errorCode;
}
/**
* Returns the unique identifier for the identity.
* The default implementation simply returns {@link username}.
* This method is required by {@link IUserIdentity}.
* @return string the unique identifier for the identity.
*/
public function getId() {
return $this->_id;
}
/**
* Returns the display name for the identity.
* The default implementation simply returns {@link username}.
* This method is required by {@link IUserIdentity}.
* @return string the display name for the identity.
*/
public function getName() {
return $this->email;
}
}