PHP-Yii:强制进行身份验证

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了PHP-Yii:强制进行身份验证脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

是否有任何办法强迫yii通过给定的用户名用户进行身份验证而不咨询数据库

我的应用程序将使用api登录,直到未编写该api,我们才能使用该应用程序.

由于这个api,我们没有用户模型,因此,尝试使用User :: model()时,身份验证过程会破裂

public function authenticate()
{
    if (strpos($this->username,"@")) {
        $user=User::model()->findByAttributes(array('email'=>$this->username));
    } else {
        $user=User::model()->findByAttributes(array('username'=>$this->username));
    }
    if($user===null)
        if (strpos($this->username,"@")) {
            $this->errorCode=self::ERROR_EMAIL_INVALID;
        } else {
            $this->errorCode=self::ERROR_USERNAME_INVALID;
        }
    else if(Yii::app()->getModule('user')->encrypting($this->password)!==$user->password)
        $this->errorCode=self::ERROR_PASSWORD_INVALID;
    else if($user->active==0&&Yii::app()->getModule('user')->loginNotActiv==false)
        $this->errorCode=self::ERROR_STATUS_NOTACTIV;
    else if($user->active==-1)
        $this->errorCode=self::ERROR_STATUS_BAN;
    else {
        $this->_id=$user->id;
        $this->errorCode=self::ERROR_NONE;
        $user->saveState($this);
    }
    return !$this->errorCode;
}

解决方法:

以下代码显示了yiic生成认UserIdentity.PHP.它根据需要使用硬编码的用户名和密码.

<?PHP

/**
 * UserIdentity represents the data needed to identity a user.
 * It contains the authentication method that checks if the provided
 * data can identity the user.
 */
class UserIdentity extends CUserIdentity
{
    /**
     * Authenticates a user.
     * The example implementation makes sure if the username and password
     * are both 'demo'.
     * In practical applications, this should be changed to authenticate
     * against some persistent user identity storage (e.g. database).
     * @return boolean whether authentication succeeds.
     */
    public function authenticate()
    {
        $users=array(
            // username => password
            'demo'=>'demo',
            '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->errorCode=self::ERROR_NONE;
        return !$this->errorCode;
    }
}

脚本宝典总结

以上是脚本宝典为你收集整理的PHP-Yii:强制进行身份验证全部内容,希望文章能够帮你解决PHP-Yii:强制进行身份验证所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: