189 lines
5.3 KiB
PHP
189 lines
5.3 KiB
PHP
<?php
|
|
|
|
namespace app\admin\controller\auth;
|
|
|
|
use app\admin\model\AuthGroup;
|
|
use app\common\controller\Backend;
|
|
use fast\Tree;
|
|
use think\Db;
|
|
use think\Exception;
|
|
|
|
/**
|
|
* 角色组
|
|
*
|
|
* @icon fa fa-group
|
|
* @remark 角色组可以有多个,角色有上下级层级关系,如果子角色有角色组和管理员的权限则可以派生属于自己组别下级的角色组或管理员
|
|
*/
|
|
class Group extends Backend
|
|
{
|
|
|
|
/**
|
|
* @var \app\admin\model\AuthGroup
|
|
*/
|
|
protected $model = null;
|
|
public function _initialize()
|
|
{
|
|
parent::_initialize();
|
|
$this->model = new \app\admin\model\auth\Group;
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
|
|
* 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
|
|
* 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
|
|
*/
|
|
|
|
|
|
/**
|
|
* 查看
|
|
*/
|
|
public function index()
|
|
{
|
|
//当前是否为关联查询
|
|
$this->relationSearch = false;
|
|
//设置过滤方法
|
|
$this->request->filter(['strip_tags', 'trim']);
|
|
//如果发送的来源是Selectpage,则转发到Selectpage
|
|
if ($this->request->request('keyField')) {
|
|
return $this->selectpage();
|
|
}
|
|
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
|
|
|
|
$list = $this->model
|
|
->where($where)
|
|
->order($sort, $order)
|
|
->select();
|
|
|
|
foreach ($list as $row) {
|
|
$row->visible(['id','name','remark','status','createtime','is_lecturer']);
|
|
}
|
|
|
|
//讲师角色写死在代码中,与超级管理员同理
|
|
$list[] = [
|
|
'id' => \app\admin\model\auth\Group::LECTURER_GROUP_ID,
|
|
'name' => \app\admin\model\auth\Group::LECTURER_GROUP_NAME,
|
|
'remark' => \app\admin\model\auth\Group::LECTURER_GROUP_REMARK,
|
|
'status' => '1',
|
|
'createtime' => 0,
|
|
'is_lecturer' => 1,
|
|
];
|
|
|
|
//在$list数组前插入一个超级管理员的成员
|
|
array_unshift($list,['id'=>0,'name'=>'超级管理员','remark'=>'','status'=>'1','createtime'=>0,'is_lecturer'=>0]);
|
|
return $this->success("获取成功",$list);
|
|
}
|
|
|
|
|
|
/**
|
|
* 添加角色
|
|
*/
|
|
public function add()
|
|
{
|
|
$params = $this->request->post("row/a", [], 'strip_tags');
|
|
// 当前组别的规则节点
|
|
$rules = $params['rules'];
|
|
$params['rules'] = implode(',', $rules);
|
|
if ($params) {
|
|
$params['rules'] = $this->model->patchingMenu($params['rules']);
|
|
$params['uniacid'] = UNIACID;
|
|
$params['status'] = 1;
|
|
$this->model->create($params);
|
|
$this->success("添加角色成功");
|
|
}
|
|
$this->error("参数错误,请重试");
|
|
}
|
|
|
|
/**
|
|
* 编辑
|
|
*/
|
|
public function edit($ids = null)
|
|
{
|
|
//讲师角色写死在代码中,不可编辑
|
|
if ($ids == \app\admin\model\auth\Group::LECTURER_GROUP_ID) {
|
|
$this->error("讲师角色不可编辑");
|
|
}
|
|
|
|
$row = $this->model->get(['id' => $ids]);
|
|
|
|
if (!$row) {
|
|
$this->error(__('No Results were found'));
|
|
}
|
|
$params = $this->request->post("row/a", [], 'strip_tags');
|
|
|
|
$rules = $params['rules'];
|
|
|
|
$params['rules'] = implode(',', $rules);
|
|
if ($params) {
|
|
Db::startTrans();
|
|
try {
|
|
$params['rules'] = $this->model->patchingMenu($params['rules']);
|
|
|
|
$row->save($params);
|
|
Db::commit();
|
|
$this->success("修改角色成功");
|
|
} catch (Exception $e) {
|
|
Db::rollback();
|
|
$this->error($e->getMessage());
|
|
}
|
|
}
|
|
$this->error("参数错误,请重试");
|
|
}
|
|
|
|
|
|
/**
|
|
* 删除
|
|
*/
|
|
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'));
|
|
}
|
|
//讲师角色写死在代码中,不可删除
|
|
if (in_array(\app\admin\model\auth\Group::LECTURER_GROUP_ID, (array)$ids)) {
|
|
$this->error("讲师角色不可删除");
|
|
}
|
|
parent::del($ids);
|
|
}
|
|
|
|
|
|
/**
|
|
* 获取详情
|
|
* @param $id
|
|
* @return mixed
|
|
*/
|
|
public function detail($id){
|
|
if($id === 0){
|
|
//获取所有的权限
|
|
return $this->success("获取成功",$this->model->getAllRoles());
|
|
}
|
|
$row = $this->model->get($id);
|
|
if (!$row) {
|
|
$this->error(__('No Results were found'));
|
|
}
|
|
return $this->success("获取成功",$row);
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* 角色分组权限树
|
|
* @return mixed
|
|
*/
|
|
public function roleTree(){
|
|
$list = config('menu');
|
|
$id = $this->request->post("id");
|
|
|
|
$groupRules = $this->model->getGroupRole($id);
|
|
|
|
$list = $this->model->updateMenuSelected($list,$groupRules);
|
|
|
|
return $this->success("获取成功",$list);
|
|
}
|
|
}
|