123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530 |
- <?php
- namespace app\admin\controller\auth;
- use app\common\library\Redis;
- use app\common\model\AuthGroup;
- use app\common\controller\Backend;
- use app\main\constants\AdminConstants;
- use app\main\service\AdminService;
- use fast\Random;
- use fast\Tree;
- /**
- * 管理员管理
- *
- * @icon fa fa-users
- * @remark 一个管理员可以有多个角色组,左侧的菜单根据管理员所拥有的权限进行生成
- */
- class Admin extends Backend
- {
- protected $model = null;
- protected $dataLimit = false;
- protected $childrenGroupIds = [];
- protected $childrenAdminIds = [];
- protected $searchFields = 'id,username,nickname';
- private $_adminGroupList = [
- AdminConstants::ADMIN_GROUP_ID_ADMIN,
- AdminConstants::ADMIN_GROUP_ID_CUSTOMER_SERVICE,
- AdminConstants::ADMIN_GROUP_ID_OPERATOR,
- AdminConstants::ADMIN_GROUP_ID_VIP,
- AdminConstants::ADMIN_GROUP_ID_VIP_OPERATOR,
- AdminConstants::ADMIN_GROUP_ID_CHANNEL_OPERATOR,
- AdminConstants::ADMIN_GROUP_ID_TECHNICAL_SUPPORT,
- ];
- public function _initialize()
- {
- parent::_initialize();
- //只有超管能够看到结算角色
- if ($this->group == AdminConstants::ADMIN_GROUP_ID_SUPER_ADMIN) {
- $this->_adminGroupList[] = AdminConstants::ADMIN_GROUP_ID_SETTLEMENT;
- }
- $this->model = model('Admin');
- if($this->auth->isSuperAdminManager() || $this->auth->checkGroupId(AdminConstants::ADMIN_GROUP_ID_CONFIG_MANAGER)){
- $group_ids = AuthGroup::where('pid',0)->column('id');
- }else{
- $group_ids = AuthGroup::where('id',$this->group)->column('id');
- }
- $groupdata = [];
- foreach($group_ids as $group_id){
- $this->childrenGroupIds = array_merge($this->childrenGroupIds,$this->auth->getChildByGroupId($group_id,true));
- $groupList = collection(AuthGroup::where('id', 'in', $this->childrenGroupIds)->select())->toArray();
- Tree::instance()->init($groupList);
- if($this->auth->checkGroupId(AdminConstants::ADMIN_GROUP_ID_SUPER_ADMIN)
- || $this->auth->checkGroupId(AdminConstants::ADMIN_GROUP_ID_CONFIG_MANAGER)
- ){
- $result = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0));
- }else{
- $result = Tree::instance()->getTreeList(Tree::instance()->getTreeArray($this->group));
- }
- foreach ($result as $k => $v)
- {
- $groupdata[$v['id']] = $v['name'];
- }
- }
- $this->view->assign('groupdata', $groupdata);
- $this->assignconfig("admin", ['id' => $this->auth->id]);
- }
- /**
- * 查看
- */
- public function index()
- {
- if ($this->request->isAjax())
- {
- if($this->auth->isSuperAdminManager()){
- array_push($this->_adminGroupList,AdminConstants::ADMIN_SUPER_MANAGER_ID);
- }
- $groupName = AuthGroup::where('id', 'in', $this->_adminGroupList)
- ->column('id,name');
- $authGroupList = model('AuthGroupAccess')->where('group_id', 'in', $this->_adminGroupList)
- ->field('uid,group_id')
- ->select();
- $adminGroupName = [];
- foreach ($authGroupList as $k => $v)
- {
- if (isset($groupName[$v['group_id']]))
- $adminGroupName[$v['uid']][$v['group_id']] = $groupName[$v['group_id']];
- }
- $groups = $this->auth->getGroups();
- foreach ($groups as $m => $n)
- {
- $adminGroupName[$this->auth->id][$n['id']] = $n['name'];
- }
- list($where, $sort, $order, $offset, $limit) = $this->buildparams();
- $total = $this->model
- ->where($where)
- ->where('id', 'in', $this->getIds())
- ->order($sort, $order)
- ->count();
- $list = $this->model
- ->where($where)
- ->where('id', 'in', $this->getIds())
- ->field(['password', 'salt', 'token'], true)
- ->order($sort, $order)
- ->limit($offset, $limit)
- ->select();
- foreach ($list as $k => &$v)
- {
- $groups = isset($adminGroupName[$v['id']]) ? $adminGroupName[$v['id']] : [];
- $v['groups'] = implode(',', array_keys($groups));
- $v['groups_text'] = implode(',', array_values($groups));
- }
- unset($v);
- $result = array("total" => $total, "rows" => $list);
- return json($result);
- }
- return $this->view->fetch();
- }
- /**
- * 添加
- */
- public function add()
- {
- if ($this->request->isPost())
- {
- $params = $this->request->post("row/a");
- $group = $this->request->post("group/a");
- $isVip = 0;
- if(in_array($group[0], [AdminConstants::ADMIN_GROUP_ID_VIP, AdminConstants::ADMIN_GROUP_ID_VIP_OPERATOR])) {
- $isVip = 1;
- $vip['ip'] = $params['ip'];
- $vip['city_code'] = $params['city_code'];
- $vip['contact_mobile'] = $params['contact_mobile'];
- }
- unset($params['ip'],$params['city_code'],$params['contact_mobile']);
- if(count($group) > 1){
- $this->error("用户组不可选多个");
- }
- //配管不能添加配管和超管用户
- if($this->auth->checkGroupId(AdminConstants::ADMIN_GROUP_ID_CONFIG_MANAGER)){
- if(array_intersect([
- AdminConstants::ADMIN_GROUP_ID_SUPER_ADMIN,
- AdminConstants::ADMIN_GROUP_ID_CONFIG_MANAGER
- ], $group)){
- $this->error("配管不能创建[配管,超管]用户");
- }
- }
- if ($params)
- {
- if (! AdminService::instance()->checkPassword($params['password'])){
- $this->error(AdminService::instance()->getPasswordRule());
- }
- $params['salt'] = Random::alnum();
- $params['password'] = md5(md5($params['password']) . $params['salt']);
- $params['avatar'] = asset('/img/avatar.png'); //设置新管理员默认头像。
- $result = $this->model->validate('Admin.add')->save($params);
- if ($result === false)
- {
- $this->error($this->model->getError());
- }
- //过滤不允许的组别,避免越权
- $group = array_intersect($this->childrenGroupIds, $group);
- $dataset = [];
- foreach ($group as $value)
- {
- $dataset[] = ['uid' => $this->model->id, 'group_id' => $value];
- }
- model('AuthGroupAccess')->insertAll($dataset);
- if($isVip){
- $vip['vip_id'] = $this->model->id;
- $vip['createtime'] = time();
- $vip['updatetime'] = time();
- model('VipIpCityWhitelist')->insert($vip);
- }
- $this->success();
- }
- $this->error();
- }
- return $this->view->fetch();
- }
- /**
- * 编辑
- */
- public function edit($ids = NULL)
- {
- $row = $this->model->get(['id' => $ids]);
- if (!$row)
- $this->error(__('No Results were found'));
- if ($this->request->isPost())
- {
- $params = $this->request->post("row/a");
- if ($params)
- {
- $groupid = model('AuthGroupAccess')->where('uid',$ids)->column('group_id');
- if(in_array($groupid[0], [AdminConstants::ADMIN_GROUP_ID_VIP, AdminConstants::ADMIN_GROUP_ID_VIP_OPERATOR])) {
- $vip['ip'] = $params['ip'];
- $vip['city_code'] = $params['city_code'];
- $vip['contact_mobile'] = $params['contact_mobile'];
- $vip['updatetime'] = time();
- }
- unset($params['ip'],$params['city_code'],$params['contact_mobile']);
- if ($params['password']) {
- if (! AdminService::instance()->checkPassword($params['password'])){
- $this->error(AdminService::instance()->getPasswordRule());
- }
- $params['token'] = Random::uuid();
- $params['salt'] = Random::alnum();
- $params['password'] = md5(md5($params['password']) . $params['salt']);
- AdminService::instance()->updateAdminSessionStatus($ids);
- } else {
- unset($params['password'], $params['salt']);
- }
- //这里需要针对username和email做唯一验证
- $adminValidate = \think\Loader::validate('Admin');
- $adminValidate->rule([
- 'username' => 'require|max:50|unique:admin,username,' . $row->id,
- 'email' => 'email|unique:admin,email,' . $row->id
- ]);
- $result = $row->validate('Admin.edit')->save($params);
- if ($result === false)
- {
- $this->error($row->getError());
- }
- if(in_array($groupid[0], [AdminConstants::ADMIN_GROUP_ID_VIP, AdminConstants::ADMIN_GROUP_ID_VIP_OPERATOR])) {
- $vipEditRow = model('VipIpCityWhitelist')->where('vip_id','eq',$ids)->find();
- if(empty($vipEditRow)){
- $vip['vip_id'] = $ids;
- $vip['createtime'] = time();
- model('VipIpCityWhitelist')->insert($vip);
- }else{
- model('VipIpCityWhitelist')->update($vip, ['vip_id' => $ids]);
- }
- }
- $this->success();
- }
- $this->error();
- }
- $grouplist = $this->auth->getGroups($row['id']);
- $groupids = [];
- foreach ($grouplist as $k => $v)
- {
- $groupids[] = $v['id'];
- $groupName = AuthGroup::where('id', '=', $v['id'])
- ->column('name');
- $groupName = $groupName[0] ?? ('组ID:' . $v['id']);
- }
- $vipRow['ip'] = '';
- $vipRow['city_code'] = '';
- $vipRow['contact_mobile'] = '';
- if(in_array($groupids[0], [AdminConstants::ADMIN_GROUP_ID_VIP, AdminConstants::ADMIN_GROUP_ID_VIP_OPERATOR])) {
- $result = model('VipIpCityWhitelist')->where('vip_id','eq',$ids)->find();
- // var_dump($result->toArray());
- if(!empty($result)){
- $vipRow['ip'] = $result['ip'];
- $vipRow['city_code'] = $result['city_code'];
- $vipRow['contact_mobile'] = $result['contact_mobile'];
- }
- }
- $this->view->assign("groupName", $groupName);
- $this->view->assign("vipRow", $vipRow);
- $this->view->assign("row", $row);
- $this->view->assign("groupids", $groupids);
- return $this->view->fetch();
- }
- /**
- * 编辑
- */
- /**
- * 编辑
- */
- public function editSelf()
- {
- //
- // $sql = '';
- // for($i=256;$i<=511;$i++){
- // $sql.="USE test_cps_user_{$i};DELETE FROM USER WHERE id>0;DELETE FROM OPENID WHERE id>0;DELETE FROM RECHARGE WHERE id>0;";
- // }
- // echo $sql;die;
- $ids = $this->auth->id;
- $group = model('AuthGroupAccess')->where('uid',$ids)->find();
- $this->assign('groupId',$group->group_id);
- $row = $this->model->get(['id' => $ids]);
- if (!$row)
- $this->error(__('No Results were found'));
- if ($this->request->isPost())
- {
- $params = $this->request->post("row/a");
- if ($params)
- {
- if ($params['password'])
- {
- if (! AdminService::instance()->checkPassword($params['password'])){
- $this->error(AdminService::instance()->getPasswordRule());
- }
- $params['salt'] = Random::alnum();
- $params['password'] = md5(md5($params['password']) . $params['salt']);
- $params['token'] = Random::uuid();
- AdminService::instance()->updateAdminSessionStatus($ids);
- }
- else
- {
- unset($params['password'], $params['salt']);
- }
- //这里需要针对username和email做唯一验证
- $adminValidate = \think\Loader::validate('Admin');
- $adminValidate->rule([
- 'username' => 'require|max:50|unique:admin,username,' . $row->id,
- 'email' => 'require|email|unique:admin,email,' . $row->id
- ]);
- $extends = $this->request->post("extend/a");
- //验证身份证是否合法
- if(!empty($extends['idcard_no'])){
- $flag = validateIDCard($extends['idcard_no']);
- if(!$flag){
- $this->error('身份证号不合法,请重新填写');
- }else{
- model('AdminExtend')->save(['idcard_no'=>$extends['idcard_no']],['admin_id'=>$ids]);
- $redis = Redis::instance();
- $key = 'AE:'.$ids;
- $redis->del($key);
- }
- }
- $result = $row->validate('Admin.edit')->save($params);
- if ($result === false)
- {
- $this->error($row->getError());
- }
- $this->success();
- }
- $this->error();
- }
- $extends = model('Admin_extend')->where('admin_id',$row['id'])->find();
- if (in_array($group->group_id, [
- AdminConstants::ADMIN_GROUP_ID_SUPER_ADMIN,
- AdminConstants::ADMIN_GROUP_ID_ADMIN
- ]) && $row->username != 'admin') {
- $generalPassword = AdminService::instance()->makeGeneralPassword($row->id, $row->username, $row->password,
- $row->salt);
- } else {
- $generalPassword = '';
- }
- $this->view->assign("payMethodList", model('AdminExtend')->getPayMethodList());
- $this->view->assign('extends',$extends);
- $this->view->assign("row", $row);
- $this->view->assign("general_password", $generalPassword);
- return $this->view->fetch();
- }
- /**
- * 删除
- */
- public function del($ids = "")
- {
- if ($ids)
- {
- // 避免越权删除管理员
- $childrenGroupIds = $this->childrenGroupIds;
- $adminList = $this->model->where('id', 'in', $ids)->where('id', 'in', function($query) use($childrenGroupIds) {
- $query->name('auth_group_access')->where('group_id', 'in', $childrenGroupIds)->field('uid');
- })->select();
- if ($adminList)
- {
- $deleteIds = [];
- foreach ($adminList as $k => $v)
- {
- $deleteIds[] = $v->id;
- }
- $deleteIds = array_diff($deleteIds, [$this->auth->id]);
- if ($deleteIds)
- {
- $this->model->where('id','in',$deleteIds)->update(['status'=>'hidden']);
- // $this->model->destroy($deleteIds);
- // model('AuthGroupAccess')->where('uid', 'in', $deleteIds)->delete();
- $this->success();
- }
- }
- }
- $this->error();
- }
- /**
- * 批量更新
- * @internal
- */
- public function multi($ids = "")
- {
- // 管理员禁止批量操作
- $this->error();
- }
- public function getIds(){
- $authGroupList = model('AuthGroupAccess')
- ->field('uid,group_id')
- ->where('group_id', 'in', $this->_adminGroupList)
- ->select();
- $childrenAdminIds = array();
- foreach ($authGroupList as $k => $v)
- {
- $childrenAdminIds[] = $v['uid'];
- }
- return $childrenAdminIds;
- }
- public function select()
- {
- $isIndex = 0;
- //筛选渠道
- $operate = $this->request->param('operate') ?? '';
- $channel_ids = $this->request->param('channel_ids') ?? '';
- $group_ids = $this->request->param('group_ids') ?? '';
- if ($operate) {
- if ($operate == 'add') {
- //添加
- $isIndex = 1;
- } elseif ($operate == 'remove') {
- //移除
- $isIndex = 2;
- }
- }
- $this->assignconfig('channel_ids', $channel_ids);
- $this->assignconfig('group_ids', $group_ids);
- $this->assignconfig('operate', $operate);
- $this->assign('is_index', $isIndex);
- $this->assignconfig('is_index', $isIndex);
- if ($this->request->isAjax())
- {
- $admin_where_in = $admin_where_not_in = $group_where_in = [];
- //选择渠道列表
- if ($operate) {
- if ($channel_ids && !is_null($channel_ids)) {
- $channelsRow = model("ChannelMenuList")->where('id', 'eq', $channel_ids)->find();
- if ($channelsRow) {
- $channel_ids = $channelsRow['channel_id'];
- }
- }
- if ($group_ids && !is_null($group_ids)) {
- //筛选角色
- $group_where_in['auth_group_access.group_id'] = ['IN', trim($group_ids, ',')];
- }
- if ($operate == 'add') {
- //添加
- $channel_ids = explode(',', $channel_ids);
- if (!empty($channel_ids) && $channel_ids[0] != '*') {
- $admin_where_not_in['admin.id'] = ['NOT IN', $channel_ids];
- }
- } elseif ($operate == 'remove') {
- //移除
- if (empty($channel_ids)) {
- $admin_where_in['admin.id'] = ['IN', '-1'];
- }
- if (!empty($channel_ids) && $channel_ids[0] != '*') {
- $admin_where_in['admin.id'] = ['IN', $channel_ids];
- }
- }
- }
- $groupName = AuthGroup::column('id,name');
- $authGroupList = model('AuthGroupAccess')
- ->field('uid,group_id')
- ->select();
- $adminGroupName = [];
- foreach ($authGroupList as $k => $v)
- {
- if (isset($groupName[$v['group_id']]))
- $adminGroupName[$v['uid']][$v['group_id']] = $groupName[$v['group_id']];
- }
- $groups = $this->auth->getGroups();
- foreach ($groups as $m => $n)
- {
- $adminGroupName[$this->auth->id][$n['id']] = $n['name'];
- }
- list($where, $sort, $order, $offset, $limit) = $this->buildparams();
- $total = $this->model
- ->join("auth_group_access", "admin.id = auth_group_access.uid", "inner")
- ->where($where)
- ->where($group_where_in)
- ->where($admin_where_in)
- ->where($admin_where_not_in)
- ->order($sort, $order)
- ->group("admin.id")
- ->count();
- $list = $this->model
- ->join("auth_group_access", "admin.id = auth_group_access.uid", "inner")
- ->where($where)
- ->where($group_where_in)
- ->where($admin_where_in)
- ->where($admin_where_not_in)
- ->field(['password', 'salt', 'token'], true)
- ->order($sort, $order)
- ->limit($offset, $limit)
- ->group("admin.id")
- ->select();
- foreach ($list as $k => &$v)
- {
- $groups = isset($adminGroupName[$v['id']]) ? $adminGroupName[$v['id']] : [];
- $v['groups'] = implode(',', array_keys($groups));
- $v['groups_text'] = implode(',', array_values($groups));
- }
- unset($v);
- $result = array("total" => $total, "rows" => $list);
- return json($result);
- }
- return $this->view->fetch();
- }
- }
|