kefu/application/admin/controller/WhiteIp.php

209 lines
7.7 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* 平台控制器
*/
namespace app\admin\controller;
use Repository\LogRepository;
class WhiteIp extends Base
{
// 白名单列表
public function index()
{
if(request()->isAjax()){
$param = input('param.');
$limit = $param['pageSize'];
$offset = ($param['pageNumber'] - 1) * $limit;
try {
$gdb = db('groups');
$group_id = $gdb->where('admin_id', $this->admin_id)->value('id');
if($this->is_root != true && empty($group_id)){
return json(['code' => -1, 'data' => '', 'msg' => '您没有该权限']);
}
$where = [];
if ($this->is_root != true) {
$where['group_id'] = $group_id;
} else {
if ($param['group_id'] != -1) {
$where['group_id'] = $param['group_id'];
}
}
if ($param['ip']) {
$where['ip'] = trim($param['ip']);
}
$data = db('white_ip')->where($where)->limit($offset, $limit)->order('id desc')->select();
if ($data) {
$admin_id_arr = array_column($data, 'oper');
// 管理员名称
$admin_arr = db('admins')->where('id', 'in', $admin_id_arr)->column('user_name', 'id');
$group_id_arr = array_column($data, 'group_id');
// 平台名称
$group_arr = $gdb->where('id', 'in', $group_id_arr)->column('name', 'id');
foreach($data as $key => $vo){
// 生成操作按钮
$data[$key]['operate'] = $this->makeBtn($vo['id']);
// 管理员名称
$data[$key]['admin_name'] = isset($admin_arr[$vo['oper']]) ? $admin_arr[$vo['oper']] : '--';
// 平台名称
if ($vo['group_id']) {
$data[$key]['group_name'] = isset($group_arr[$vo['group_id']]) ? $group_arr[$vo['group_id']] : '--';
} else {
$data[$key]['group_name'] = '超管';
}
}
}
$return['total'] = db('white_ip')->where($where)->count(); //总数据
$return['rows'] = $data;
return json($return);
} catch (\Exception $e){
return json(['code' => -1, 'data' => '', 'msg' => $e->getMessage()]);
}
}
$group_info = [];
if ($this->is_root) {
$group_info = db('groups')->where('status', 1)->column('name', 'id');
}
// var_dump($group_info);exit;
$this->assign([
'is_root' => $this->is_root ? 1 : 0,
'group_info' => $group_info,
]);
return $this->fetch();
}
// 添加
public function add_white_ip()
{
if(request()->isPost()){
$param = input('post.');
try{
if ($this->is_root) {
$group_id = $param['group_id'];
} else {
$gdb = db('groups');
$group_id = $gdb->where('admin_id', $this->admin_id)->value('id');
if($this->is_root != true && empty($group_id)){
return json(['code' => -1, 'data' => '', 'msg' => '您没有该权限']);
}
}
$param['ip'] = htmlentities($param['ip']);
$where = ['ip' => $param['ip'], 'group_id' => $group_id];
$db_white_ip = db('white_ip');
$has = $db_white_ip->field('id')->where($where)->find();
if(!empty($has)){
return json(['code' => -1, 'data' => '', 'msg' => '该ip已经存在']);
}
// 先添加平台管理员
$data = [
'ip' => $param['ip'],
'group_id' => $group_id,
'oper' => $this->admin_id,
'create_time' => date('Y-m-d H:i:s'),
];
$add_id = $db_white_ip->insertGetId($data);
if ($add_id) {
LogRepository::write('白名单管理', '添加ip--'.$param['ip']);
// 更新 redis
$this->_update_white_list($group_id);
return json(['code' => 1, 'data' => '', 'msg' => '添加成功']);
}
} catch (\Exception $e){
return json(['code' => -2, 'data' => '', 'msg' => $e->getMessage()]);
}
}
$group_info = [];
if ($this->is_root) {
$group_info = db('groups')->where('status', 1)->column('name', 'id');
}
$this->assign([
'is_root' => $this->is_root ? 1 : 0,
'group_info' => $group_info,
]);
return $this->fetch('');
}
protected function _update_white_list($group_id)
{
// 更新 redis
$db_white_ip = db('white_ip');
$white_ip = $db_white_ip->where('group_id', $group_id)->column('ip');
$redis = new \Redis();
$redis->connect(config('cache.host'),config('cache.port'));
$redis->auth(config('cache.password'));
$redis->hSet('white_list', $group_id, json_encode($white_ip));
}
// 删除
public function del_white_ip()
{
if(request()->isAjax()){
try{
$id = input('param.id/d');
$db_white_ip = db('white_ip');
$info = $db_white_ip->where('id', $id)->find();
if (!$info) {
return json(['code' => -1, 'data' => '', 'msg' => '该ip已删除']);
}
// 只允许超管和平台管理员删除
if (!$this->is_root) {
if ($this->role_name == '平台管理员') {
// 查看是否有权限删除
$group_id = db('groups')->where('admin_id', $this->admin_id)->value('id');
if ($group_id != $info['group_id']) {
return json(['code' => -1, 'data' => '', 'msg' => '您无权限不可删除该ip']);
}
} else {
return json(['code' => -2, 'data' => '', 'msg' => '您无权限不可删除该ip']);
}
}
$res = $db_white_ip->where('id', $id)->delete();
if ($res) {
LogRepository::write('白名单管理', '平台id'.$info['group_id'].',删除的ip--' . $info['ip']);
// 更新 redis
$this->_update_white_list($info['group_id']);
return json(['code' => 1, 'data' => '', 'msg' => '删除成功']);
}
} catch (\Exception $e){
return json(['code' => -1, 'data' => '', 'msg' => $e->getMessage()]);
}
return json(['code' => -1, 'data' => '', 'msg' => '删除失败']);
}
}
// 生成按钮
private function makeBtn($id)
{
$operate = '';
// if ($this->is_root) {
$operate .= '<a href="javascript:userGroup(' . $id . ')"><button type="button" class="btn btn-danger btn-sm">';
$operate .= '<i class="fa fa-trash-o"></i> 删除</button></a> ';
// }
//$operate .= '<a href="' . url('groups/manageUser') . '">';
//$operate .= '<button type="button" class="btn btn-info btn-sm"><i class="fa fa-user-plus"></i> 管理组员</button></a>';
return $operate;
}
}