about Yii2 basic Login demo

Hi all

i am newcomer for Yii2.

When i learn Yii2 basic login demo,i was confused about:

in \yii2\basic\models\User.php:

public function validatePassword($password)

{

return $this->password === $password;

}

well,when and where($this->password)was filled out?

thx

:)

when login form is submitted

sitecontroller loginaction u will find below code.




if ($model->load(Yii::$app->request->post()) && $model->login()) {



so it will goes to loginform model.

in models/loginform




in rules: ['password', 'validatePassword'],




public function validatePassword($attribute, $params)

    {

        if (!$this->hasErrors()) {

            $user = $this->getUser();


            if (!$user || !$user->validatePassword($this->password)){

                $this->addError($attribute, 'Incorrect username or password.');

            }

        }

    }



in models/user




public function validatePassword($password)

    {

        return $this->password === $password;//here $this->password is stored db password and $password is pass from loginform.

    }



here compare string with data types.

In the basic template, the usernames and passwords are stored in the $users array in app\models\User, when the user attempts to log in, the User object is populated from the $users array and that is where the password comes from.

thx,your code flow is so clear。

but as mentioned:

“return $this->password === $password;//here $this->password is stored db password and $password is pass from loginform.”,

password is from loginform,my question is:

which step $this->password is filled?

which method?

thx again。

thx。

as mentioned:

“the User object is populated from the $users array and that is where the password comes from.”

can U explain some more details?

see this code:(in basic\models\User.php)

class User extends \yii\base\Object implements \yii\web\IdentityInterface

{

public $id;


public $username;


public $password;


public $authKey;


public $accessToken;





private static $users = [


    '100' => [


        'id' => '100',


        'username' => 'admin',


        'password' => 'admin',


        'authKey' => 'test100key',


        'accessToken' => '100-token',


    ],


    '101' => [


        'id' => '101',


        'username' => 'demo',


        'password' => 'demo',


        'authKey' => 'test101key',


        'accessToken' => '101-token',


    ],


];

。。。。。。

////

there are two members in $users( admin and demo )

thx again。

the answer refers to:

http://www.yiichina.com/tutorial/332

thx all。

It is in the function


public static function findByUsername($username)

{

    foreach (self::$users as $user) {

       if (strcasecmp($user['username'], $username) === 0) {

           return new static($user);

        }

     }

    return null;

}

This is called when the user attempts to login. If their username exists in the $users array, a new User is returned which is populated by the contents of the array. The syntax new static($user) means create a new type of the current class using the data in the given array to populate the fields.