yiisoft / yii2

Yii 2: The Fast, Secure and Professional PHP Framework
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
14.24k stars 6.91k forks source link

Assign method uses time() for timestamp field #18141

Closed Aresenka closed 4 years ago

Aresenka commented 4 years ago

What steps will reproduce the problem?

I'm trying to assign role to user with both of these methods: 1) $userRole = Yii::$app->authManager->getRole($role); Yii::$app->authManager->assign($userRole, $user->getId()); 2) $auth = new DbManager(); $auth->init(); $userRole = $auth->getRole($role); $auth->assign($userRole, $user->getId());

What is the expected result?

New line in auth_assignment table, created by default yii migrate --migrationPath=@yii/rbac/migrations console command

What do you get instead?

Error: SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value: '1593448642' for column 'created_at' at row 1 The SQL being executed was: INSERT INTOauth_assignment(user_id,item_name,created_at) VALUES (32, 'admin', '1593448642')

Additional info

As I can see, it is $assignment = new Assignment([ 'userId' => $userId, 'roleName' => $role->name, 'createdAt' => time(), ]); code in assign method of DbManager class. auth_assignment table described created_at field as timestamp, so it is incorrect to insert integer in timestamp field, isn't it?

darkdef commented 4 years ago

@Aresenka What database are you using?

Aresenka commented 4 years ago

@darkdef mysql Ver 14.14 Distrib 5.7.30, for Linux (x86_64)

darkdef commented 4 years ago
  1. I'm install clean yii2-advanced and do:
    yii migrate
    yii migrate --migrationPath=@yii/rbac/migrations
    yii my-rbac/init
  2. Create user with id=1 and after in my controller:
        $userId = 1;
        $role = 'admin';
        $auth = new DbManager();
        $auth->init();
        $userRole = $auth->getRole($role);
        $auth->assign($userRole, $userId);

Did you do the same?

Aresenka commented 4 years ago

@darkdef no, insted of my-rbac/init I've used c-rbac/init Is that a fault?

darkdef commented 4 years ago

my-rbac/init - it's my test code for create rules (role, permission and etc.) for example from:

        $auth = Yii::$app->authManager;
        //Create our 4 Roles
        $admin = $auth->createRole('admin');
        $auth->add($admin);
Aresenka commented 4 years ago

Yeah, sorry. It works ok: all roles and permissions are created. But when I try to assign any role to any user - it causes error I wrote before.

darkdef commented 4 years ago

Please show structure of table auth_assignment (dump) And you version of yii?

Aresenka commented 4 years ago

It's strange, but at fresh installed app it is int type of created_at field of auth_assignment table. Maybe it was an mistake from my side, but now it has been a migration that altered the table, so I can't just dump it to show what is going on. So, if I'll catch that situation again - I'll reopen the issue. Sorry.