123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380 |
- <?php
- namespace app\admin\controller;
- use cmf\controller\AdminBaseController;
- use think\Db;
- use think\facade\Cache;
- use tree\Tree;
- use app\admin\model\AdminMenuModel;
- class RbacController extends AdminBaseController
- {
-
- public function index()
- {
- $content = hook_one('admin_rbac_index_view');
- if (!empty($content)) {
- return $content;
- }
- $data = Db::name('role')->order(["list_order" => "ASC", "id" => "DESC"])->select();
- $this->assign("roles", $data);
- return $this->fetch();
- }
-
- public function roleAdd()
- {
- $content = hook_one('admin_rbac_role_add_view');
- if (!empty($content)) {
- return $content;
- }
- return $this->fetch();
- }
-
- public function roleAddPost()
- {
- if ($this->request->isPost()) {
- $data = $this->request->param();
- $result = $this->validate($data, 'role');
- if ($result !== true) {
-
- $this->error($result);
- } else {
- $result = Db::name('role')->insert($data);
- if ($result) {
- $this->success("添加角色成功", url("rbac/index"));
- } else {
- $this->error("添加角色失败");
- }
- }
- }
- }
-
- public function roleEdit()
- {
- $content = hook_one('admin_rbac_role_edit_view');
- if (!empty($content)) {
- return $content;
- }
- $id = $this->request->param("id", 0, 'intval');
- if ($id == 1) {
- $this->error("超级管理员角色不能被修改!");
- }
- $data = Db::name('role')->where("id", $id)->find();
- if (!$data) {
- $this->error("该角色不存在!");
- }
- $this->assign("data", $data);
- return $this->fetch();
- }
-
- public function roleEditPost()
- {
- $id = $this->request->param("id", 0, 'intval');
- if ($id == 1) {
- $this->error("超级管理员角色不能被修改!");
- }
- if ($this->request->isPost()) {
- $data = $this->request->param();
- $result = $this->validate($data, 'role');
- if ($result !== true) {
-
- $this->error($result);
- } else {
- if (Db::name('role')->update($data) !== false) {
- $this->success("保存成功!", url('rbac/index'));
- } else {
- $this->error("保存失败!");
- }
- }
- }
- }
-
- public function roleDelete()
- {
- $id = $this->request->param("id", 0, 'intval');
- if ($id == 1) {
- $this->error("超级管理员角色不能被删除!");
- }
- $count = Db::name('RoleUser')->where('role_id', $id)->count();
- if ($count > 0) {
- $this->error("该角色已经有用户!");
- } else {
- $status = Db::name('role')->delete($id);
- if (!empty($status)) {
- $this->success("删除成功!", url('rbac/index'));
- } else {
- $this->error("删除失败!");
- }
- }
- }
-
- public function authorize()
- {
- $content = hook_one('admin_rbac_authorize_view');
- if (!empty($content)) {
- return $content;
- }
- $AuthAccess = Db::name("AuthAccess");
- $adminMenuModel = new AdminMenuModel();
-
- $roleId = $this->request->param("id", 0, 'intval');
- if (empty($roleId)) {
- $this->error("参数错误!");
- }
- $tree = new Tree();
- $tree->icon = ['│ ', '├─ ', '└─ '];
- $tree->nbsp = ' ';
- $result = $adminMenuModel->menuCache();
- $newMenus = [];
- $privilegeData = $AuthAccess->where("role_id", $roleId)->column("rule_name");
- foreach ($result as $m) {
- $newMenus[$m['id']] = $m;
- }
- foreach ($result as $n => $t) {
- $result[$n]['checked'] = ($this->_isChecked($t, $privilegeData)) ? ' checked' : '';
- $result[$n]['level'] = $this->_getLevel($t['id'], $newMenus);
- $result[$n]['style'] = empty($t['parent_id']) ? '' : 'display:none;';
- $result[$n]['parentIdNode'] = ($t['parent_id']) ? ' class="child-of-node-' . $t['parent_id'] . '"' : '';
- }
- $str = "<tr id='node-\$id'\$parentIdNode style='\$style'>
- <td style='padding-left:30px;'>\$spacer<input type='checkbox' name='menuId[]' value='\$id' level='\$level' \$checked onclick='javascript:checknode(this);'> \$name</td>
- </tr>";
- $tree->init($result);
- $category = $tree->getTree(0, $str);
- $this->assign("category", $category);
- $this->assign("roleId", $roleId);
- return $this->fetch();
- }
-
- public function authorizePost()
- {
- if ($this->request->isPost()) {
- $roleId = $this->request->param("roleId", 0, 'intval');
- if (!$roleId) {
- $this->error("需要授权的角色不存在!");
- }
- if (is_array($this->request->param('menuId/a')) && count($this->request->param('menuId/a')) > 0) {
- Db::name("authAccess")->where(["role_id" => $roleId, 'type' => 'admin_url'])->delete();
- foreach ($_POST['menuId'] as $menuId) {
- $menu = Db::name("adminMenu")->where("id", $menuId)->field("app,controller,action")->find();
- if ($menu) {
- $app = $menu['app'];
- $model = $menu['controller'];
- $action = $menu['action'];
- $name = strtolower("$app/$model/$action");
- Db::name("authAccess")->insert(["role_id" => $roleId, "rule_name" => $name, 'type' => 'admin_url']);
- }
- }
- Cache::clear('admin_menus');
- $this->success("授权成功!");
- } else {
-
- Db::name("authAccess")->where("role_id", $roleId)->delete();
- $this->error("没有接收到数据,执行清除授权成功!");
- }
- }
- }
-
- private function _isChecked($menu, $privData)
- {
- $app = $menu['app'];
- $model = $menu['controller'];
- $action = $menu['action'];
- $name = strtolower("$app/$model/$action");
- if ($privData) {
- if (in_array($name, $privData)) {
- return true;
- } else {
- return false;
- }
- } else {
- return false;
- }
- }
-
- protected function _getLevel($id, $array = [], $i = 0)
- {
- if ($array[$id]['parent_id'] == 0 || empty($array[$array[$id]['parent_id']]) || $array[$id]['parent_id'] == $id) {
- return $i;
- } else {
- $i++;
- return $this->_getLevel($array[$id]['parent_id'], $array, $i);
- }
- }
-
- public function member()
- {
-
- }
- }
|