G
G
GONJY MONJY2020-07-02 01:27:08
Yii
GONJY MONJY, 2020-07-02 01:27:08

How to create RBAC role using migrations in yii?

Hello!

To create a role, use the following code:

$role = Yii::$app->authManager->createRole('admin');
$role->description = 'Администратор';
Yii::$app->authManager->add($role);


I need to create an admin role using MIGRATION. I already have the code, but the console throws an error after activating the migration.

Here is the code in the migration file:
public function safeUp()
    {
        $this->insert('auth_item',[
            'name' => 'admin',
            'type' => 1,
            'descryption' => 'Administrator',
        ]);
    }


Please explain what I wrote wrong and how it was necessary to do it right!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Michael, 2020-07-02
@GONJY_MONJY

You must create roles in migrations in the same way as you do in the first way - through authManager. Here in the docks . To do this, check that this component is present in the console config

T
TsVadim, 2020-07-02
@TsVadim

It would be nice to point out the error you are seeing. But I'll assume...
To start with, check whether "authManager" is configured in the console to add a role through it.
Check if the role was created earlier...
That 's the idea to do it right...
But in the case when we have permission added during the development of the project, this option can become a headache, since each time you will need to reset the permissions in order to added new...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question