Getting current user permissions

Hello,

I’m triying to make certain menu items visible for users that have specific permissions but so far i have failed, what im doing is this:


function(){                                                

                if(Yii::$app->user->can('abrir caja'))

                {

                  return true;                                                    

                }

                  else

                  {

                   return false;

                  }                                                

     }

But im failing to get the user permissions is what i’m thinking, my question is how can i get the current user permissions? I have already checked that my users have their respective roles and permissions assigned.

I have changed my code to this:


function conseguirPermisos($nombreP){

                    $idUsuario = Yii::$app->user->getId();

                    $permisos=Yii::$app->authManager->getPermissionsByUser($idUsuario);

                    if (in_array($nombreP, $permisos)) {

                    return true;

                } else {

                    return false;

                }

            }

But it appears to be comparing wrongly

Usually you don’t need to do this kind of thing. “Yii::$app->user->can()” is just enough.

Check to see whether the user has those permissions that you believe to have assigned.




function conseguirPermisos($nombreP){

    $permisos = Yii::$app->authManager->getPermissionsByUser(Yii::$app->user->id);

    yii\helpers\VarDumper::dump($perisos);

    if (in_array($nombreP, array_keys($permisos))) {

        return true;

    } else {

        return false;

    }

}



Note that getPermissionsByUser() doesn’t return an array of strings (an array of permission names), but an array of Permission objects indexed by permission names.

http://www.yiiframework.com/doc-2.0/yii-rbac-managerinterface.html#getPermissionsByUser()-detail