src/Controller/BaseController.php
public function canAccess($permissionKeys, $fullAccessKey, $errorMsg, $defaultUrl, $redirect = false)
    {
        if ($this->getUser()->isGuest) {
            return $this->getUser()->loginRequired();
        }

        if ($this->getPermissionManager()->canAccess($fullAccessKey)) {
            return true;
        }

        if (!is_array($permissionKeys)) {
            $permissionKeys = [$permissionKeys];
        }

        foreach ($permissionKeys as $permissionKey) {
            if ($this->getPermissionManager()->canAccess($permissionKey)) {
                return true;
            }
        }

        if ($redirect) {
            $this->flashError($errorMsg);

            $request = $this->getRequest();
            $referrerUrl = $request->referrer;

            $redirectUrl = ($referrerUrl == $request->url || is_null($referrerUrl)) ?
                $defaultUrl : $referrerUrl;

            $this->redirect($redirectUrl)->send();
            Yii::$app->end(); //this enforces the redirect
        }
        return false;
    }
public function canAccess($permissionKeys, $fullAccessKey, $errorMsg, $defaultUrl, $redirect = false)
    {
        if ($this->getUser()->isGuest) {
            return $this->getUser()->loginRequired();
        }

        if ($this->getPermissionManager()->canAccess($fullAccessKey)) {
            return true;
        }

        if (!is_array($permissionKeys)) {
            $permissionKeys = [$permissionKeys];
        }

        foreach ($permissionKeys as $permissionKey) {
            if ($this->getPermissionManager()->canAccess($permissionKey)) {
                return true;
            }
        }

        if ($redirect) {
            $this->flashError($errorMsg);

            $request = $this->getRequest();
            $referrerUrl = $request->referrer;

            $redirectUrl = ($referrerUrl == $request->url || is_null($referrerUrl)) ?
                $defaultUrl : $referrerUrl;

            $this->redirect($redirectUrl)->send();
            Yii::$app->end(); //this enforces the redirect
        }
        return false;
    }
Handle access based on permissions
@author Taiwo Ladipo <ladipotaiwo01@gmail.com>
@param $permissionKeys
@param bool $redirect
@param $fullAccessKey
@param $errorMsg
@param $defaultUrl
@return bool|Response
@throws \yii\base\ExitException
@throws \yii\web\ForbiddenHttpException