Начинаю разбираться с Yii (запускаю его локально под denwer-ом), вылезла проблема с авторизацией, перекопал все форумы,
но проблемы решить так и не смог.
Вызываю метод login для пользователя, но все равно при дальнейшей проверке в методе beforeAction выдает что пользователя нет.
Настройки сессии прописал следующим образом:
'components'=>array(
'session' => array(
'autoStart' => true,
/*'timeout' => 300,*/
'cookieMode' => 'only',
),
'user'=>array(
// enable cookie-based authentication
'allowAutoLogin'=>true,
),
Код identity стандартный:
class UserIdentity extends CUserIdentity
{
private $_id;
/**
* Authenticates a user.
* @return boolean whether authentication succeeds.
*/
public function authenticate()
{
$users=array(
// username => password
'admin'=>'admin',
);
if(!isset($users[$this->username]))
$this->errorCode=self::ERROR_USERNAME_INVALID;
elseif($users[$this->username]!==$this->password)
$this->errorCode=self::ERROR_PASSWORD_INVALID;
else {
$this->_id = $this->username;
$this->setState('id', $this->_id);
$this->errorCode=self::ERROR_NONE;
}
return !$this->errorCode;
}
public function getId()
{
return $this->_id;
}
}
Код самой авторизации тоже стандартный:
public function actionLogin()
{
$this->layout = '//layouts/simple';
$model=new LoginForm;
// 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()) {
$this->redirect(Yii::app()->createUrl('site/index'));
}
}
// display the login form
$this->render('login',array('model'=>$model));
}
Добавил логи после авторизации и в начало метода beforeAction:
Yii::log('Session id: ' . Yii::app()->session->sessionID, 'info');
Yii::log('User id:' . Yii::app()->user->getId(), 'info');
Yii::log('Is guest: ' . Yii::app()->user->isGuest, 'info');
Yii::log('Session: ' . print_r(Yii::app()->session, true), 'info');
Yii::log('User' . print_r(Yii::app()->user, true), 'info');
Вот что выводит сразу после авторизации:
В beforeAction же уже этих данных нет:
Даже наличие файла с сессии проверил. Файл есть, данные в нем тоже есть: