Example #1
0
 public function updateUserGroupAccess($get, $post)
 {
     $user_group_id = $get['user_group_id'] ?: $post['user_group_id'];
     if (Router::isPost()) {
         $ex = Model::transaction(function () use($post, $user_group_id) {
             $con = "user_group_id = '" . addslashes($user_group_id) . "'";
             UserGroupAuth::deleteWhere(0, $con);
             $data = array();
             if ($post['act_bids']) {
                 foreach ($post['act_bids'] as $act_id) {
                     $data[] = array('user_group_id' => $user_group_id, 'action_id' => $act_id, 'type' => UserGroupAuth::TYPE_BLACK);
                 }
             }
             if ($post['act_wids']) {
                 foreach ($post['act_wids'] as $act_id) {
                     $data[] = array('user_group_id' => $user_group_id, 'action_id' => $act_id, 'type' => UserGroupAuth::TYPE_WHITE);
                 }
             }
             if ($data) {
                 UserGroupAuth::insertMany($data);
             }
         });
         return new Result(!$ex ? '操作成功' : $ex->getMessage(), !$ex, null, Router::getUrl('UserGroup'));
     }
     if (!$user_group_id) {
         $tmp = UserGroup::find()->one();
         $user_group_id = $tmp->id;
     }
     $act_list = AccessAction::find()->all(true);
     $act_ids = array_column($act_list, 'id');
     //user_group values
     $w_user_group_values = $b_user_group_values = array();
     if ($act_ids) {
         $user_group_values = UserGroupAuth::find('action_id IN ? AND user_group_id = ?', $act_ids, $user_group_id)->all(true);
         foreach ($user_group_values as $val) {
             if ($val['type'] == UserGroupAuth::TYPE_WHITE) {
                 $w_user_group_values[$val['action_id']] = $val;
             } else {
                 $b_user_group_values[$val['action_id']] = $val;
             }
         }
     }
     array_orderby($act_list, 'desc', SORT_DESC);
     $white_list = $black_list = array();
     foreach ($act_list as $act) {
         $a = $this->convertPathToArray('全部/' . $act['desc'], array($act['id'], $act['uri'], !!$w_user_group_values[$act['id']]));
         $white_list = array_merge_recursive($white_list, $a);
         $b = $this->convertPathToArray('全部/' . $act['desc'], array($act['id'], $act['uri'], !!$b_user_group_values[$act['id']]));
         $black_list = array_merge_recursive($black_list, $b);
     }
     return array('user_group_id' => $user_group_id, 'auth_list' => array($white_list, $black_list), 'user_group_list' => UserGroup::find()->all());
 }
Example #2
0
 /**
  * 获取所有权限列表
  * @description 这里为了强制后台权限更新之后,刷新用户最新权限,
  * 因此不做cache
  * @param $user_group_id
  * @param int $type 白名单0,黑名单1
  * @return mixed
  */
 private static function getActionList($user_group_id, $type = 0)
 {
     $tmp = UserGroupAuth::find('user_group_id = ? AND type = ?', $user_group_id, $type)->all(true);
     $action_id_list = array_column($tmp, 'action_id');
     $action_list = AccessAction::find('id IN ?', $action_id_list)->all(true);
     $action_list = array_group($action_list, 'id', true);
     foreach ($tmp as $k => $item) {
         $tmp[$k]['uri'] = $action_list[$item['action_id']]['uri'];
     }
     return $tmp;
 }