Manager.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // |
  4. // +----------------------------------------------------------------------
  5. // |
  6. // +----------------------------------------------------------------------
  7. // |
  8. // +----------------------------------------------------------------------
  9. // |
  10. // +----------------------------------------------------------------------
  11. // | Original reference: https://gitee.com/karson/fastadmin
  12. // +----------------------------------------------------------------------
  13. // +----------------------------------------------------------------------
  14. // | 管理员管理
  15. // +----------------------------------------------------------------------
  16. namespace app\admin\controller\auth;
  17. use app\admin\model\AdminUser;
  18. use app\admin\model\AuthGroup;
  19. use app\admin\model\AuthGroupAccess;
  20. use app\common\controller\Backend;
  21. use think\exception\ValidateException;
  22. use think\facade\Db;
  23. use util\Tree;
  24. class Manager extends Backend
  25. {
  26. protected $searchFields = 'id,username,nickname';
  27. protected $childrenGroupIds = [];
  28. protected $childrenAdminIds = [];
  29. //无需要权限判断的方法
  30. protected $noNeedRight = ['get_group_list'];
  31. protected function initialize()
  32. {
  33. parent::initialize();
  34. $this->modelClass = new AdminUser;
  35. $this->childrenAdminIds = $this->auth->getChildrenAdminIds($this->auth->isAdministrator());
  36. $this->childrenGroupIds = $this->auth->getChildrenGroupIds($this->auth->isAdministrator());
  37. $this->assignconfig("admin", ['id' => $this->auth->id]);
  38. }
  39. /**
  40. * 管理员管理列表
  41. */
  42. public function index()
  43. {
  44. if ($this->request->isAjax()) {
  45. //如果发送的来源是Selectpage,则转发到Selectpage
  46. if ($this->request->request('keyField')) {
  47. return $this->selectpage();
  48. }
  49. $childrenGroupIds = $this->childrenGroupIds;
  50. $groupName = AuthGroup::where('id', 'in', $childrenGroupIds)
  51. ->column('title', 'id');
  52. $authGroupList = AuthGroupAccess::where('group_id', 'in', $childrenGroupIds)
  53. ->field('uid,group_id')
  54. ->select();
  55. $adminGroupName = [];
  56. foreach ($authGroupList as $k => $v) {
  57. if (isset($groupName[$v['group_id']])) {
  58. $adminGroupName[$v['uid']][$v['group_id']] = $groupName[$v['group_id']];
  59. }
  60. }
  61. $groups = $this->auth->getGroups();
  62. foreach ($groups as $m => $n) {
  63. $adminGroupName[$this->auth->id][$n['id']] = $n['title'];
  64. }
  65. [$page, $limit, $where, $sort, $order] = $this->buildTableParames();
  66. $list = $this->modelClass
  67. ->where($where)
  68. ->where('id', 'in', $this->childrenAdminIds)
  69. ->withoutField('password', 'salt', 'token')
  70. ->order($sort, $order)
  71. ->paginate($limit);
  72. foreach ($list as $k => &$v) {
  73. $groups = $adminGroupName[$v['id']] ?? [];
  74. $v['groups'] = implode(',', array_keys($groups));
  75. $v['groups_text'] = implode(',', array_values($groups));
  76. }
  77. unset($v);
  78. $result = ["code" => 0, 'count' => $list->total(), "data" => $list->items()];
  79. return json($result);
  80. }
  81. return $this->fetch();
  82. }
  83. /**
  84. * 添加管理员
  85. */
  86. public function add()
  87. {
  88. if ($this->request->isPost()) {
  89. $this->token();
  90. $params = $this->request->param('row/a');
  91. Db::startTrans();
  92. try {
  93. $this->validate($params, 'AdminUser.insert');
  94. $passwordinfo = encrypt_password($params['password']); //对密码进行处理
  95. $params['password'] = $passwordinfo['password'];
  96. $params['encrypt'] = $passwordinfo['encrypt'];
  97. $params['avatar'] = '/assets/img/avatar.png'; //设置新管理员默认头像。
  98. $this->modelClass->save($params);
  99. $group = explode(',', $this->request->param("group"));
  100. //过滤不允许的组别,避免越权
  101. $group = array_intersect($this->childrenGroupIds, $group);
  102. if (!$group) {
  103. throw new \Exception('父组别超出权限范围');
  104. }
  105. $dataset = [];
  106. foreach ($group as $value) {
  107. $dataset[] = ['uid' => $this->modelClass->id, 'group_id' => $value];
  108. }
  109. (new AuthGroupAccess())->saveAll($dataset);
  110. Db::commit();
  111. } catch (ValidateException | \Exception $e) {
  112. Db::rollback();
  113. $this->error($e->getMessage());
  114. }
  115. $this->success("添加成功!", url('index'));
  116. }
  117. return $this->fetch();
  118. }
  119. /**
  120. * 管理员编辑
  121. */
  122. public function edit()
  123. {
  124. $id = $this->request->param('id/d', 0);
  125. $row = $this->modelClass->find($id);
  126. if (!$row) {
  127. $this->error('记录未找到');
  128. }
  129. if (!in_array($row->id, $this->childrenAdminIds)) {
  130. $this->error('没有权限操作!');
  131. }
  132. if ($this->request->isPost()) {
  133. $this->token();
  134. $params = $this->request->param('row/a');
  135. Db::startTrans();
  136. try {
  137. $this->validate($params, 'AdminUser.update');
  138. //密码为空,表示不修改密码
  139. if (isset($params['password']) && $params['password']) {
  140. $passwordinfo = encrypt_password($params['password']); //对密码进行处理
  141. $params['encrypt'] = $passwordinfo['encrypt'];
  142. $params['password'] = $passwordinfo['password'];
  143. } else {
  144. unset($params['password'], $params['encrypt']);
  145. }
  146. $row->save($params);
  147. // 先移除所有权限
  148. AuthGroupAccess::where('uid', $row->id)->delete();
  149. $group = explode(',', $this->request->param("group"));
  150. // 过滤不允许的组别,避免越权
  151. $group = array_intersect($this->childrenGroupIds, $group);
  152. if (!$group) {
  153. throw new \Exception('父组别超出权限范围');
  154. }
  155. $dataset = [];
  156. foreach ($group as $value) {
  157. $dataset[] = ['uid' => $row->id, 'group_id' => $value];
  158. }
  159. (new AuthGroupAccess())->saveAll($dataset);
  160. Db::commit();
  161. } catch (ValidateException | \Exception $e) {
  162. Db::rollback();
  163. $this->error($e->getMessage());
  164. }
  165. $this->success("修改成功!");
  166. }
  167. $groupIds = $this->auth->getGroupIds($row['id']);
  168. $this->assign("data", $row);
  169. $this->view->assign("groupids", implode(',', $groupIds));
  170. return $this->fetch();
  171. }
  172. /**
  173. * 管理员删除
  174. */
  175. public function del()
  176. {
  177. if (false === $this->request->isPost()) {
  178. $this->error('未知参数');
  179. }
  180. $id = $this->request->param('id/a');
  181. if (empty($id)) {
  182. $this->error('请指定需要删除的用户ID!');
  183. }
  184. $ids = array_intersect($this->childrenAdminIds, array_filter($id));
  185. // 避免越权删除管理员
  186. $childrenGroupIds = $this->childrenGroupIds;
  187. $adminList = $this->modelClass->where('id', 'in', $ids)->where('id', 'in', function ($query) use ($childrenGroupIds) {
  188. $query->name('auth_group_access')->where('group_id', 'in', $childrenGroupIds)->field('uid');
  189. })->select();
  190. if ($adminList) {
  191. $deleteIds = [];
  192. foreach ($adminList as $k => $v) {
  193. $deleteIds[] = $v->id;
  194. }
  195. $deleteIds = array_values(array_diff($deleteIds, [$this->auth->id]));
  196. if ($deleteIds) {
  197. Db::startTrans();
  198. try {
  199. $this->modelClass->destroy($deleteIds);
  200. AuthGroupAccess::where('uid', 'in', $deleteIds)->delete();
  201. Db::commit();
  202. } catch (\Exception $e) {
  203. Db::rollback();
  204. $this->error($e->getMessage());
  205. }
  206. $this->success("删除成功!");
  207. }
  208. }
  209. $this->error('没有权限删除!');
  210. }
  211. //批量更新.
  212. public function multi()
  213. {
  214. // 管理员禁止批量操作
  215. $this->error();
  216. }
  217. public function get_group_list()
  218. {
  219. $keyValue = $this->request->param('keyValue');
  220. if ($keyValue) {
  221. $groupList = AuthGroup::where('id', 'in', $keyValue)->select()->toArray();
  222. foreach ($groupList as $k => $v) {
  223. $groupdata[] = ['id' => $v['id'], 'name' => $v['title']];
  224. }
  225. return ['count' => count($groupdata), 'data' => $groupdata];
  226. }
  227. $groupList = AuthGroup::where('id', 'in', $this->childrenGroupIds)->select()->toArray();
  228. Tree::instance()->init($groupList);
  229. $groupdata = [];
  230. if ($this->auth->isAdministrator()) {
  231. $result = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'title');
  232. foreach ($result as $k => $v) {
  233. $groupdata[] = ['id' => $v['id'], 'name' => $v['title']];
  234. }
  235. } else {
  236. $groups = $this->auth->getGroups();
  237. foreach ($groups as $m => $n) {
  238. $childlist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray($n['id']), 'title');
  239. foreach ($childlist as $k => $v) {
  240. $groupdata[] = ['id' => $v['id'], 'name' => $v['title']];
  241. }
  242. }
  243. }
  244. return ['count' => count($groupdata), 'data' => $groupdata];
  245. }
  246. }