RbacController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-present http://www.thinkcmf.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 小夏 < 449134904@qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\controller;
  12. use app\admin\model\AuthAccessModel;
  13. use app\admin\model\RoleModel;
  14. use app\admin\model\RoleUserModel;
  15. use cmf\controller\AdminBaseController;
  16. use think\facade\Cache;
  17. use tree\Tree;
  18. use app\admin\model\AdminMenuModel;
  19. class RbacController extends AdminBaseController
  20. {
  21. /**
  22. * 角色管理列表
  23. * @adminMenu(
  24. * 'name' => '角色管理',
  25. * 'parent' => 'admin/User/default',
  26. * 'display'=> true,
  27. * 'hasView'=> true,
  28. * 'order' => 10000,
  29. * 'icon' => '',
  30. * 'remark' => '角色管理',
  31. * 'param' => ''
  32. * )
  33. * @return mixed
  34. * @throws \think\db\exception\DataNotFoundException
  35. * @throws \think\db\exception\ModelNotFoundException
  36. * @throws \think\exception\DbException
  37. */
  38. public function index()
  39. {
  40. $content = hook_one('admin_rbac_index_view');
  41. if (!empty($content)) {
  42. return $content;
  43. }
  44. $data = RoleModel::order(["list_order" => "ASC", "id" => "DESC"])->select();
  45. $this->assign("roles", $data);
  46. return $this->fetch();
  47. }
  48. /**
  49. * 添加角色
  50. * @adminMenu(
  51. * 'name' => '添加角色',
  52. * 'parent' => 'index',
  53. * 'display'=> false,
  54. * 'hasView'=> true,
  55. * 'order' => 10000,
  56. * 'icon' => '',
  57. * 'remark' => '添加角色',
  58. * 'param' => ''
  59. * )
  60. * @return mixed
  61. */
  62. public function roleAdd()
  63. {
  64. $content = hook_one('admin_rbac_role_add_view');
  65. if (!empty($content)) {
  66. return $content;
  67. }
  68. return $this->fetch();
  69. }
  70. /**
  71. * 添加角色提交
  72. * @adminMenu(
  73. * 'name' => '添加角色提交',
  74. * 'parent' => 'index',
  75. * 'display'=> false,
  76. * 'hasView'=> false,
  77. * 'order' => 10000,
  78. * 'icon' => '',
  79. * 'remark' => '添加角色提交',
  80. * 'param' => ''
  81. * )
  82. */
  83. public function roleAddPost()
  84. {
  85. if ($this->request->isPost()) {
  86. $data = $this->request->param();
  87. $result = $this->validate($data, 'role');
  88. if ($result !== true) {
  89. // 验证失败 输出错误信息
  90. $this->error($result);
  91. } else {
  92. $result = RoleModel::insert($data);
  93. if ($result) {
  94. $this->success("添加角色成功", url("rbac/index"));
  95. } else {
  96. $this->error("添加角色失败");
  97. }
  98. }
  99. }
  100. }
  101. /**
  102. * 编辑角色
  103. * @adminMenu(
  104. * 'name' => '编辑角色',
  105. * 'parent' => 'index',
  106. * 'display'=> false,
  107. * 'hasView'=> true,
  108. * 'order' => 10000,
  109. * 'icon' => '',
  110. * 'remark' => '编辑角色',
  111. * 'param' => ''
  112. * )
  113. * @return mixed
  114. * @throws \think\db\exception\DataNotFoundException
  115. * @throws \think\db\exception\ModelNotFoundException
  116. * @throws \think\exception\DbException
  117. */
  118. public function roleEdit()
  119. {
  120. $content = hook_one('admin_rbac_role_edit_view');
  121. if (!empty($content)) {
  122. return $content;
  123. }
  124. $id = $this->request->param("id", 0, 'intval');
  125. if ($id == 1) {
  126. $this->error("超级管理员角色不能被修改!");
  127. }
  128. $data = RoleModel::where("id", $id)->find();
  129. if (!$data) {
  130. $this->error("该角色不存在!");
  131. }
  132. $this->assign("data", $data);
  133. return $this->fetch();
  134. }
  135. /**
  136. * 编辑角色提交
  137. * @adminMenu(
  138. * 'name' => '编辑角色提交',
  139. * 'parent' => 'index',
  140. * 'display'=> false,
  141. * 'hasView'=> false,
  142. * 'order' => 10000,
  143. * 'icon' => '',
  144. * 'remark' => '编辑角色提交',
  145. * 'param' => ''
  146. * )
  147. * @throws \think\Exception
  148. * @throws \think\exception\PDOException
  149. */
  150. public function roleEditPost()
  151. {
  152. $id = $this->request->param("id", 0, 'intval');
  153. if ($id == 1) {
  154. $this->error("超级管理员角色不能被修改!");
  155. }
  156. if ($this->request->isPost()) {
  157. $data = $this->request->param();
  158. $result = $this->validate($data, 'role');
  159. if ($result !== true) {
  160. // 验证失败 输出错误信息
  161. $this->error($result);
  162. } else {
  163. if (RoleModel::update($data) !== false) {
  164. $this->success("保存成功!", url('rbac/index'));
  165. } else {
  166. $this->error("保存失败!");
  167. }
  168. }
  169. }
  170. }
  171. /**
  172. * 删除角色
  173. * @adminMenu(
  174. * 'name' => '删除角色',
  175. * 'parent' => 'index',
  176. * 'display'=> false,
  177. * 'hasView'=> false,
  178. * 'order' => 10000,
  179. * 'icon' => '',
  180. * 'remark' => '删除角色',
  181. * 'param' => ''
  182. * )
  183. * @throws \think\Exception
  184. * @throws \think\exception\PDOException
  185. */
  186. public function roleDelete()
  187. {
  188. if ($this->request->isPost()) {
  189. $id = $this->request->param("id", 0, 'intval');
  190. if ($id == 1) {
  191. $this->error("超级管理员角色不能被删除!");
  192. }
  193. $count = RoleUserModel::where('role_id', $id)->count();
  194. if ($count > 0) {
  195. $this->error("该角色已经有用户!");
  196. } else {
  197. $status = RoleModel::destroy($id);
  198. if (!empty($status)) {
  199. $this->success("删除成功!", url('rbac/index'));
  200. } else {
  201. $this->error("删除失败!");
  202. }
  203. }
  204. }
  205. }
  206. /**
  207. * 设置角色权限
  208. * @adminMenu(
  209. * 'name' => '设置角色权限',
  210. * 'parent' => 'index',
  211. * 'display'=> false,
  212. * 'hasView'=> true,
  213. * 'order' => 10000,
  214. * 'icon' => '',
  215. * 'remark' => '设置角色权限',
  216. * 'param' => ''
  217. * )
  218. * @return mixed
  219. */
  220. public function authorize()
  221. {
  222. $content = hook_one('admin_rbac_authorize_view');
  223. if (!empty($content)) {
  224. return $content;
  225. }
  226. $adminMenuModel = new AdminMenuModel();
  227. //角色ID
  228. $roleId = $this->request->param("id", 0, 'intval');
  229. if (empty($roleId)) {
  230. $this->error("参数错误!");
  231. }
  232. $tree = new Tree();
  233. $tree->icon = ['│ ', '├─ ', '└─ '];
  234. $tree->nbsp = '&nbsp;&nbsp;&nbsp;';
  235. $result = $adminMenuModel->menuCache();
  236. $newMenus = [];
  237. $privilegeData = AuthAccessModel::where("role_id", $roleId)->column("rule_name");//获取权限表数据
  238. foreach ($result as $m) {
  239. $newMenus[$m['id']] = $m;
  240. }
  241. foreach ($result as $n => $t) {
  242. $result[$n]['checked'] = ($this->_isChecked($t, $privilegeData)) ? ' checked' : '';
  243. $result[$n]['level'] = $this->_getLevel($t['id'], $newMenus);
  244. $result[$n]['style'] = empty($t['parent_id']) ? '' : 'display:none;';
  245. $result[$n]['parentIdNode'] = ($t['parent_id']) ? ' class="child-of-node-' . $t['parent_id'] . '"' : '';
  246. }
  247. $str = "<tr id='node-\$id'\$parentIdNode style='\$style'>
  248. <td style='padding-left:30px;'>\$spacer<input type='checkbox' name='menuId[]' value='\$id' level='\$level' \$checked onclick='javascript:checknode(this);'> \$name</td>
  249. </tr>";
  250. $tree->init($result);
  251. $category = $tree->getTree(0, $str);
  252. $this->assign("category", $category);
  253. $this->assign("roleId", $roleId);
  254. return $this->fetch();
  255. }
  256. /**
  257. * 角色授权提交
  258. * @adminMenu(
  259. * 'name' => '角色授权提交',
  260. * 'parent' => 'index',
  261. * 'display'=> false,
  262. * 'hasView'=> false,
  263. * 'order' => 10000,
  264. * 'icon' => '',
  265. * 'remark' => '角色授权提交',
  266. * 'param' => ''
  267. * )
  268. * @throws \think\Exception
  269. * @throws \think\db\exception\DataNotFoundException
  270. * @throws \think\db\exception\ModelNotFoundException
  271. * @throws \think\exception\DbException
  272. * @throws \think\exception\PDOException
  273. */
  274. public function authorizePost()
  275. {
  276. if ($this->request->isPost()) {
  277. $roleId = $this->request->param("roleId", 0, 'intval');
  278. if (!$roleId) {
  279. $this->error("需要授权的角色不存在!");
  280. }
  281. $menuIds = $this->request->param('menuId/a');
  282. if (is_array($menuIds) && count($menuIds) > 0) {
  283. AuthAccessModel::where(["role_id" => $roleId, 'type' => 'admin_url'])->delete();
  284. foreach ($menuIds as $menuId) {
  285. $menu = AdminMenuModel::where("id", $menuId)->field("app,controller,action")->find();
  286. if ($menu) {
  287. $app = $menu['app'];
  288. $model = $menu['controller'];
  289. $action = $menu['action'];
  290. $name = strtolower("$app/$model/$action");
  291. AuthAccessModel::insert(["role_id" => $roleId, "rule_name" => $name, 'type' => 'admin_url']);
  292. }
  293. }
  294. Cache::clear('admin_menus');// 删除后台菜单缓存
  295. $this->success("授权成功!");
  296. } else {
  297. //当没有数据时,清除当前角色授权
  298. AuthAccessModel::where("role_id", $roleId)->delete();
  299. $this->error("没有接收到数据,执行清除授权成功!");
  300. }
  301. }
  302. }
  303. /**
  304. * 检查指定菜单是否有权限
  305. * @param array $menu menu表中数组
  306. * @param $privData
  307. * @return bool
  308. */
  309. private function _isChecked($menu, $privData)
  310. {
  311. $app = $menu['app'];
  312. $model = $menu['controller'];
  313. $action = $menu['action'];
  314. $name = strtolower("$app/$model/$action");
  315. if ($privData) {
  316. if (in_array($name, $privData)) {
  317. return true;
  318. } else {
  319. return false;
  320. }
  321. } else {
  322. return false;
  323. }
  324. }
  325. /**
  326. * 获取菜单深度
  327. * @param $id
  328. * @param array $array
  329. * @param int $i
  330. * @return int
  331. */
  332. protected function _getLevel($id, $array = [], $i = 0)
  333. {
  334. if ($array[$id]['parent_id'] == 0 || empty($array[$array[$id]['parent_id']]) || $array[$id]['parent_id'] == $id) {
  335. return $i;
  336. } else {
  337. $i++;
  338. return $this->_getLevel($array[$id]['parent_id'], $array, $i);
  339. }
  340. }
  341. //角色成员管理
  342. public function member()
  343. {
  344. //TODO 添加角色成员管理
  345. }
  346. }