313 lines
9.4 KiB
PHP
313 lines
9.4 KiB
PHP
<?php
|
|
|
|
namespace app\admin\controller\auth;
|
|
|
|
use app\admin\model\AuthGroup;
|
|
use app\admin\model\AuthGroupAccess;
|
|
use app\common\controller\Backend;
|
|
use fast\Random;
|
|
use fast\Tree;
|
|
use think\Db;
|
|
use think\Validate;
|
|
|
|
/**
|
|
* 管理员管理
|
|
*
|
|
* @icon fa fa-users
|
|
* @remark 一个管理员可以有多个角色组,左侧的菜单根据管理员所拥有的权限进行生成
|
|
*/
|
|
class Admin extends Backend
|
|
{
|
|
|
|
/**
|
|
* @var \app\admin\model\Admin
|
|
*/
|
|
protected $model = null;
|
|
protected $selectpageFields = 'id,username,nickname,avatar';
|
|
protected $searchFields = 'id,username,nickname';
|
|
protected $childrenGroupIds = [];
|
|
protected $childrenAdminIds = [];
|
|
protected $noNeedRight = ['getAdminRules'];
|
|
|
|
public function _initialize()
|
|
{
|
|
parent::_initialize();
|
|
|
|
$this->model = new \app\admin\model\Admin;
|
|
$this->authGroupAccessModel = new \app\admin\model\AuthGroupAccess;
|
|
$this->childrenGroupIds = $this->auth->getChildrenGroupIds($this->auth->isSuperAdmin());
|
|
|
|
}
|
|
|
|
/**
|
|
* 查看
|
|
*/
|
|
public function index()
|
|
{
|
|
//设置过滤方法
|
|
$this->request->filter(['strip_tags', 'trim']);
|
|
//如果发送的来源是Selectpage,则转发到Selectpage
|
|
|
|
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
|
|
|
|
$list = $this->model
|
|
->with(['group'])
|
|
->where($where)
|
|
->order($sort, $order)
|
|
->paginate($limit);
|
|
|
|
foreach ($list as $row) {
|
|
$row->visible(['id','username','name','loginip','group_id','updatetime','createtime','logintime','group','status']);
|
|
$row->getRelation('group')->visible(['name','id']);
|
|
}
|
|
|
|
unset($v);
|
|
$result = array("total" => $list->total(), "rows" => $list->items(),'attr'=>[
|
|
'statusList'=>$this->model->getStatusList()
|
|
]);
|
|
$this->success("获取成功",$result);
|
|
}
|
|
|
|
/**
|
|
* 添加
|
|
*/
|
|
public function add()
|
|
{
|
|
$params = $this->request->post("row/a");
|
|
if ($params) {
|
|
$params['nickname'] = $params['username'];
|
|
$params['email'] = $params['username']."@163.com";
|
|
Db::startTrans();
|
|
try {
|
|
if (!Validate::is($params['password'], '\S{6,30}')) {
|
|
exception(__("Please input correct password"));
|
|
}
|
|
$params['salt'] = Random::alnum();
|
|
$params['password'] = md5(md5($params['password']) . $params['salt']);
|
|
$params['avatar'] = '/assets/img/avatar.png'; //设置新管理员默认头像。
|
|
$params['uniacid'] = UNIACID;
|
|
|
|
$group = $params['group'];
|
|
unset($params['group']);
|
|
|
|
$result = $this->model->validate('Admin.add')->save($params);
|
|
if ($result === false) {
|
|
exception($this->model->getError());
|
|
}
|
|
$dataset = [
|
|
['uid' => $this->model->id, 'group_id' => $group,'uniacid'=>UNIACID]
|
|
];
|
|
$this->authGroupAccessModel->saveAll($dataset);
|
|
Db::commit();
|
|
} catch (\Exception $e) {
|
|
Db::rollback();
|
|
$this->error($e->getMessage());
|
|
}
|
|
$this->success("添加成功");
|
|
}
|
|
$this->error(__('Parameter %s can not be empty', ''));
|
|
}
|
|
|
|
/**
|
|
* 编辑
|
|
*/
|
|
public function edit($ids = null)
|
|
{
|
|
$row = $this->model->get(['id' => $ids]);
|
|
if (!$row) {
|
|
$this->error(__('No Results were found'));
|
|
}
|
|
$params = $this->request->post("row/a");
|
|
if ($params) {
|
|
$params['nickname'] = $params['username'];
|
|
$params['email'] = $params['username']."@163.com";
|
|
Db::startTrans();
|
|
try {
|
|
if ($params['password']) {
|
|
if (!Validate::is($params['password'], '\S{6,30}')) {
|
|
exception(__("Please input correct password"));
|
|
}
|
|
$params['salt'] = Random::alnum();
|
|
$params['password'] = md5(md5($params['password']) . $params['salt']);
|
|
} else {
|
|
unset($params['password'], $params['salt']);
|
|
}
|
|
//这里需要针对username和email做唯一验证
|
|
$adminValidate = \think\Loader::validate('Admin');
|
|
$adminValidate->rule([
|
|
'username' => 'require|regex:\w{3,30}|unique:admin,username,' . $row->id,
|
|
'email' => 'require|email|unique:admin,email,' . $row->id,
|
|
'mobile' => 'regex:1[3-9]\d{9}|unique:admin,mobile,' . $row->id,
|
|
'password' => 'regex:\S{32}',
|
|
]);
|
|
|
|
$group = $params['group'];
|
|
unset($params['group']);
|
|
|
|
$result = $row->validate('Admin.edit')->save($params);
|
|
if ($result === false) {
|
|
exception($row->getError());
|
|
}
|
|
|
|
// 先移除所有权限
|
|
$this->authGroupAccessModel->where('uid', $row->id)->delete();
|
|
|
|
$dataset[] = ['uid' => $row->id, 'group_id' => $group,'uniacid'=>UNIACID];
|
|
|
|
$this->authGroupAccessModel->saveAll($dataset);
|
|
Db::commit();
|
|
} catch (\Exception $e) {
|
|
Db::rollback();
|
|
$this->error($e->getMessage());
|
|
}
|
|
$this->success("操作成功");
|
|
}
|
|
$this->error(__('Parameter %s can not be empty', ''));
|
|
}
|
|
|
|
/**
|
|
* 获取管理员详情
|
|
* @param $ids
|
|
* @return void
|
|
*/
|
|
public function detail($ids){
|
|
$row = $this->model->with(['groupaccess'])->where(['id' => $ids])->find();
|
|
if (!$row) {
|
|
$this->error(__('No Results were found'));
|
|
}
|
|
$this->success("获取成功",$row);
|
|
}
|
|
|
|
|
|
/**
|
|
* 删除
|
|
*
|
|
* @param $ids
|
|
* @return void
|
|
* @throws DbException
|
|
* @throws DataNotFoundException
|
|
* @throws ModelNotFoundException
|
|
*/
|
|
public function del($ids = null)
|
|
{
|
|
if (false === $this->request->isPost()) {
|
|
$this->error(__("Invalid parameters"));
|
|
}
|
|
$ids = $ids ?: $this->request->post("ids");
|
|
if (empty($ids)) {
|
|
$this->error(__('Parameter %s can not be empty', 'ids'));
|
|
}
|
|
$pk = $this->model->getPk();
|
|
$adminIds = $this->getDataLimitAdminIds();
|
|
if (is_array($adminIds)) {
|
|
$this->model->where($this->dataLimitField, 'in', $adminIds);
|
|
}
|
|
$list = $this->model->where($pk, 'in', $ids)->select();
|
|
|
|
$count = 0;
|
|
Db::startTrans();
|
|
try {
|
|
foreach ($list as $item) {
|
|
if($item->id == 1){
|
|
continue;
|
|
}
|
|
$this->authGroupAccessModel->where('uid', 'in', $item->id)->delete();
|
|
$count += $item->delete();
|
|
}
|
|
Db::commit();
|
|
} catch (PDOException|Exception $e) {
|
|
Db::rollback();
|
|
$this->error($e->getMessage());
|
|
}
|
|
if ($count) {
|
|
$this->success("操作成功");
|
|
}
|
|
$this->error(__('No rows were deleted'));
|
|
}
|
|
|
|
/**
|
|
* 批量更新
|
|
* @internal
|
|
*/
|
|
public function multi($ids = "")
|
|
{
|
|
// 管理员禁止批量操作
|
|
$this->error();
|
|
}
|
|
|
|
/**
|
|
* 下拉搜索
|
|
*/
|
|
public function selectpage()
|
|
{
|
|
$this->dataLimit = 'auth';
|
|
$this->dataLimitField = 'id';
|
|
return parent::selectpage();
|
|
}
|
|
|
|
/**
|
|
* 切换状态
|
|
* @param $ids
|
|
* @param $status
|
|
* @param $timing 是否为定时切换事件
|
|
* @return void
|
|
*/
|
|
public function status($ids=null,$status=1,$timing = false){
|
|
if (false === $this->request->isPost()) {
|
|
$this->error(__("Invalid parameters"));
|
|
}
|
|
$ids = $ids ?: $this->request->post("ids");
|
|
if (empty($ids)) {
|
|
$this->error(__('Parameter %s can not be empty', 'ids'));
|
|
}
|
|
|
|
$status = $status ?: $this->request->post("status");
|
|
|
|
$pk = $this->model->getPk();
|
|
$adminIds = $this->getDataLimitAdminIds();
|
|
if (is_array($adminIds)) {
|
|
$this->model->where($this->dataLimitField, 'in', $adminIds);
|
|
}
|
|
$list = $this->model->where($pk, 'in', $ids)->select();
|
|
|
|
$count = 0;
|
|
Db::startTrans();
|
|
try {
|
|
foreach ($list as $item) {
|
|
if($item['id'] == 0 || $item['username'] == 'admin'){
|
|
continue;
|
|
}
|
|
$row = [
|
|
'status'=>$status
|
|
];
|
|
$this->model->where([
|
|
'id'=>$item['id']
|
|
])->update($row);
|
|
$count++;
|
|
}
|
|
Db::commit();
|
|
} catch (PDOException|Exception $e) {
|
|
Db::rollback();
|
|
$this->error($e->getMessage());
|
|
}
|
|
if ($count) {
|
|
$this->success("操作成功");
|
|
}
|
|
$this->error(__('未操作任何员工'));
|
|
}
|
|
|
|
/**
|
|
* 获取管理员角色组
|
|
* @return void
|
|
*/
|
|
public function getAdminRules(){
|
|
|
|
$rules = $this->auth->getRuleIds($this->auth->id);
|
|
|
|
$this->success("操作成功", [
|
|
'rules' => $rules,
|
|
'is_lecturer' => $this->auth->isLecturer()
|
|
]);
|
|
}
|
|
}
|