Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/252.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Yii2框架无法使用自定义数据库登录_Php_Yii2_Yii2 Basic App - Fatal编程技术网

Php Yii2框架无法使用自定义数据库登录

Php Yii2框架无法使用自定义数据库登录,php,yii2,yii2-basic-app,Php,Yii2,Yii2 Basic App,新手和学习者。我有一个自定义的用户数据库,我想用它作为我的登录凭据,幸运的是我能够查询它,但是在我的公共静态函数validatePassword($password)上,当不在对象上下文中时,我得到了一个Using$this 这是我的密码 public static function findByUsername($username) { $user = self::find()->where(['USR_USERNAME' => $username, 'USR_STATUS'

新手和学习者。我有一个自定义的用户数据库,我想用它作为我的登录凭据,幸运的是我能够查询它,但是在我的公共静态函数validatePassword($password)上,当不在对象上下文中时,我得到了一个Using$this

这是我的密码

public static function findByUsername($username)
{
   $user = self::find()->where(['USR_USERNAME' => $username, 'USR_STATUS' => '1'])->one();
   return new static($user);
}

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

/**
 * {@inheritdoc}
 */


public static function validatePassword($password)
{
    return $this->password === $password;
}
这是我的错误截图

所有的脚本都是使用gii模块生成的,所以我不知道为什么会产生这个错误,而且在观看教程时,我复制了他们的代码,但没有在我的端工作。请帮忙,谢谢

这里需要的是DbUser.php代码

<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "{{%RBAC_USERS}}".
 *
 * @property string $USR_UID
 * @property string $USR_USERNAME
 * @property string $USR_PASSWORD
 * @property string $USR_FIRSTNAME
 * @property string $USR_LASTNAME
 * @property string $USR_EMAIL
 * @property string $USR_DUE_DATE
 * @property string $USR_CREATE_DATE
 * @property string $USR_UPDATE_DATE
 * @property int $USR_STATUS
 * @property string $USR_AUTH_TYPE
 * @property string $UID_AUTH_SOURCE
 * @property string $USR_AUTH_USER_DN
 * @property string $USR_AUTH_SUPERVISOR_DN
 */
class DbUsers extends \yii\db\ActiveRecord implements \yii\web\IdentityInterface

{

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

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['USR_UID', 'USR_DUE_DATE'], 'required'],
            [['USR_DUE_DATE', 'USR_CREATE_DATE', 'USR_UPDATE_DATE'], 'safe'],
            [['USR_STATUS'], 'integer'],
            [['USR_UID', 'USR_AUTH_TYPE', 'UID_AUTH_SOURCE'], 'string', 'max' => 32],
            [['USR_USERNAME', 'USR_EMAIL'], 'string', 'max' => 100],
            [['USR_PASSWORD'], 'string', 'max' => 128],
            [['USR_FIRSTNAME', 'USR_LASTNAME'], 'string', 'max' => 50],
            [['USR_AUTH_USER_DN', 'USR_AUTH_SUPERVISOR_DN'], 'string', 'max' => 255],
            [['USR_UID'], 'unique'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'USR_UID' => Yii::t('app', 'Uid'),
            'USR_USERNAME' => Yii::t('app', 'Username'),
            'USR_PASSWORD' => Yii::t('app', 'Password'),
        ];
    }


    /**
     * {@inheritdoc}
     */
    public static function findIdentity($id)
    {
        throw new NotSupportedException();
    }

    /**
     * {@inheritdoc}
     */
    public static function findIdentityByAccessToken($token, $type = null)
    {

        throw new NotSupportedException();
    }

    /**
     * Finds user by username
     *
     * @param string $username
     * @return static|null
     */
    public function getAuthKey()
    {
        throw new NotSupportedException();
    }

    /**
     * {@inheritdoc}
     */
    public function validateAuthKey($authKey)
    {
        throw new NotSupportedException();
    }

    public static function findByUsername($username, $password)
    {
        $user = self::find()->where(['USR_USERNAME' => $username, 'USR_STATUS' => '1'])->one();

        return $user;


    }

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

    /**
     * {@inheritdoc}
     */


    public static function validatePassword($password)
    {
        return $this->password === $password;
    }


}

在静态函数中,不能使用$this。使validatePassword()函数非静态。

在静态函数中,不能使用$this。使validatePassword()函数非静态。

您不能将
$this
用于静态函数,请更改为

public function validatePassword($password)
{
   return $this->password === $password;
}

您不能将
$this
与静态功能一起使用,请更改为

public function validatePassword($password)
{
   return $this->password === $password;
}

请更新您的问题,添加DbUsers模型代码。 执行方法
validatePassword()
的位置?
当然,您不能在静态方法中使用$this,使该方法不是静态的

请更新您的问题,添加DbUsers模型代码。 执行方法
validatePassword()
的位置?
当然,你不能在静态方法中使用$this,让方法不是静态的

我回答我自己的问题。我在find中添加了以下代码

  public static function findByUsername($username, $password)
    {
        $user = self::find()->where(['USR_USERNAME' => $username, 'USR_STATUS' => '1'])->one();

        if (hash('sha256', $password) == $user->USR_PASSWORD)
        {
            return new static($user);
        }


    }
现在,这将验证密码是否与sha256加密中的数据库匹配,然后在我的validatePassword方法中,我将其修改为simple always return true,因为在LoginForm中,他们总是首先搜索用户名,然后检查密码验证,因此,如果用户名/密码错误,它将立即验证用户不存在

 public static function validatePassword($password)
    {
        return true;
    }

我觉得Yi2很难,因为缺乏文档。我回答我自己的问题。我在find中添加了以下代码

  public static function findByUsername($username, $password)
    {
        $user = self::find()->where(['USR_USERNAME' => $username, 'USR_STATUS' => '1'])->one();

        if (hash('sha256', $password) == $user->USR_PASSWORD)
        {
            return new static($user);
        }


    }
现在,这将验证密码是否与sha256加密中的数据库匹配,然后在我的validatePassword方法中,我将其修改为simple always return true,因为在LoginForm中,他们总是首先搜索用户名,然后检查密码验证,因此,如果用户名/密码错误,它将立即验证用户不存在

 public static function validatePassword($password)
    {
        return true;
    }

我发现Yi2很难,因为缺少文档。

我尝试过,但现在在获取未知属性时出现了一个新错误:app\models\DbUsers::password@MiksMeister. 使用
Login
action code和
DbUsers
model更新您的问题。我尝试过,但现在在获取未知属性时出现新错误:app\models\DbUsers::password@MiksMeister. 使用
Login
action code和
DbUsers
model更新您的问题。如果您使用
sha256
对密码进行哈希运算,尤其是在没有任何盐的情况下,这是非常糟糕的迹象。您应该使用密码管理。谢谢您的帮助。这是我第一次使用sha256@rob006,最好的加盐方法是什么?基本上,数据库已经使用了sha256加密,所以据我所知,将其哈希到sha256已经可以了。如果您使用
sha256
对密码进行哈希,这是一个非常糟糕的迹象,尤其是在没有任何盐的情况下。您应该使用密码管理。谢谢您的帮助。这是我第一次使用sha256@rob006,最好的加盐方法是什么?基本上,数据库已经在sha256加密中,所以在我的理解中,将其散列到sha256已经可以了