Boa tarde pessoal,
Gostaria de tirar um duvida eu nao sei se ja aconteceu com alguem:
Tenho um formulario de login que te a opçao de lembre-me, so que quando ativa ele nao grava as informaçao no cookies pois toda vez que fecho o navegado e abro o aplicativo novamente ele pede o e-mail e a senha novamente. Nao sei se isso é do navegador ou do meu codigo:
Loginform
class LoginForm extends CFormModel
{
public $email;
public $password;
public $rememberMe = false;
private $_identity;
/**
* Declares the validation rules.
* The rules state that username and password are required,
* and password needs to be authenticated.
*/
public function rules()
{
return array(
array('email, password', 'required'),
array('email', 'email'),
array('password', 'authenticate'),
);
}
/**
* Declares attribute labels.
*/
public function attributeLabels()
{
return array(
'email'=>'Email Address',
);
}
/**
* 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->email,$this->password);
$identity->authenticate();
switch($identity->errorCode)
{
case UserIdentity::ERROR_NONE:
Yii::app()->user->login($identity);
break;
case UserIdentity::ERROR_USERNAME_INVALID:
$this->addError('email','Email address is incorrect.');
break;
default: // UserIdentity::ERROR_PASSWORD_INVALID
$this->addError('password','Password is incorrect.');
break;
}
}
}
public function login()
{
if($this->_identity===null)
{
$this->_identity=new UserIdentity($this->email,$this->password);
$this->_identity->authenticate();
}
if($this->_identity->errorCode===UserIdentity::ERROR_NONE)
{
$duration=(Yii::app()->user->allowAutoLogin && $this->rememberMe) ? 3600*24*30 : 0;
// log user in and save in session all appended data
Yii::app()->user->login($this->_identity,$duration);
// $duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days
// Yii::app()->user->login($this->_identity,$duration);
return true;
}
else
return false;
}
}
UserIdentify
class UserIdentity extends CUserIdentity
{
// Need to store the user's ID:
private $_id;
/**
* Authenticates a user.
* The example implementation makes sure if the username and password
* are both 'demo'.
* In practical applications, this should be changed to authenticate
* against some persistent user identity storage (e.g. database).
* @return boolean whether authentication succeeds.
*/
public function authenticate()
{
$user = Usuario::model()->findByAttributes(array('email'=>$this->username));
if ($user===null) { // No user found!
$this->errorCode=self::ERROR_USERNAME_INVALID;
} else if ($user->password !== md5($this->password) ) { // Invalid password!
$this->errorCode=self::ERROR_PASSWORD_INVALID;
} else { // Okay!
$this->errorCode=self::ERROR_NONE;
// Store the role in a session:
$this->setState('role', $user->perfil);
$this->_id = $user->id;
}
return !$this->errorCode;
}
public function getId()
{
return $this->_id;
}
}
[code]
Login
<div class="form" id="login">
<br />
<?php $form=$this->beginWidget(‘CActiveForm’, array(
'id'=>'login-form',
'enableAjaxValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true,
),
)); ?>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'E-mail'); ?>
<?php echo $form->textField($model,'email',array('class'=>'text ui-widget-content ui-corner-all','size'=>'30')); ?>
<?php // echo $form->error($model,'username'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'password'); ?>
<?php echo $form->passwordField($model,'password', array('class'=>'text ui-widget-content ui-corner-all', 'size'=>'30')); ?>
<?php // echo $form->error($model,'password'); ?>
</div>
<div class="row rememberMe">
<?php echo $form->checkBox($model,'rememberMe'); ?>
<?php echo $form->label($model,'rememberMe'); ?>
<?php // echo $form->error($model,'rememberMe'); ?>
</div>
<div class="row submit">
<?php echo CHtml::submitButton('Entrar'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
[/code]
LoginController
class LoginController extends Controller
{
public $layout='login';
public function actionLogin()
{
$model=new LoginForm;
if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
{
echo CActiveForm::validate($model);
Yii::app()->end();
}
if(isset($_POST['LoginForm']))
{
$model->attributes=$_POST['LoginForm'];
if($model->validate() && $model->login())
$this->redirect('home');
}else{
if(!Yii::app()->user->isGuest){
$this->redirect('home');
}
}
$this->render('login',array('model'=>$model));
}
public function actionLogout()
{
Yii::app()->user->logout();
$this->redirect(Yii::app()->homeUrl);
}
}
Se alguem poder me ajudar agradeço…
Valew