134 lines
4.0 KiB
PHP
134 lines
4.0 KiB
PHP
<?php
|
|
|
|
namespace app\adminapi\controller;
|
|
|
|
use app\adminapi\model\AuthRule;
|
|
use app\common\controller\AdminApi;
|
|
|
|
use app\adminapi\model\AuthGroup;
|
|
use app\adminapi\model\AuthGroupAccess;
|
|
|
|
use fast\Tree;
|
|
use think\Db;
|
|
use think\Exception;
|
|
|
|
|
|
/**
|
|
* api角色组
|
|
*
|
|
* @icon fa fa-group
|
|
* @remark 角色组可以有多个,角色有上下级层级关系,如果子角色有角色组和管理员的权限则可以派生属于自己组别下级的角色组或管理员
|
|
*/
|
|
class Group extends AdminApi
|
|
{
|
|
protected $model = null;
|
|
|
|
//无需要权限判断的方法
|
|
protected $noNeedRight = ['roletree'];
|
|
//当前登录管理员所有子组别
|
|
protected $childrenGroupIds = [];
|
|
//当前组别列表数据
|
|
protected $grouplist = [];
|
|
protected $groupdata = [];
|
|
|
|
|
|
/**
|
|
* 初始化操作
|
|
* @access protected
|
|
*/
|
|
public function _initialize()
|
|
{
|
|
$this->model = new AuthGroup;
|
|
parent::_initialize();
|
|
|
|
$this->childrenGroupIds = $this->auth->getChildrenGroupIds(true);
|
|
|
|
$groupList = collection(AuthGroup::where('id', 'in', $this->childrenGroupIds)->select())->toArray();
|
|
|
|
Tree::instance()->init($groupList);
|
|
$groupList = [];
|
|
if ($this->auth->isSuperAdmin()) {
|
|
$groupList = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0));
|
|
} else {
|
|
$groups = $this->auth->getGroups();
|
|
$groupIds = [];
|
|
foreach ($groups as $m => $n) {
|
|
if (in_array($n['id'], $groupIds) || in_array($n['pid'], $groupIds)) {
|
|
continue;
|
|
}
|
|
$groupList = array_merge($groupList, Tree::instance()->getTreeList(Tree::instance()->getTreeArray($n['pid'])));
|
|
foreach ($groupList as $index => $item) {
|
|
$groupIds[] = $item['id'];
|
|
}
|
|
}
|
|
}
|
|
$groupName = [];
|
|
foreach ($groupList as $k => $v) {
|
|
$groupName[$v['id']] = $v['name'];
|
|
}
|
|
|
|
$this->grouplist = $groupList;
|
|
$this->groupdata = $groupName;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* 查看
|
|
*/
|
|
public function index()
|
|
{
|
|
// if ($this->request->isAjax()) {
|
|
$list = $this->grouplist;
|
|
$total = count($list);
|
|
$result = array("total" => $total, "rows" => $list);
|
|
$this->success('查询成功', $result);
|
|
// return json($result);
|
|
// }
|
|
// return $this->view->fetch();
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* 添加
|
|
*/
|
|
public function add()
|
|
{
|
|
if ($this->request->isPost()) {
|
|
$this->token();
|
|
$params = $this->request->post();
|
|
foreach ($params as $k => &$v){
|
|
$params[$k] = strip_tags($v);
|
|
}
|
|
|
|
$params['rules'] = explode(',', $params['rules']);
|
|
if (!in_array($params['pid'], $this->childrenGroupIds)) {
|
|
$this->error(__('The parent group exceeds permission limit'));
|
|
}
|
|
$parentmodel = (new AuthGroup)->get($params['pid']);
|
|
if (!$parentmodel) {
|
|
$this->error(__('The parent group can not found'));
|
|
}
|
|
// 父级别的规则节点
|
|
$parentrules = explode(',', $parentmodel->rules);
|
|
// 当前组别的规则节点
|
|
$currentrules = $this->auth->getRuleIds();
|
|
$rules = $params['rules'];
|
|
// 如果父组不是超级管理员则需要过滤规则节点,不能超过父组别的权限
|
|
$rules = in_array('*', $parentrules) ? $rules : array_intersect($parentrules, $rules);
|
|
// 如果当前组别不是超级管理员则需要过滤规则节点,不能超当前组别的权限
|
|
$rules = in_array('*', $currentrules) ? $rules : array_intersect($currentrules, $rules);
|
|
$params['rules'] = implode(',', $rules);
|
|
if ($params) {
|
|
$this->model->create($params);
|
|
$this->success();
|
|
}
|
|
$this->error();
|
|
}
|
|
|
|
}
|
|
|
|
} |