A
A
AUN2015-03-22 00:49:22
Yii
AUN, 2015-03-22 00:49:22

Yii2 authentication. How to fix?

Hey!
I'm reading the guide, the basic template, I've created a users table, and I'm trying to authenticate. authentication passes (???), but when going to the main page after successful completion of authentication, instead of "login", "logout" should be displayed, this does not happen because Yii::$app->user->isGuest = true.
If you use the standard template model, then everything is ok. That is, the error, as I understand it, is in the model.
I don't know what to think anymore..

<?php

namespace app\modules\admin\models;

use Yii;
use yii\base\Exception;

/**
 * This is the model class for table "vkl_users".
 *
 * @property integer $id
 * @property string $email
 * @property string $password
 * @property string $token
 * @property integer $auth
 * @property integer $ban
 * @property string $create
 * @property string $update
 */
class Users extends \yii\db\ActiveRecord implements \yii\web\IdentityInterface
{

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'vkl_users';
    }

    /**
     * @inheritdoc
     */

    public function rules()
    {
        return [
            [['email', 'token', 'auth', 'ban', 'create'], 'required'],
            [['auth', 'ban'], 'integer'],
            [['create', 'update'], 'safe'],
            [['email', 'token'], 'string', 'max' => 50],
            [['password'], 'string', 'max' => 100],
            [['email'], 'unique']
        ];
    }

    public function scenarios()
    {
        $scenarios = parent::scenarios();
        $scenarios['register'] = ['email', 'password'];//массовое присвоение
        return $scenarios;
    }

    //token is the hash of email and salt
    public function beforeSave($insert)
    {
        if (parent::beforeSave($insert)) {
            if ($this->isNewRecord) {
                $this->token = hash('sha256',$this->email.Yii::$app->params['salt']);
                $this->password= hash('sha256',$this->password.Yii::$app->params['salt']);
                $this->auth=0;
                $this->ban=0;

            }
            return true;
        }
        return false;
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'email' => 'Email',
            'password' => 'Password',
            'token' => 'Token',
            'auth' => 'Auth',
            'ban' => 'Ban',
            'create' => 'Create',
            'update' => 'Update',
        ];
    }

    public static function findIdentity($id)
    {
        return static::findOne($id);
    }

    /**
     * @inheritdoc
     */
    public static function findIdentityByAccessToken($token, $type = null)
    {
//        $users=self::find()->all();
//        foreach ($users as $user) {
//            if ($user['token'] === $token) {
//                return new static($user);
//            }
//        }
        return static::findOne(['access_token' => $token]);
    }

    /**
     * Finds user by username
     *
     * @param  string      $username
     * @return static|null
     */
    public static function findByUsername($username)
    {
        return self::find()->where('email=:mail',['mail'=>$username])->one();
    }

    /**
     * @inheritdoc
     */
    public function getId()
    {
        return $this->id;
    }

//    protected $authKey;

    /**
     * @inheritdoc
     */
    public $auth_key;
    public function getAuthKey()
    {
//        if (empty($this->auth_key)){
//            $this->auth_key=hash('sha256',Yii::$app->params['salt'].$this->email.Yii::$app->params['salt']);//authKey like hash of concatinating of email+password
//        }
        return $this->auth_key;
//        throw new Exception($this->auth_key);
    }

    /**
     * @inheritdoc
     */
    public function validateAuthKey($authKey)
    {
        return $this->getAuthKey() === $authKey;
//        throw new Exception("validate auth exception");
    }

    /**
     * Validates password
     *
     * @param  string  $password password to validate
     * @return boolean if password provided is valid for current user
     */
    public function validatePassword($pass)
    {
//       var_dump(hash('sha256',$pass.Yii::$app->params['salt']).'___'.$this->password);exit;
        return hash('sha256',$pass.Yii::$app->params['salt'])==$this->password;
    }
}

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
A
AUN, 2015-03-22
@AUN

And the jamb was not in the model. in the config it was necessary to register the path to the model:

'user' => [
            'identityClass' => 'app\modules\admin\models\Users',
            'enableAutoLogin' => true,
        ],

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question