/**
  * 绑定角色与权限的对应关系
  * @param $roleId   角色ID
  * @param $pids 权限ID列表
  * @param $loginUser \liuxy\admin\models\AdminUser
  */
 public static function bind($roleId, $pids, $loginUser)
 {
     self::deteteAll(['role_id' => $roleId]);
     if (!empty($pids)) {
         $pids = explode(',', $pids);
         $pids = array_unique($pids);
         foreach ($pids as $pid) {
             if (!empty($pid)) {
                 $item = new RolePermission();
                 $item->isNewRecord = true;
                 $item->role_id = $roleId;
                 $item->permission_id = $pid;
                 $item->insert_by = $loginUser->username;
                 $item->insert();
                 if ($item->hasErrors()) {
                     Yii::error(VarDumper::dumpAsString($item->getErrors()), __METHOD__);
                 }
                 unset($item);
             }
         }
         /**
          * 清理角色下所对应用户的权限
          */
         foreach (AdminUserRole::find()->where(['role_id' => $roleId])->all() as $userRole) {
             AdminUser::clearPermission($userRole['user_id']);
         }
     }
 }
 /**
  * 清理用户缓存
  * @param $permissionId
  */
 public static function clearUserPermission($permissionId)
 {
     $roles = [];
     foreach (RolePermission::find()->where(['permission_id' => $permissionId])->all() as $rolePerm) {
         $roles[] = $rolePerm['role_id'];
     }
     foreach (AdminUserRole::find()->where(['role_id' => $roles])->all() as $userRole) {
         AdminUser::clearPermission($userRole['user_id']);
     }
 }
 /**
  * 获取角色对应的权限列表
  */
 public function actionPerm()
 {
     $data = [];
     $parentId = $this->get('parent', '#');
     $roleId = $this->get('id', 0);
     $generator = new JSTreeGenerator();
     if ($parentId != '#') {
         $len = strlen('node_');
         $id = substr($parentId, $len, strlen($parentId) - $len);
         $data = $generator->generator(Permission::getSub($id), true);
     } else {
         $data = $generator->getDefault();
     }
     if (!empty($roleId)) {
         $roles = RolePermission::findAllArray(['role_id' => $roleId]);
         Permission::setChecked($data, $roles);
     }
     $this->setResponseData('data', $data);
 }
 /**
  * 获取用户的权限列表
  * @param $userId
  */
 public static function getPermission($userId)
 {
     $_cache_key = self::CACHE_MY_PERMISSION . $userId;
     $myPermission = self::getCache()->get($_cache_key);
     if ($myPermission) {
         return $myPermission;
     }
     $roles = [];
     foreach (AdminUserRole::find()->where(['user_id' => $userId])->all() as $role) {
         $roles[] = $role->role_id;
     }
     $permissions = [];
     foreach (RolePermission::find()->where(['role_id' => $roles])->all() as $permission) {
         $permissions[] = $permission['permission_id'];
     }
     self::getCache()->set($_cache_key, $permissions);
     return $permissions;
 }