/**
  * Finds the Role model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  *
  * @param integer $id
  *
  * @return Role the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = Role::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
 /**
  * @param        $controller
  * @param mixed  $action
  * @param null   $role_id
  *
  * @return bool
  */
 public static function isAuth($controller, $action = null, $role_id = null)
 {
     if (Yii::$app->user->isGuest) {
         return false;
     }
     if ($role_id != null) {
         /**@var $role Role */
         $role = Role::findOne(['id' => $role_id]);
     } else {
         /**@var $user User */
         $user = Yii::$app->user->identity;
         if ($user->getRole()->exists()) {
             /**@var $role Role */
             $role = $user->getRole()->one();
         } else {
             $role = new Role();
         }
     }
     if ($role === null) {
         return false;
     }
     if ($role->is_backend_login != 1) {
         return false;
     }
     $permissions = Json::decode($role->permissions);
     if ($permissions != null) {
         if (in_array($controller, array_keys($permissions))) {
             if ($action != null) {
                 $valid = false;
                 foreach ($permissions as $controllerName => $actions) {
                     if ($controllerName != $controller) {
                         continue;
                     } else {
                         if (!is_array($action)) {
                             $action = [$action];
                         }
                         foreach ($action as $item) {
                             if (in_array($item, array_keys($actions))) {
                                 if ($actions[$item] == 1) {
                                     $valid = true;
                                     break;
                                 }
                             } else {
                                 $valid = true;
                                 break;
                             }
                         }
                     }
                 }
                 return $valid;
             } else {
                 $valid = false;
                 foreach ($permissions as $controllerName => $actions) {
                     if ($controllerName != $controller) {
                         continue;
                     } else {
                         foreach ($actions as $item) {
                             if ($item == 1) {
                                 $valid = true;
                                 break;
                             }
                         }
                     }
                 }
                 return $valid;
             }
         } else {
             return true;
         }
     } else {
         return true;
     }
 }