Beispiel #1
0
 /**
  * 删除角色的授权任务
  * @author lixupeng
  * @param  type $id
  * @throws Exception
  */
 public function actionDeleteAssignItems($id)
 {
     $model = self::findModel($id);
     $items = Yii::$app->request->post('authItems');
     if (!is_array($items)) {
         throw new Exception('Invalid request.Params has Error. Please do not repeat this request again.');
     }
     if ($items && models\RbacRoleTask::deleteRoleTask($id, $items)) {
         echo '删除授权成功';
     } else {
         throw new Exception('删除授权失败');
     }
 }
 public function actionUnAssignRole($role_id, $task_id)
 {
     if (preg_match('/^\\d+$/', $task_id) && preg_match('/^\\d+$/', $role_id)) {
         models\RbacRoleTask::deleteRoleTask($role_id, $task_id);
     } else {
         throw new Exception('params is not safe!');
     }
     return $this->redirect(['/rbac/authtask/related-role', 'id' => $task_id]);
 }
Beispiel #3
0
 /**
  * 获取用户所有的操作、自定义的授权项,缓存半小时,用于防止缓存被集中请求
  * @param  int $userId
  * @param  boolean $useCache
  * @return array
  */
 public static function getUserOperationAuthItems($userId, $useCache = true)
 {
     //获取RBAC权限使用的cache名字
     $cacheComponents = Yii::$app->getModule('rbac')->cacheComponents;
     $cachekey = 'UserAuthItems_userId_' . $userId;
     $items = [];
     //所有权限
     // 如果使用cache优先从cache读取
     if ($useCache) {
         $items = Yii::$app->{$cacheComponents}->get($cachekey);
     }
     // 如果未使用cache或从cache中未读出值,则到数据库读取,并缓存起来。
     if (!$useCache || $items === false) {
         // 先获得用户的所有角色
         $roles = RbacUserRole::getUserRoles($userId, true);
         if (empty($roles)) {
             return $items;
         }
         //获取该用户所有角色的任务
         $taskIds = RbacRoleTask::getTaskAuthorized($roles);
         $authItems = [];
         ////获取任务的所有权限
         if (is_array($taskIds) && count($taskIds) > 0) {
             //获取任务的所有权限
             $authItems = RbacTaskItems::getTaskAuthorized($taskIds);
         }
         foreach ($authItems as $v) {
             $items[$v] = $v;
         }
         Yii::$app->{$cacheComponents}->set($cachekey, $items, 1800);
     }
     return $items;
 }