123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281 |
- <?php
-
-
-
-
-
-
-
-
-
- namespace app\admin\Controller\auth;
-
- use app\admin\model\AuthGroup as AuthGroupModel;
- use app\common\controller\Adminbase;
- use think\Db;
- use util\Tree;
-
-
- class Group extends Adminbase
- {
-
- protected $childrenGroupIds = [];
-
- protected $grouplist = [];
- protected $groupdata = [];
- protected $noNeedRight = [];
-
- protected function initialize()
- {
- parent::initialize();
- $this->modelClass = new AuthGroupModel;
-
- $this->childrenGroupIds = $this->auth->getChildrenGroupIds(true);
-
- $groupList = AuthGroupModel::where('id', 'in', $this->childrenGroupIds)->select()->toArray();
-
- Tree::instance()->init($groupList);
- $groupList = [];
- if ($this->auth->isAdministrator()) {
- $groupList = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'title');
- } else {
- $groups = $this->auth->getGroups();
- $groupIds = [];
- foreach ($groups as $m => $n) {
- if (in_array($n['id'], $groupIds) || in_array($n['parentid'], $groupIds)) {
- continue;
- }
- $groupList = array_merge($groupList, Tree::instance()->getTreeList(Tree::instance()->getTreeArray($n['parentid']), 'title'));
- foreach ($groupList as $index => $item) {
- $groupIds[] = $item['id'];
- }
- }
- }
-
- $groupName = [];
- foreach ($groupList as $k => $v) {
- $groupName[$v['id']] = $v['title'];
- }
- $this->grouplist = $groupList;
- $this->groupdata = $groupName;
- $this->assign('groupdata', $this->groupdata);
- }
-
-
- public function index()
- {
- if ($this->request->isAjax()) {
- $list = $this->grouplist;
- $total = count($list);
- $result = ["code" => 0, "count" => $total, "data" => $list];
- return json($result);
- } else {
- return $this->fetch();
- }
- }
-
-
- public function add()
- {
- if ($this->request->isPost()) {
- $this->token();
- $params = $this->request->post("row/a", [], 'strip_tags');
- $result = $this->validate($params, 'app\admin\validate\AuthGroup');
- if (true !== $result) {
- return $this->error($result);
- }
- if (!in_array($params['parentid'], $this->childrenGroupIds)) {
- $this->error('父组别超出权限范围');
- }
- $parentmodel = AuthGroupModel::get($params['parentid']);
- if (!$parentmodel) {
- $this->error('父组别未找到');
- }
- if ($params) {
- $this->modelClass->create($params);
- $this->success('新增成功');
- }
- $this->error('参数不能为空');
- }
- return $this->fetch();
-
- }
-
-
- public function edit()
- {
- $id = $this->request->param('id/d');
- if (!in_array($id, $this->childrenGroupIds)) {
- $this->error('你没有权限访问!');
- }
- $row = $this->modelClass->get($id);
- if (!$row) {
- $this->error('记录未找到');
- }
- if ($this->request->isPost()) {
- $this->token();
- $params = $this->request->post("row/a", [], 'strip_tags');
-
- if (!in_array($params['parentid'], $this->childrenGroupIds)) {
- $this->error('父组别超出权限范围');
- }
-
- if (in_array($params['parentid'], Tree::instance()->getChildrenIds($row->id, true))) {
- $this->error('父角色不能是自身!');
- }
- $params['rules'] = explode(',', $params['rules']);
-
- $parentmodel = AuthGroupModel::get($params['parentid']);
- if (!$parentmodel) {
- $this->error('父组别未找到');
- }
-
-
- $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) {
- Db::startTrans();
- try {
- $row->save($params);
- $children_auth_groups = model("AuthGroup")->all(['id' => ['in', implode(',', (Tree::instance()->getChildrenIds($row->id)))]]);
- $childparams = [];
- foreach ($children_auth_groups as $key => $children_auth_group) {
- $childparams[$key]['id'] = $children_auth_group->id;
- $childparams[$key]['rules'] = implode(',', array_intersect(explode(',', $children_auth_group->rules), $rules));
- }
- model("AuthGroup")->saveAll($childparams);
- Db::commit();
- } catch (Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- $this->success('编辑成功');
- }
- $this->error('参数不能为空');
- }
- $this->assign("data", $row);
- return $this->fetch();
- }
-
-
- public function access()
- {
- $id = $this->request->param('id/d');
- $pid = $this->request->param('pid/d');
- if (!in_array($id, $this->childrenGroupIds)) {
- $this->error('你没有权限访问!');
- }
- $row = $this->modelClass->get($id);
- if (!$row) {
- $this->error('记录未找到');
- }
- if ($this->request->isPost()) {
- $this->token();
- $params = $this->request->post("row/a", [], 'strip_tags');
- $params['rules'] = explode(',', $params['rules']);
-
- $parentmodel = $this->modelClass->get($params['parentid']);
- if (!$parentmodel) {
- $this->error('父组别未找到');
- }
-
- $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);
- Db::startTrans();
- try {
- $row->rules = implode(',', $rules);
- $row->save();
- $children_auth_groups = model("AuthGroup")->all(['id' => ['in', implode(',', (Tree::instance()->getChildrenIds($row->id)))]]);
- $childparams = [];
- foreach ($children_auth_groups as $key => $children_auth_group) {
- $childparams[$key]['id'] = $children_auth_group->id;
- $childparams[$key]['rules'] = implode(',', array_intersect(explode(',', $children_auth_group->rules), $rules));
- }
- model("AuthGroup")->saveAll($childparams);
- Db::commit();
- } catch (Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- $this->success('编辑成功');
- }
- $parentGroupModel = $this->modelClass->get($pid);
-
- $ruleList = model('AuthRule')->order('listorder', 'desc')->order('id', 'asc')->select()->toArray();
-
- $parentRuleList = [];
- if (in_array('*', explode(',', $parentGroupModel->rules))) {
- $parentRuleList = $ruleList;
- } else {
- $parentRuleIds = explode(',', $parentGroupModel->rules);
- foreach ($ruleList as $k => $v) {
- if (in_array($v['id'], $parentRuleIds)) {
- $parentRuleList[] = $v;
- }
- }
- }
-
- $ruleTree = new Tree();
- $groupTree = new Tree();
-
- $ruleTree->init($parentRuleList);
-
- $groupTree->init(model('AuthGroup')->where('id', 'in', $this->childrenGroupIds)->select()->toArray());
-
-
- $adminRuleIds = $this->auth->getRuleIds();
-
- $superadmin = $this->auth->isAdministrator();
-
- $currentRuleIds = explode(',', $row->rules);
- $parentRuleList = $ruleTree->getTreeList($ruleTree->getTreeArray(0), 'name');
-
-
- $parentRuleIds = array_map(function ($item) {
- return $item['id'];
- }, $parentRuleList);
- $nodeList = [];
- foreach ($parentRuleList as $k => $v) {
- if (!$superadmin && !in_array($v['id'], $adminRuleIds)) {
- continue;
- }
- if ($v['parentid'] && !in_array($v['parentid'], $parentRuleIds)) {
- continue;
- }
-
- $ischeck = in_array($v['id'], $currentRuleIds);
- $nodeList[] = ['id' => $v['id'], 'parentid' => $v['parentid'], 'name' => $v['title'], 'checked' => $ischeck];
- }
-
- $this->assign('nodeList', json_encode($nodeList));
- $this->assign("data", $row);
- return $this->fetch();
- }
-
-
- public function multi()
- {
-
- $this->error();
- }
-
- }
|