AuthRole.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. /**
  3. * 易优CMS
  4. * ============================================================================
  5. * 版权所有 2016-2028 海南赞赞网络科技有限公司,并保留所有权利。
  6. * 网站地址: http://www.eyoucms.com
  7. * ----------------------------------------------------------------------------
  8. * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
  9. * ============================================================================
  10. * Author: 小虎哥 <1105415366@qq.com>
  11. * Date: 2018-4-3
  12. */
  13. namespace app\admin\controller;
  14. use think\Page;
  15. use think\Db;
  16. use think\Validate;
  17. class AuthRole extends Base {
  18. public function _initialize() {
  19. parent::_initialize();
  20. $this->language_access(); // 多语言功能操作权限
  21. }
  22. /**
  23. * 权限组管理
  24. */
  25. public function index()
  26. {
  27. $map = array();
  28. $pid = input('pid/d');
  29. $keywords = input('keywords/s');
  30. if (!empty($keywords)) {
  31. $map['c.name'] = array('LIKE', "%{$keywords}%");
  32. }
  33. $AuthRole = M('auth_role');
  34. $count = $AuthRole->alias('c')->where($map)->count();// 查询满足要求的总记录数
  35. $Page = new Page($count, 10);// 实例化分页类 传入总记录数和每页显示的记录数
  36. $fields = "c.*,s.name AS pname";
  37. $list = DB::name('auth_role')
  38. ->field($fields)
  39. ->alias('c')
  40. ->join('__AUTH_ROLE__ s','s.id = c.pid','LEFT')
  41. ->where($map)
  42. ->order('c.id asc')
  43. ->limit($Page->firstRow.','.$Page->listRows)
  44. ->select();
  45. $show = $Page->show();// 分页显示输出
  46. $this->assign('page',$show);// 赋值分页输出
  47. $this->assign('list',$list);// 赋值数据集
  48. $this->assign('pager',$Page);// 赋值分页集
  49. return $this->fetch();
  50. }
  51. /**
  52. * 新增权限组
  53. */
  54. public function add()
  55. {
  56. if (IS_POST) {
  57. $rule = array(
  58. 'name' => 'require',
  59. );
  60. $msg = array(
  61. 'name.require' => '权限组名称不能为空!',
  62. );
  63. $data = array(
  64. 'name' => trim(input('name/s')),
  65. );
  66. $validate = new Validate($rule, $msg);
  67. $result = $validate->check($data);
  68. if(!$result){
  69. $this->error($validate->getError());
  70. }
  71. $model = model('AuthRole');
  72. $count = $model->where('name', $data['name'])->count();
  73. if(! empty($count)){
  74. $this->error('该权限组名称已存在,请检查');
  75. }
  76. $role_id = $model->saveAuthRole(input());
  77. if($role_id){
  78. adminLog('新增权限组:'.$data['name']);
  79. $admin_role_list = model('AuthRole')->getRoleAll();
  80. $this->success('操作成功', url('AuthRole/index'), ['role_id'=>$role_id,'role_name'=>$data['name'],'admin_role_list'=>json_encode($admin_role_list)]);
  81. }else{
  82. $this->error('操作失败');
  83. }
  84. }
  85. // 权限组
  86. $admin_role_list = model('AuthRole')->getRoleAll();
  87. $this->assign('admin_role_list', $admin_role_list);
  88. // 模块组
  89. $modules = getAllMenu();
  90. $this->assign('modules', $modules);
  91. // 权限集
  92. // $singleArr = array_multi2single($modules, 'child'); // 多维数组转为一维
  93. $auth_rules = get_auth_rule(['is_modules'=>1]);
  94. $auth_rule_list = group_same_key($auth_rules, 'menu_id');
  95. $this->assign('auth_rule_list', $auth_rule_list);
  96. // 栏目
  97. $arctype_data = $arctype_array = array();
  98. $arctype = M('arctype')->select();
  99. if(! empty($arctype)){
  100. foreach ($arctype as $item){
  101. if($item['parent_id'] <= 0){
  102. $arctype_data[] = $item;
  103. }
  104. $arctype_array[$item['parent_id']][] = $item;
  105. }
  106. }
  107. $this->assign('arctypes', $arctype_data);
  108. $this->assign('arctype_array', $arctype_array);
  109. // 插件
  110. $plugins = false;
  111. $web_weapp_switch = tpCache('web.web_weapp_switch');
  112. if (1 == $web_weapp_switch) {
  113. $plugins = model('Weapp')->getList(['status'=>1]);
  114. }
  115. $this->assign('plugins', $plugins);
  116. return $this->fetch();
  117. }
  118. public function edit()
  119. {
  120. $id = input('id/d', 0);
  121. if($id <= 0){
  122. $this->error('非法访问');
  123. }
  124. if (IS_POST) {
  125. $rule = array(
  126. 'name' => 'require',
  127. );
  128. $msg = array(
  129. 'name.require' => '权限组名称不能为空!',
  130. );
  131. $data = array(
  132. 'name' => trim(input('name/s')),
  133. );
  134. $validate = new Validate($rule, $msg);
  135. $result = $validate->check($data);
  136. if(!$result){
  137. $this->error($validate->getError());
  138. }
  139. $model = model('AuthRole');
  140. $count = $model->where('name', $data['name'])
  141. ->where('id', '<>', $id)
  142. ->count();
  143. if(! empty($count)){
  144. $this->error('该权限组名称已存在,请检查');
  145. }
  146. $role_id = $model->saveAuthRole(input(), true);
  147. if($role_id){
  148. adminLog('编辑权限组:'.$data['name']);
  149. $this->success('操作成功', url('AuthRole/index'), ['role_id'=>$role_id,'role_name'=>$data['name']]);
  150. }else{
  151. $this->error('操作失败');
  152. }
  153. }
  154. $model = model('AuthRole');
  155. $info = $model->getRole(array('id' => $id));
  156. if(empty($info)){
  157. $this->error('数据不存在,请联系管理员!');
  158. }
  159. $this->assign('info', $info);
  160. // 权限组
  161. $admin_role_list = model('AuthRole')->getRoleAll();
  162. $this->assign('admin_role_list', $admin_role_list);
  163. // 模块组
  164. $modules = getAllMenu();
  165. $this->assign('modules', $modules);
  166. // 权限集
  167. $auth_rules = get_auth_rule(['is_modules'=>1]);
  168. $auth_rule_list = group_same_key($auth_rules, 'menu_id');
  169. $this->assign('auth_rule_list', $auth_rule_list);
  170. // 栏目
  171. $arctype_data = $arctype_array = array();
  172. $arctype = M('arctype')->select();
  173. if(! empty($arctype)){
  174. foreach ($arctype as $item){
  175. if($item['parent_id'] <= 0){
  176. $arctype_data[] = $item;
  177. }
  178. $arctype_array[$item['parent_id']][] = $item;
  179. }
  180. }
  181. $this->assign('arctypes', $arctype_data);
  182. $this->assign('arctype_array', $arctype_array);
  183. // 插件
  184. $plugins = false;
  185. $web_weapp_switch = tpCache('web.web_weapp_switch');
  186. if (1 == $web_weapp_switch) {
  187. $plugins = model('Weapp')->getList(['status'=>1]);
  188. }
  189. $this->assign('plugins', $plugins);
  190. return $this->fetch();
  191. }
  192. public function del()
  193. {
  194. $id_arr = input('del_id/a');
  195. $id_arr = eyIntval($id_arr);
  196. if (!empty($id_arr)) {
  197. $count = M('auth_role')->where(['built_in'=>1,'id'=>['IN',$id_arr]])->count();
  198. if (!empty($count)) {
  199. $this->error('系统内置不允许删除!');
  200. }
  201. $role = M('auth_role')->where("pid",'IN',$id_arr)->select();
  202. if ($role) {
  203. $this->error('请先清空该权限组下的子权限组');
  204. }
  205. $role_admin = M('admin')->where("role_id",'IN',$id_arr)->select();
  206. if ($role_admin) {
  207. $this->error('请先清空所属该权限组的管理员');
  208. } else {
  209. $r = M('auth_role')->where("id",'IN',$id_arr)->delete();
  210. if($r){
  211. adminLog('删除权限组');
  212. $this->success('删除成功');
  213. }else{
  214. $this->error('删除失败');
  215. }
  216. }
  217. } else {
  218. $this->error('参数有误');
  219. }
  220. }
  221. }