Admin.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. namespace app\admin\controller\auth;
  3. use app\common\controller\Backend;
  4. use fast\Random;
  5. use fast\Tree;
  6. /**
  7. * 管理员管理
  8. *
  9. * @icon fa fa-users
  10. * @remark 一个管理员可以有多个角色组,左侧的菜单根据管理员所拥有的权限进行生成
  11. */
  12. class Admin extends Backend
  13. {
  14. protected $model = null;
  15. //当前登录管理员所有子节点组别
  16. protected $childrenIds = [];
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. $this->model = model('Admin');
  21. $groups = $this->auth->getGroups();
  22. // 取出所有分组
  23. $grouplist = model('AuthGroup')->all(['status' => 'normal']);
  24. $objlist = [];
  25. foreach ($groups as $K => $v)
  26. {
  27. // 取出包含自己的所有子节点
  28. $childrenlist = Tree::instance()->init($grouplist)->getChildren($v['id'], TRUE);
  29. $obj = Tree::instance()->init($childrenlist)->getTreeArray($v['pid']);
  30. $objlist = array_merge($objlist, Tree::instance()->getTreeList($obj));
  31. }
  32. $groupdata = [];
  33. foreach ($objlist as $k => $v)
  34. {
  35. $groupdata[$v['id']] = $v['name'];
  36. }
  37. $this->childrenIds = array_keys($groupdata);
  38. $this->view->assign('groupdata', $groupdata);
  39. }
  40. /**
  41. * 查看
  42. */
  43. public function index()
  44. {
  45. if ($this->request->isAjax())
  46. {
  47. $childrenAdminIds = model('AuthGroupAccess')
  48. ->field('uid')
  49. ->where('group_id', 'in', $this->childrenIds)
  50. ->column('uid');
  51. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  52. $total = $this->model
  53. ->where($where)
  54. ->where('id', 'in', $childrenAdminIds)
  55. ->order($sort, $order)
  56. ->count();
  57. $list = $this->model
  58. ->where($where)
  59. ->where('id', 'in', $childrenAdminIds)
  60. ->order($sort, $order)
  61. ->limit($offset, $limit)
  62. ->select();
  63. $result = array("total" => $total, "rows" => $list);
  64. return json($result);
  65. }
  66. return $this->view->fetch();
  67. }
  68. /**
  69. * 添加
  70. */
  71. public function add()
  72. {
  73. if ($this->request->isPost())
  74. {
  75. $this->code = -1;
  76. $params = $this->request->post("row/a");
  77. if ($params)
  78. {
  79. $params['salt'] = Random::alnum();
  80. $params['password'] = md5(md5($params['password']) . $params['salt']);
  81. $admin = $this->model->create($params);
  82. $group = $this->request->post("group/a");
  83. //过滤不允许的组别,避免越权
  84. $group = array_intersect($this->childrenIds, $group);
  85. $dataset = [];
  86. foreach ($group as $value)
  87. {
  88. $dataset[] = ['uid' => $admin->id, 'group_id' => $value];
  89. }
  90. model('AuthGroupAccess')->saveAll($dataset);
  91. $this->code = 1;
  92. }
  93. return;
  94. }
  95. return $this->view->fetch();
  96. }
  97. /**
  98. * 编辑
  99. */
  100. public function edit($ids = NULL)
  101. {
  102. $row = $this->model->get(['id' => $ids]);
  103. if (!$row)
  104. $this->error(__('No Results were found'));
  105. if ($this->request->isPost())
  106. {
  107. $this->code = -1;
  108. $params = $this->request->post("row/a");
  109. if ($params)
  110. {
  111. if ($params['password'])
  112. {
  113. $params['salt'] = Random::alnum();
  114. $params['password'] = md5(md5($params['password']) . $params['salt']);
  115. }
  116. else
  117. {
  118. unset($params['password'], $params['salt']);
  119. }
  120. $row->save($params);
  121. // 先移除所有权限
  122. model('AuthGroupAccess')->where('uid', $row->id)->delete();
  123. $group = $this->request->post("group/a");
  124. // 过滤不允许的组别,避免越权
  125. $group = array_intersect($this->childrenIds, $group);
  126. $dataset = [];
  127. foreach ($group as $value)
  128. {
  129. $dataset[] = ['uid' => $row->id, 'group_id' => $value];
  130. }
  131. model('AuthGroupAccess')->saveAll($dataset);
  132. $this->code = 1;
  133. }
  134. return;
  135. }
  136. $grouplist = $this->auth->getGroups($row['id']);
  137. $groupids = [];
  138. foreach ($grouplist as $k => $v)
  139. {
  140. $groupids[] = $v['id'];
  141. }
  142. $this->view->assign("row", $row);
  143. $this->view->assign("groupids", $groupids);
  144. return $this->view->fetch();
  145. }
  146. /**
  147. * 删除
  148. */
  149. public function del($ids = "")
  150. {
  151. $this->code = -1;
  152. if ($ids)
  153. {
  154. // 避免越权删除管理员
  155. $childrenGroupIds = $this->childrenIds;
  156. $adminList = $this->model->where('id', 'in', $ids)->where('id', 'in', function($query) use($childrenGroupIds) {
  157. $query->name('auth_group_access')->where('group_id', 'in', $childrenGroupIds)->field('uid');
  158. })->select();
  159. if ($adminList)
  160. {
  161. $deleteIds = [];
  162. foreach ($adminList as $k => $v)
  163. {
  164. $deleteIds[] = $v->id;
  165. }
  166. $deleteIds = array_diff($deleteIds, [$this->auth->id]);
  167. if ($deleteIds)
  168. {
  169. $this->model->destroy($deleteIds);
  170. model('AuthGroupAccess')->where('uid', 'in', $deleteIds)->delete();
  171. $this->code = 1;
  172. }
  173. }
  174. }
  175. return;
  176. }
  177. /**
  178. * 批量更新
  179. * @internal
  180. */
  181. public function multi($ids = "")
  182. {
  183. // 管理员禁止批量操作
  184. $this->code = -1;
  185. }
  186. }