S
S
Sergey2015-03-12 20:11:52
Yii
Sergey, 2015-03-12 20:11:52

How to make authorization in yii2 through the database?

In the standard Users model, the basic template has an array:

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',
        ],
    ];

Registration is complicated for me in several stages, so I wrote my own models / controllers for this, but I want to leave the authorization standard, built into the functionality.
I already have a user_login database with id,mail, password columns. And a number of other tables that I receive during registration, but they are not needed here.
And here I have a number of questions:
1. Why read id at all? I have it, but what does it serve?
2. What are 'authKey' and 'accessToken' for? Should I add these columns to the database? And how to fill them with random values?
3. Where can I read about creating authorization in yii2? I don’t find it in the docks, there are only RBAC and ACF filters

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey, 2015-03-13
@butteff

In general stuck with crutches in the existing code.
in the findIdentity($id) method, by this id, I look for a record about the user, form an array, as a standard one, and override it with the values ​​of this array. This method checks the logged in user.
And for validation, I did the same in findByUsername($username)
Well, in the validatePassword($password) method, I had to add md5 password generation, that's all.
If anyone needs a file /public_html/models/Users.php with these methods, write - I will send it. Email in profile.

D
Dmitry Bay, 2015-03-12
@kawabanga

You have two very simple ways
1) install the advanced template, where user is the standard functionality that works with the database, you only need to enter the database settings and run migrations
2) https://github.com/dektrium/yii2-user install this extension.
It will be easier. than to make you a bicycle.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question