Buenas a todos/as,
Estoy tratando de implantar un Captcha en el inicio de sesión (login) de los usuarios en la extensión Yii-user, me surgen dos problemas:
[list=1]
[*] La primera vez que carga la página la imagen no aparece.
[*] La segunda vez que se envía el formulario metiendo un código de verificación erróneo, nombre de usuario y contraseña incorrecta, el usuario consigue hacer login. En cambio la primera vez lo hace bien y no se autentica.
[/list]
Os pego el código por aquí y os doy las gracias anticipadas a todos.
Vista del formulario:
<?php //if (UserModule::doCaptcha('login')): ?>
<div>
<?php $this->widget('CCaptcha'); ?>
</div>
<div>
<?php echo CHtml::activeLabelEx($model,'verifyCode'); ?>
<?php echo CHtml::activetextField($model,'verifyCode'); ?>
<p><?php echo UserModule::t("Please enter the letters as they are shown in the image above."); ?>
<br/><?php echo UserModule::t("Letters are not case-sensitive."); ?></p>
<?php echo CHtml::error($model,'verifyCode'); ?>
</div>
<?php //endif; ?>
Controlador:
public function accessRules() {
return array(
array('allow',
'actions'=>array('captcha'),
'users'=>array('*'),
),
array('deny',
'users'=>array('*'),
),
);
}
public function actions()
{
return array(
'captcha'=>array(
'class'=>'CCaptchaAction',
'backColor'=>0xFFFFFF,
),
);
}
public function actionLogin()
{
if (Yii::app()->user->isGuest) {
$model=new UserLogin;
if(isset($_POST['UserLogin']))
{
$model->attributes=$_POST['UserLogin'];
// validate user input and redirect to previous page if valid
if($model->validate()) {
$this->lastViset();
if (Yii::app()->user->returnUrl=='/index.php') // Perico Comentada
$this->redirect(Yii::app()->controller->module->returnUrl);
else
$this->redirect(Yii::app()->user->returnUrl);
}
}
// display the login form
$this->render('/user/login',array('model'=>$model));
} else
$this->redirect(Yii::app()->controller->module->returnUrl);
}
.....
....
Modelo
<?php
/**
* LoginForm class.
* LoginForm is the data structure for keeping
* user login form data. It is used by the 'login' action of 'SiteController'.
*/
class UserLogin extends CFormModel
{
public $username;
public $password;
public $rememberMe;
public $verifyCode;
/**
* Declares the validation rules.
* The rules state that username and password are required,
* and password needs to be authenticated.
*/
public function rules()
{
return array(
// username and password are required
array('username, password, ', 'required'),
// rememberMe needs to be a boolean
array('rememberMe', 'boolean'),
// password needs to be authenticated
array('password', 'authenticate'),
// array('username,password,verifyCode','required','on'=>'captchaRequired'),
array('verifyCode', 'captcha', 'allowEmpty'=>false)
);
}
/**
* Declares attribute labels.
*/
public function attributeLabels()
{
return array(
'rememberMe'=>UserModule::t("Remember me next time"),
'username'=>UserModule::t("username or email"),
'password'=>UserModule::t("password"),
);
}
/**
* Authenticates the password.
* This is the 'authenticate' validator as declared in rules().
*/
public function authenticate($attribute,$params)
{
if(!$this->hasErrors()) // we only want to authenticate when no input errors
{
$identity=new UserIdentity($this->username,$this->password);
$identity->authenticate();
switch($identity->errorCode)
{
case UserIdentity::ERROR_NONE:
$duration=$this->rememberMe ? Yii::app()->controller->module->rememberMeTime : 0;
Yii::app()->user->login($identity,$duration);
break;
case UserIdentity::ERROR_EMAIL_INVALID:
$this->addError("username",UserModule::t("Email is incorrect."));
break;
case UserIdentity::ERROR_USERNAME_INVALID:
$this->addError("username",UserModule::t("Username is incorrect."));
break;
case UserIdentity::ERROR_STATUS_NOTACTIV:
$this->addError("status",UserModule::t("You account is not activated."));
break;
case UserIdentity::ERROR_STATUS_BAN:
$this->addError("status",UserModule::t("You account is blocked."));
break;
case UserIdentity::ERROR_PASSWORD_INVALID:
$this->addError("password",UserModule::t("Password is incorrect."));
break;
}
}
}
}