Yii Framework: как настроить авторизацию и аутентификацию?

Друзья, уже 2 день не могу разобраться с авторизацией. Почему не получается залогиниться?
Вот код модельки:
class User extends CActiveRecord
{
    const SCENARIO_REGISTER = 'register';
    const SCENARIO_LOGIN = 'login';

    private $_identity;

    public $password_repeat;

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'user';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('username, password', 'required'),
            array('username', 'unique'),
            array('username', 'length', 'min'=>5, 'max'=>30),
            array('username', 'match', 'pattern'=>'/^[A-z][\w]+$/'),
            array('password', 'length', 'min'=>6, 'max'=>30),
            array('password', 'authenticate', 'on'=>'login'),
            array('password_repeat, email', 'required', 'on'=>'register'),
            array('password_repeat', 'length', 'min'=>6, 'max'=>30),
            array('password', 'compare', 'compareAttribute'=>'password_repeat', 'on'=>'register'),
            array('email', 'email', 'on'=>'register'),
            array('email', 'length', 'min'=>6, 'max'=>30),
            array('email', 'filter', 'filter'=>'mb_strtolower'),
            array('id, username, password, email, dtime_register, cookie', 'safe', 'on'=>'search'),

        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        return array();
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'username' => 'Username',
            'password' => 'Password',
            'password_repeat' => 'Repeat password',
            'email' => 'Email',
        );
    }

    public function search()
    {
        // @todo Please modify the following code to remove attributes that should not be searched.

        $criteria=new CDbCriteria;

        $criteria->compare('id',$this->id);
        $criteria->compare('username',$this->username,true);
        $criteria->compare('password',$this->password,true);
        $criteria->compare('email',$this->email,true);
        $criteria->compare('dtime_register',$this->dtime_register,true);
        $criteria->compare('cookie',$this->cookie,true);

        return new CActiveDataProvider($this, array(
            'criteria'=>$criteria,
        ));
    }

    /**
     * Returns the static model of the specified AR class.
     * Please note that you should have this exact method in all your CActiveRecord descendants!
     * @param string $className active record class name.
     * @return User the static model class
     */
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    protected function beforeSave() {
        if(parent::beforeSave()) {
            if($this->getIsNewRecord()) {
                $this->dtime_register = time();
                $this->password = md5($this->password);
            }

            return true;
        }
        return false;
    }

    public function authenticate() {
        if(!$this->hasErrors()) {
            $this->_identity = new UserIdentity($this->username, $this->password);
            if(!$this->_identity->authenticate()) {
                $this->addError('password', 'Incorrect username or password');
            }
        }
    }

    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 = 3600*24*30;
            Yii::app()->user->login($this->_identity, $duration);
            return true;
        }
        else {
            return false;
        }
    }

    public function logout() {
       // ...
    }

}

UserIdentity:
class UserIdentity extends CUserIdentity
{
    private $_id;

    public function authenticate()
    {
        $record = User::model()->findByAttributes(array('username'=>$this->username));
        if($record === null)
            $this->errorCode=self::ERROR_USERNAME_INVALID;
        else if(md5($this->password) !== $record->password)
            $this->errorCode=self::ERROR_PASSWORD_INVALID;
        else
        {
            $this->_id = $record->id;
            $this->setState('username', $record->username );
            $this->errorCode=self::ERROR_NONE;
        }
        return !$this->errorCode;
    }

    public function getId()
    {
        return $this->_id;
    }
}

и сам экшен а контроллере:
public function actionLogin() {

        $user = new User(User::SCENARIO_LOGIN);

        if(isset($_POST['User'])) {
            $user->attributes = $_POST['User'];
            if($user->login()) {
                $this->redirect('/');
            }
        }

        $this->render('login', array('model'=>$user));
    }
  • Вопрос задан
  • 7471 просмотр
Пригласить эксперта
Ответы на вопрос 4
AMar4enko
@AMar4enko
Писал-писал, потом плюнул. Все плохо в этом коде. Не знаю, чем вам помочь.
Ответ написан
Комментировать
fornit1917
@fornit1917
Неправильная проверка пароля. Если у вас в модельке в поле password реальное значение пароля, то в Identity вы его хэшируете и с собой же сравниваете.
Если в модельке хэш, то опять fail: вы его снова хэшируете и с собой же сравиваете.
Ответ написан
Комментировать
У вас плохой вопрос. Из разряда "почему не работает сайт?", ведь конкретики никакой. Подключите xdebug или даже тем же var_dump найдите где ошибка и спросите совета как ее решить. А так вы выложили свой код, и хотите что бы мы сами нашли где у вас ошибка? Это потому что вам лень или вы не умеете искать?

Что вообще происходит после попытки логина? Метод login() какое значение возвращает?
Ответ написан
Комментировать
AMar4enko
@AMar4enko
Народ, простите за вопрос на отвлеченную тему, но xdebug нынче непопулярен?

$this->password = CPasswordHelper::hashPassword($this->password);

и

md5($this->password) !== $record->password
как вообще друг с другом соотносятся?

Для тех, кто в танке:
У вас хеш для пароля при сохранении генерится одним способом, а хеш для введенного пользователем пароля берется как md5. Они никогда одинаковыми не будут.
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы