Failing to get user permissions yii2

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.

Upvotes: 0

Views: 1202

Answers (2)

I've managed to fix it by doing next:

    function conseguirPermisos($nombreP){

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

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

                if (\yii\helpers\ArrayHelper::getValue($permisos, $nombreP)) {

                    return true;

                } 
                    else {

                            return false;

                    }
            }

Upvotes: 0

Clyff
Clyff

Reputation: 4076

You can use the getPermissionsByUser() method. Example:

$userId = Yii::$app->user->getId();
var_dump(Yii::$app->authManager->getPermissionsByUser($userId));

Upvotes: 2

Related Questions