This is a question about using RBAC in Yii2.
So far, I have found that it works reasonably well and satisfactorily, but there is one key function that I am missing: the Yii2 Rules ability to provide “feedback” is similar to the way Yii2 Rules set error messages to explain why the check doesn’t done. I am looking for a way to provide some feedback on why permission was not granted.
In particular, the can () method returns a boolean type, which is good, but when checking the permission, we have no idea why the user was not specifically granted this specific permission.
To give a more practical example. Say we are trying to determine if the current user can post a comment. Usually we will do something like this:
if (Yii::$app->user->can('postComment',['comment'=>$comment])) { $comment->post(); } else { throw new ForbiddenHttpException('Sorry m8, you cant do this. No idea why tho!'); }
It works fine, but as shown in the example, we really don't know why the user cannot post a comment. There can be any number of reasons, for example, because the stream is blocked or because they do not have permission to publish in a certain category or because they do not have a sufficiently high reputation, etc. But we want to tell the user why! So my question is: how do we get this feedback from Yii2 RBAC?
source share