315 lines
11 KiB
PHP
315 lines
11 KiB
PHP
<?php
|
||
|
||
namespace app\adminapi\controller;
|
||
|
||
use app\admin\command\Menu;
|
||
use app\admin\model\AuthGroup;
|
||
use app\admin\model\AuthGroupAccess;
|
||
use app\common\controller\adminApi;
|
||
use fast\Random;
|
||
use fast\Tree;
|
||
use think\Db;
|
||
use think\Validate;
|
||
|
||
/**
|
||
* 管理员管理
|
||
*
|
||
* @icon fa fa-users
|
||
* @remark 一个管理员可以有多个角色组,左侧的菜单根据管理员所拥有的权限进行生成
|
||
*/
|
||
class Admin extends adminApi
|
||
{
|
||
|
||
/**
|
||
* @var \app\admin\model\Admin
|
||
*/
|
||
protected $model = null;
|
||
protected $selectpageFields = 'id,username,nickname,avatar';
|
||
protected $searchFields = 'id,username,nickname';
|
||
protected $childrenGroupIds = [];
|
||
protected $childrenAdminIds = [];
|
||
|
||
public function _initialize()
|
||
{
|
||
parent::_initialize();
|
||
$this->model = model('admin/Admin');
|
||
$this->childrenAdminIds = $this->auth->getChildrenAdminIds($this->auth->isSuperAdmin());
|
||
$this->childrenGroupIds = $this->auth->getChildrenGroupIds($this->auth->isSuperAdmin());
|
||
|
||
$groupList = collection(AuthGroup::where('id', 'in', $this->childrenGroupIds)->select())->toArray();
|
||
|
||
Tree::instance()->init($groupList);
|
||
$groupdata = [];
|
||
if ($this->auth->isSuperAdmin()) {
|
||
$result = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0));
|
||
foreach ($result as $k => $v) {
|
||
$groupdata[$v['id']] = $v['name'];
|
||
}
|
||
} else {
|
||
$result = [];
|
||
$groups = $this->auth->getGroups();
|
||
foreach ($groups as $m => $n) {
|
||
$childlist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray($n['id']));
|
||
$temp = [];
|
||
foreach ($childlist as $k => $v) {
|
||
$temp[$v['id']] = $v['name'];
|
||
}
|
||
$result[__($n['name'])] = $temp;
|
||
}
|
||
$groupdata = $result;
|
||
}
|
||
$this->view->assign('groupdata', $groupdata);
|
||
$this->assignconfig("admin", ['id' => $this->auth->id]);
|
||
}
|
||
|
||
/**
|
||
* 查看
|
||
*/
|
||
public function index()
|
||
{
|
||
//设置过滤方法
|
||
$this->request->filter(['strip_tags', 'trim']);
|
||
//如果发送的来源是Selectpage,则转发到Selectpage
|
||
if ($this->request->request('keyField')) {
|
||
return $this->selectpage();
|
||
}
|
||
$childrenGroupIds = $this->childrenGroupIds;
|
||
$groupName = AuthGroup::where('id', 'in', $childrenGroupIds)
|
||
->column('id,name');
|
||
$authGroupList = AuthGroupAccess::where('group_id', 'in', $childrenGroupIds)
|
||
->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();
|
||
|
||
$list = $this->model
|
||
->where($where)
|
||
->where('id', 'in', $this->childrenAdminIds)
|
||
->field(['password', 'salt', 'token'], true)
|
||
->order($sort, $order)
|
||
->paginate($limit);
|
||
|
||
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" => $list->total(), "rows" => $list->items());
|
||
return V(1,"管理员列表", $result);
|
||
}
|
||
|
||
/**
|
||
* 添加
|
||
* username
|
||
* email
|
||
* mobile
|
||
* nickname
|
||
* password
|
||
* status
|
||
*/
|
||
public function add()
|
||
{
|
||
$params = $this->request->Post();
|
||
if (empty($params)) {
|
||
return V(0,"参数错误", []);
|
||
}
|
||
if (!Validate::is($params['password'], '\S{6,30}')) {
|
||
return V(0,"密码长度必须在6-30位之间,不能包含空格", []);
|
||
}
|
||
$group = $params['group'];
|
||
unset($params['group']);
|
||
unset($params['__token__']);
|
||
if ($params) {
|
||
Db::startTrans();
|
||
try {
|
||
$params['salt'] = Random::alnum();
|
||
$params['password'] = $this->auth->getEncryptPassword($params['password'], $params['salt']);
|
||
$params['avatar'] = '/assets/img/avatar.png'; //设置新管理员默认头像。
|
||
$result = $this->model->save($params);
|
||
if ($result === false) {
|
||
return V(0,"失败", []);
|
||
}
|
||
//过滤不允许的组别,避免越权
|
||
$group = array_intersect($this->childrenGroupIds, $group);
|
||
if (!$group) {
|
||
return V(0,"失败", []);
|
||
}
|
||
|
||
$dataset = [];
|
||
foreach ($group as $value) {
|
||
$dataset[] = ['uid' => $this->model->id, 'group_id' => $value];
|
||
}
|
||
model('admin/AuthGroupAccess')->saveAll($dataset);
|
||
Db::commit();
|
||
return V(1,"添加成功", []);
|
||
} catch (\Exception $e) {
|
||
Db::rollback();
|
||
return V(0,"失败", []);
|
||
$this->error($e->getMessage());
|
||
}
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 编辑
|
||
*/
|
||
public function edit($ids = null)
|
||
{
|
||
if ($this->request->isPost()) {
|
||
$params = $this->request->post();
|
||
$group = $params['group']?? [];
|
||
unset($params['group']);
|
||
unset($params['__token__']);
|
||
if ($params) {
|
||
Db::startTrans();
|
||
try {
|
||
if ($params['password']) {
|
||
if (!Validate::is($params['password'], '\S{6,30}')) {
|
||
return V(0,"密码长度必须在6-30位之间,不能包含空格", []);
|
||
}
|
||
$params['salt'] = Random::alnum();
|
||
$params['password'] = $this->auth->getEncryptPassword($params['password'], $params['salt']);
|
||
} else {
|
||
unset($params['password'], $params['salt']);
|
||
}
|
||
//这里需要针对username和email做唯一验证
|
||
$adminValidate = \think\Loader::validate('admin/Admin');
|
||
$adminValidate->rule([
|
||
'username' => 'require|regex:\w{3,30}|unique:admin,username,' . $params['id'],
|
||
'email' => 'require|email|unique:admin,email,' . $params['id'],
|
||
'mobile' => 'regex:1[3-9]\d{9}|unique:admin,mobile,' . $params['id'],
|
||
'password' => 'regex:\S{32}',
|
||
]);
|
||
$result = $this->model->save($params, $params['id']);
|
||
if ($result === false) {
|
||
return V(0,"失败", []);
|
||
}
|
||
|
||
// 先移除所有权限
|
||
model('admin/AuthGroupAccess')->where('uid', $params['id'])->delete();
|
||
|
||
if(!empty($group)){
|
||
// 过滤不允许的组别,避免越权
|
||
$group = array_intersect($this->childrenGroupIds, $group);
|
||
if (!$group) {
|
||
return V(0,"失败", []);
|
||
}
|
||
|
||
$dataset = [];
|
||
foreach ($group as $value) {
|
||
$dataset[] = ['uid' => $params['id'], 'group_id' => $value];
|
||
}
|
||
model('admin/AuthGroupAccess')->saveAll($dataset);
|
||
}
|
||
Db::commit();
|
||
return V(1,"成功", []);
|
||
} catch (\Exception $e) {
|
||
Db::rollback();
|
||
return V(0,"失败", []);
|
||
}
|
||
}
|
||
return V(0,"参数错误!", []);
|
||
}else{
|
||
return V(0,"参数错误!", []);
|
||
}
|
||
|
||
}
|
||
/*
|
||
* 详情
|
||
*
|
||
*/
|
||
public function detail($ids = ""){
|
||
$id = $this->request->get('id');
|
||
if (!$id) {
|
||
return V(0,"未找到记录", []);
|
||
}
|
||
if (!in_array($id, $this->childrenAdminIds)) {
|
||
return V(0,"你没有权限访问", []);
|
||
}
|
||
$data = $this->model->where('id',$id)->find();
|
||
$grouplist = $this->auth->getGroups($id);
|
||
$groupids = [];
|
||
foreach ($grouplist as $k => $v) {
|
||
$groupids[] = $v['id'];
|
||
}
|
||
$result = [];
|
||
$result['row'] = $data;
|
||
$result['groupids'] = $groupids;
|
||
return V(1,"管理员详情", $result);
|
||
}
|
||
/*
|
||
* 管理员状态修改
|
||
*/
|
||
public function changeStatus(){
|
||
if (!$this->request->isPost()) {
|
||
return V(0,"失败", []);
|
||
}
|
||
$id = $this->request->post("id");
|
||
if (empty($id)) {
|
||
return V(0,"失败", []);
|
||
}
|
||
if($id==1){
|
||
return V(0,"超级管理员不能修改状态", []);
|
||
}
|
||
$status = $this->request->post("status");
|
||
$data = [];
|
||
$data['status'] = $status;
|
||
$res = Db::name("admin")->where("id",$id)->update($data);
|
||
if ($res) {
|
||
return V(1,"成功", []);
|
||
}else{
|
||
return V(0,"失败", []);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 删除
|
||
*/
|
||
public function del($ids = "")
|
||
{
|
||
if (!$this->request->isPost()) {
|
||
return V(0,"失败", []);
|
||
}
|
||
$ids = $ids ? $ids : $this->request->post("id");
|
||
if ($ids) {
|
||
$ids = array_intersect($this->childrenAdminIds, array_filter(explode(',', $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_values(array_diff($deleteIds, [$this->auth->id]));
|
||
if ($deleteIds) {
|
||
Db::startTrans();
|
||
try {
|
||
$this->model->destroy($deleteIds);
|
||
model('admin/AuthGroupAccess')->where('uid', 'in', $deleteIds)->delete();
|
||
Db::commit();
|
||
return V(1,"成功", []);
|
||
} catch (\Exception $e) {
|
||
Db::rollback();
|
||
return V(0,"失败", []);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
return V(0,"失败", []);
|
||
}
|
||
}
|