Hola! Primero que nada quiero aclarar que soy nuevo en Yii…
Bueno, mi problema es el siguiente… estoy tratando de realizar la autenticacion contra un usuario de la BD, la cual su clave esta encriptada con md5, y me arroja que la clave o usuario son invalidas… no me arroja ningun error ni nada por el estilo, solo me rechaza la autenticacion…
Hice trace de las variables y verifique que efectivamente son iguales, no entiendo porque no retorna true…
he aqui mi codigo,
LoginForm
public function authenticate($attribute,$params)
{
if(!$this->hasErrors())
{
$this->_identity=new UserIdentity($this->username,$this->password);
if(!$this->_identity->authenticate())
$this->addError('password','Incorrect username or password.');
}
}
/**
* Logs in the user using the given username and password in the model.
* @return boolean whether login is successful
*/
public function login()
{
if($this->_identity===null)
{
$this->_identity=new UserIdentity($this->username,$this->password);
$this->_identity->authenticate();
}
if($this->_identity->errorCode===UserIdentity::ERROR_NONE)
{
$duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days
Yii::app()->user->login($this->_identity,$duration);
return true;
}
else
return false;
}
Usuario (modelo)
public function encrypt($value)
{
return md5($value);
}
SiteController
public function actionLogin()
{
$model=new LoginForm;
Yii::trace("The actionLogin() method is being requested","application.controllers.SiteController");
// if it is ajax validation request
if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
{
echo CActiveForm::validate($model);
Yii::app()->end();
}
// collect user input data
if(isset($_POST['LoginForm']))
{
$model->attributes=$_POST['LoginForm'];
// validate user input and redirect to the previous page if valid
if($model->validate() && $model->login()){
Yii::log("Successful login of user: " . Yii::app()->usuario->id, "info", "application.controllers.SiteController");
$this->redirect(Yii::app()->user->returnUrl);
}else{
Yii::log("Failed login attempt", "warning", "application.controllers.SiteController");
}
}
// display the login form
$this->render('login',array('model'=>$model));
}
UserIdentity
private $_id;
/**
* Authenticates a user using the User data model.
* @return boolean whether authentication succeeds.
*/
public function authenticate()
{
$user=Usuario::model()->findByAttributes(array('login'=>$this->username));
if($user===null){
$this->errorCode=self::ERROR_USERNAME_INVALID;
}else{
// output here the passwords; be sure that at least one log route is enabled!
Yii::log('encrypted db password: '.$user->clave,'trace', "application.controllers.SiteController");
Yii::log('input password: '.$this->password.' / encrypted: '.$user->encrypt($this->password),'trace', "application.controllers.SiteController");
Yii::log('la comparacion es = '.strcmp($user->clave,$user->encrypt($this->password)),'trace', "application.controllers.SiteController");
if(strcmp($user->clave,$user->encrypt($this->password)) != 0){
$this->errorCode=self::ERROR_PASSWORD_INVALID;
}else{
$this->_id = $user->id;
}
}
return !$this->errorCode;
}
public function getId()
{
return $this->_id;
}
y bueno, aqui parte del log
22:35:20.089421 trace system.db.ar.CActiveRecord
Usuario.findByAttributes()
in C:\wamp\www\enlaceOficios\protected\components\UserIdentity.php (17)
in C:\wamp\www\enlaceOficios\protected\models\LoginForm.php (52)
in C:\wamp\www\enlaceOficios\protected\controllers\SiteController.php (90)
22:35:20.102744 trace system.db.CDbCommand
Querying SQL: SELECT * FROM usuario
t
WHERE t
.login
=:yp0 LIMIT 1
in C:\wamp\www\enlaceOficios\protected\components\UserIdentity.php (17)
in C:\wamp\www\enlaceOficios\protected\models\LoginForm.php (52)
in C:\wamp\www\enlaceOficios\protected\controllers\SiteController.php (90)
22:35:20.108603 trace application.controllers.SiteController
encrypted db password: 9667e39572b8a1dc9e2728a5f8fe77bf
in C:\wamp\www\enlaceOficios\protected\components\UserIdentity.php (23)
in C:\wamp\www\enlaceOficios\protected\models\LoginForm.php (52)
in C:\wamp\www\enlaceOficios\protected\controllers\SiteController.php (90)
22:35:20.108925 trace application.controllers.SiteController
input password: tevez / encrypted: 9667e39572b8a1dc9e2728a5f8fe77bf
in C:\wamp\www\enlaceOficios\protected\components\UserIdentity.php (24)
in C:\wamp\www\enlaceOficios\protected\models\LoginForm.php (52)
in C:\wamp\www\enlaceOficios\protected\controllers\SiteController.php (90)
22:35:20.109246 trace application.controllers.SiteController
la comparacion es = 0
in C:\wamp\www\enlaceOficios\protected\components\UserIdentity.php (25)
in C:\wamp\www\enlaceOficios\protected\models\LoginForm.php (52)
in C:\wamp\www\enlaceOficios\protected\controllers\SiteController.php (90)
22:35:20.109783 warning application.controllers.SiteController
Failed login attempt
in C:\wamp\www\enlaceOficios\protected\controllers\SiteController.php (94)
in C:\wamp\www\enlaceOficios\index.php (13)
Espero que alguien me pueda ayudar, Gracias de antemano… Saludos