Rbac Auth Test To Check For Updateownpost Not Working

Hello,

Followed this…

http://www.yiiframework.com/doc-2.0/guide-authorization.html#role-based-access-control-rbac

Trying to test this…

if (\Yii::$app->user->can(‘updateOwnPost’, [‘post’ => $model->id])) {

echo "Update Own Post";

}

in AuthorRule I changed

    return isset($params['post']) ? $params['post']->createdBy == $user : false;

to

   return isset($params['post']) ? $params['post']->createdBy == $params['user'] : false;

because there is no $user??

Still getting getting error of this…

Trying to get property of non-object

basic/rbac/AuthorRule.php

return isset($params[‘post’]) ? $params[‘post’]->createdBy == $params[‘user’] : false;

If i dump params from AuthorRule It is getting the params

Array ( [post] => 200 [user] => 1 )

Where does the ->createdBy come from? Could this be causing the problem?

Regards,

Dennis

Hello rawlly.

You are passing the model’s id attribute instead of model. use the below code




if (\Yii::$app->user->can('updateOwnPost', ['post' => $model])) {

    echo "Update Own Post";

}



Hi!

It is not working for me too!

I also do as the [color="#006400"]THIS GUIDE[/color]

But I find isset($params[‘post’]) is always FALSE and ver_dump($params[‘post’]) is always NULL.

PS. I have modify the code as Ahamed Rifaideen said.

Can anybody help me?

Thanks!

Your current code?

Yes.

Any difference?

BTW, I run ‘composer update’ everyday.

After commented yii2-admin at common\config\main.php, it works!