Admin.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. $this->assignconfig("admin", ['id' => $this->auth->id]);
  40. }
  41. /**
  42. * 查看
  43. */
  44. public function index()
  45. {
  46. if ($this->request->isAjax())
  47. {
  48. $groupData = model('AuthGroup')->where('status', 'normal')->column('id,name');
  49. $childrenAdminIds = [];
  50. $authGroupList = model('AuthGroupAccess')
  51. ->field('uid,group_id')
  52. ->where('group_id', 'in', $this->childrenIds)
  53. ->select();
  54. $adminGroupName = [];
  55. foreach ($authGroupList as $k => $v)
  56. {
  57. $childrenAdminIds[] = $v['uid'];
  58. if (isset($groupData[$v['group_id']]))
  59. $adminGroupName[$v['uid']][$v['group_id']] = $groupData[$v['group_id']];
  60. }
  61. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  62. $total = $this->model
  63. ->where($where)
  64. ->where('id', 'in', $childrenAdminIds)
  65. ->order($sort, $order)
  66. ->count();
  67. $list = $this->model
  68. ->where($where)
  69. ->where('id', 'in', $childrenAdminIds)
  70. ->field(['password', 'salt', 'token'], true)
  71. ->order($sort, $order)
  72. ->limit($offset, $limit)
  73. ->select();
  74. foreach ($list as $k => &$v)
  75. {
  76. $groups = isset($adminGroupName[$v['id']]) ? $adminGroupName[$v['id']] : [];
  77. $v['groups'] = implode(',', array_keys($groups));
  78. $v['groups_text'] = implode(',', array_values($groups));
  79. }
  80. $result = array("total" => $total, "rows" => $list);
  81. return json($result);
  82. }
  83. return $this->view->fetch();
  84. }
  85. /**
  86. * 添加
  87. */
  88. public function add()
  89. {
  90. if ($this->request->isPost())
  91. {
  92. $params = $this->request->post("row/a");
  93. if ($params)
  94. {
  95. $params['salt'] = Random::alnum();
  96. $params['password'] = md5(md5($params['password']) . $params['salt']);
  97. $params['avatar'] = '/assets/img/avatar.png'; //设置新管理员默认头像。
  98. $admin = $this->model->create($params);
  99. $group = $this->request->post("group/a");
  100. //过滤不允许的组别,避免越权
  101. $group = array_intersect($this->childrenIds, $group);
  102. $dataset = [];
  103. foreach ($group as $value)
  104. {
  105. $dataset[] = ['uid' => $admin->id, 'group_id' => $value];
  106. }
  107. model('AuthGroupAccess')->saveAll($dataset);
  108. $this->success();
  109. }
  110. $this->error();
  111. }
  112. return $this->view->fetch();
  113. }
  114. /**
  115. * 编辑
  116. */
  117. public function edit($ids = NULL)
  118. {
  119. $row = $this->model->get(['id' => $ids]);
  120. if (!$row)
  121. $this->error(__('No Results were found'));
  122. if ($this->request->isPost())
  123. {
  124. $params = $this->request->post("row/a");
  125. if ($params)
  126. {
  127. if ($params['password'])
  128. {
  129. $params['salt'] = Random::alnum();
  130. $params['password'] = md5(md5($params['password']) . $params['salt']);
  131. }
  132. else
  133. {
  134. unset($params['password'], $params['salt']);
  135. }
  136. $row->save($params);
  137. // 先移除所有权限
  138. model('AuthGroupAccess')->where('uid', $row->id)->delete();
  139. $group = $this->request->post("group/a");
  140. // 过滤不允许的组别,避免越权
  141. $group = array_intersect($this->childrenIds, $group);
  142. $dataset = [];
  143. foreach ($group as $value)
  144. {
  145. $dataset[] = ['uid' => $row->id, 'group_id' => $value];
  146. }
  147. model('AuthGroupAccess')->saveAll($dataset);
  148. $this->success();
  149. }
  150. $this->error();
  151. }
  152. $grouplist = $this->auth->getGroups($row['id']);
  153. $groupids = [];
  154. foreach ($grouplist as $k => $v)
  155. {
  156. $groupids[] = $v['id'];
  157. }
  158. $this->view->assign("row", $row);
  159. $this->view->assign("groupids", $groupids);
  160. return $this->view->fetch();
  161. }
  162. /**
  163. * 删除
  164. */
  165. public function del($ids = "")
  166. {
  167. if ($ids)
  168. {
  169. // 避免越权删除管理员
  170. $childrenGroupIds = $this->childrenIds;
  171. $adminList = $this->model->where('id', 'in', $ids)->where('id', 'in', function($query) use($childrenGroupIds) {
  172. $query->name('auth_group_access')->where('group_id', 'in', $childrenGroupIds)->field('uid');
  173. })->select();
  174. if ($adminList)
  175. {
  176. $deleteIds = [];
  177. foreach ($adminList as $k => $v)
  178. {
  179. $deleteIds[] = $v->id;
  180. }
  181. $deleteIds = array_diff($deleteIds, [$this->auth->id]);
  182. if ($deleteIds)
  183. {
  184. $this->model->destroy($deleteIds);
  185. model('AuthGroupAccess')->where('uid', 'in', $deleteIds)->delete();
  186. $this->success();
  187. }
  188. }
  189. }
  190. $this->error();
  191. }
  192. /**
  193. * 批量更新
  194. * @internal
  195. */
  196. public function multi($ids = "")
  197. {
  198. // 管理员禁止批量操作
  199. $this->error();
  200. }
  201. }