5

Yii でのユーザー認証をデータベースのユーザー テーブルに基づいて行いたいと考えています。これは私のユーザーモデルです:

<?php

namespace app\models;

use Yii;
use yii\base\NotSupportedException;
use yii\db\ActiveRecord;
use yii\helpers\Security;
use yii\web\IdentityInterface;


/**
 * This is the model class for table "user".
 *
 * @property integer $id
 * @property string $username
 * @property string $password
 * @property string $title
 */
class User extends \yii\db\ActiveRecord implements IdentityInterface
{


    public static function tableName()
    {
        return 'user';
    }



    /**
     * @inheritdoc
     */

    public function rules()
    {
        return [
            [['username', 'password'], 'required'],

            [['username', 'password'], 'string', 'max' => 100]           

        ];
    }



    /**
     * @inheritdoc
 */

    public function attributeLabels()
    {
        return [
            'id' => 'UserID',
            'username' => 'Username',
            'password' => 'Password',

        ];

    }   

    public static function findIdentity($id) {

        return static::findOne($id);
    }

    public static function findIdentityByAccessToken($token, $type = null) {
        return static::findOne(['access_token' => $token]);
    }

    public static function findByUsername ($username){

        return static::findOne(['username' => $username]);

    }
    public static function findbyPasswordResetToken($token)
    {
        $expire= \Yii::$app->params['user.passwordResetTokenExpire'];
        $parts = explode('_', $token);
        $timestamp = (int) end($parts);
        if ($timestamp + $expire < time()) {
            //token expired
            return null;

        }
        return static::findOne(['password_reset_token' => $token ]);


    }

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

    public function getAuthKey() {
        return $this->auth_key;
    }

    public function validateAuthKey($authKey) {
        return $this->getAuthKey() === $authKey;
    }

    public function validatePassword($password){
        $this->password_hash= Yii::$app->security->generatePasswordHash ($password);
    }
    public function generateAuthKey()
    {
        $this->auth_key = Yii::$app->security->generateRandomKey();
    }

    /**
     * Generates new password reset token
     */
    public function generatePasswordResetToken()
    {
        $this->password_reset_token = Yii::$app->security->generateRandomKey() . '_' . time();
    }

    /**
     * Removes password reset token
     */
    public function removePasswordResetToken()
    {
        $this->password_reset_token = null;
    }



}

しかし、ログインしようとすると、次のエラーが表示されます。

不明なプロパティの設定: app\models\User::password_hash

これは、siteController の actionLogin です。

public function actionLogin()
    {
        if (!\Yii::$app->user->isGuest) {
            return $this->goHome();
        }

        $model = new LoginForm();
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
            return $this->goBack();
        } else {
            return $this->render('login', [
                'model' => $model,
            ]);
        }
    }

これは LoginForm.php のコードです。

  public function validatePassword($attribute, $params)
    {
        if (!$this->hasErrors()) {
            $user = $this->getUser();

            if (!$user || !$user->validatePassword($this->password)) {
                $this->addError($attribute, 'Incorrect username or password.');
            }
        }
}

/**
 * Logs in a user using the provided username and password.
 * @return boolean whether the user is logged in successfully
 */
public function login()
{
    if ($this->validate()) {
        return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600*24*30 : 0);
    } else {
        return false;
    }
}

何が問題なのかわかりません、これを修正するのを手伝ってもらえますか?

4

2 に答える 2

6

これは、関数「validatePassword()」で割り当てられた列「password_hash」がデータベースに存在しないか、User モデルで宣言されていないためです。

パスワードハッシュがデータベースの「password」フィールドに保存されている場合は、「validatePassword()」を次のように変更します。

public function validatePassword($password)
{
    return $this->password === Yii::$app->security->generatePasswordHash ($password);
}
于 2015-04-01T13:21:01.397 に答える
0

解決策は次のとおりです1:ユーザーコントローラーで宣言します

     * @property string $password_hash
        class User extends ActiveRecord implements IdentityInterface
        {
        public $password;

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

  public function rules()
    {
        return [
      [['username','email','first_name','last_name','address','password','contact_no','role'], 'safe'],

                [['email'], 'unique'],
                [['email'], 'email'],

        ];
    }
        .
    .
    ....

         public function validatePassword($password)
        {
            return Yii::$app->security->validatePassword($password, $this->password_hash);
        }

2: LoginForm モデル ルール関数にこれを追加します。

['password', 'validatePassword'],

他のすべては問題ないようです。これがお役に立てば幸いです

于 2017-09-19T06:39:42.613 に答える