简体   繁体   English

Yii框架中的身份验证

[英]Auth in Yii Framework

I dont know why authenticate() is not working in this simply example: 我不知道为什么authenticate()在此简单示例中不起作用:

This is one record from my User table 这是我的用户表中的一条记录

记录

model/User.php 模型/ user.php的

class User extends CActiveRecord
{
    public $id;
    public $name;
    public $lastname;
    public $login;
    public $password;
    public $date;

    public static function model($className = __CLASS__)
    {
        return parent::model($className);
    }

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

    public function attributeLabels()
    {
        return array(
            'id' => 'ID',
            'name' => 'Imię',
            'lastname' => 'Nazwisko',
            'login' => 'Login',
            'password' => 'Hasło',
            'date' => 'Data rejestracji',
        );
    }

    public function rules()
    {
        return array(
            array('name','required'),
            array('lastname','required'),
            array('login','required'),
            array('password','required'),
            array('date','default',
             'value'=>new CDbExpression('NOW()'),
             'setOnEmpty'=>false,'on'=>'insert')
            );
    }
}

model/UserIdentity.php 模型/ UserIdentity.php

class UserIdentity extends CUserIdentity
{
    private $_id;

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

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

}

controller/UserController.php 控制器/ UserController.php

[...]
public function actionLogin()
    {
        $username = 'janek';
        $password = '1234';
        $identity=new UserIdentity($username,$password);
        if($identity->authenticate())
        {
            echo $identity;
        }
        else
        {
            echo "NOT OK";
        }

    }
[...]

and when action login is requested then always are showing NOT OK. 并且当请求操作登录时,总是显示不正常。 I modify example from yii doc. 我从yii doc修改示例。

The problem is that the password in your database is not encrypted, according to the data you presented. 问题在于,根据您提供的数据,数据库中的密码未加密。

In this case, rewrite your check as 在这种情况下,请将支票改写为

else if($record->password !== $this->password)

instead of 代替

else if($record->password !== md5($this->password))

You should however, be saving your password in an encrypted manner. 但是,您应该以加密方式保存密码。 Of the many options, using md5 is not generally regarded as a safe option. 在许多选项中,使用md5通常不被视为安全选项。 Have a look at the official Yii documentation that shows how to use the Password helper library. 请查看Yii的官方文档,该文档显示了如何使用密码帮助程序库。 http://www.yiiframework.com/doc/guide/1.1/en/topics.auth http://www.yiiframework.com/doc/guide/1.1/en/topics.auth

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM