Exemple #1
0
 /**
  * 获得权限列表
  * @param integer $uid 用户id
  * @param integer $type
  */
 protected function getAuthList($uid, $type)
 {
     static $_authList = array();
     //保存用户验证通过的权限列表
     $t = implode(',', (array) $type);
     if (isset($_authList[$uid . $t])) {
         return $_authList[$uid . $t];
     }
     if ($this->_config['AUTH_TYPE'] == 2 && isset($_SESSION['_AUTH_LIST_' . $uid . $t])) {
         return $_SESSION['_AUTH_LIST_' . $uid . $t];
     }
     //读取用户所属用户组
     $groups = $this->getGroups($uid);
     $ids = array();
     //保存用户所属用户组设置的所有权限规则id
     foreach ($groups as $g) {
         $ids = array_merge($ids, explode(',', trim($g['rules'], ',')));
     }
     $ids = array_unique($ids);
     if (empty($ids)) {
         $_authList[$uid . $t] = array();
         return array();
     }
     $map = array('id' => array('in', $ids), 'type' => $type, 'status' => 1);
     //读取用户组所有权限规则
     $rules = \think\Db::table($this->_config['AUTH_RULE'])->where($map)->field('condition,url,module')->select();
     //循环规则,判断结果。
     $authList = array();
     //
     foreach ($rules as $rule) {
         if (!empty($rule['condition'])) {
             //根据condition进行验证
             $user = $this->getUserInfo($uid);
             //获取用户信息,一维数组
             $condition = '';
             $command = preg_replace('/\\{(\\w*?)\\}/', '$user[\'\\1\']', $rule['condition']);
             @eval('$condition=(' . $command . ');');
             if ($condition) {
                 $authList[] = strtolower($rule['module'] . "/" . $rule['url']);
             }
         } else {
             //只要存在就记录
             $authList[] = strtolower($rule['module'] . "/" . $rule['url']);
         }
     }
     $_authList[$uid . $t] = $authList;
     if ($this->_config['AUTH_TYPE'] == 2) {
         //规则列表结果保存到session
         $_SESSION['_AUTH_LIST_' . $uid . $t] = $authList;
     }
     return array_unique($authList);
 }
Exemple #2
0
 /**
  * 解除关联的一个中间表数据
  * @access public
  * @param integer|array $data 数据 可以使用关联对象的主键
  * @param bool $relationDel 是否同时删除关联表数据
  * @return integer
  */
 public function detach($data, $relationDel = false)
 {
     if (is_array($data)) {
         $id = $data;
     } elseif (is_int($data)) {
         // 根据关联表主键直接写入中间表
         $id = $data;
     } elseif ($data instanceof Model) {
         // 根据关联表主键直接写入中间表
         $relationFk = $data->getPk();
         $id = $data->{$relationFk};
     }
     // 删除中间表数据
     $pk = $this->parent->getPk();
     $pivot[$this->localKey] = $this->parent->{$pk};
     $pivot[$this->foreignKey] = is_array($id) ? ['in', $id] : $id;
     Db::table($this->middle)->where($pivot)->delete();
     // 删除关联表数据
     if ($relationDel) {
         $model = $this->model;
         $model::destroy($id);
     }
 }