Admin.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. namespace app\admin\controller\auth;
  3. use app\admin\model\AuthGroup;
  4. use app\admin\model\AuthGroupAccess;
  5. use app\common\controller\Backend;
  6. use fast\Random;
  7. use fast\Tree;
  8. /**
  9. * 管理员管理
  10. *
  11. * @icon fa fa-users
  12. * @remark 一个管理员可以有多个角色组,左侧的菜单根据管理员所拥有的权限进行生成
  13. */
  14. class Admin extends Backend
  15. {
  16. /**
  17. * @var \app\admin\model\Admin
  18. */
  19. protected $model = null;
  20. protected $childrenGroupIds = [];
  21. protected $childrenAdminIds = [];
  22. public function _initialize()
  23. {
  24. parent::_initialize();
  25. $this->model = model('Admin');
  26. $this->childrenAdminIds = $this->auth->getChildrenAdminIds(true);
  27. $this->childrenGroupIds = $this->auth->getChildrenGroupIds(true);
  28. $groupList = collection(AuthGroup::where('id', 'in', $this->childrenGroupIds)->select())->toArray();
  29. Tree::instance()->init($groupList);
  30. $groupdata = [];
  31. if ($this->auth->isSuperAdmin()) {
  32. $result = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0));
  33. foreach ($result as $k => $v) {
  34. $groupdata[$v['id']] = $v['name'];
  35. }
  36. } else {
  37. $result = [];
  38. $groups = $this->auth->getGroups();
  39. foreach ($groups as $m => $n) {
  40. $childlist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray($n['id']));
  41. $temp = [];
  42. foreach ($childlist as $k => $v) {
  43. $temp[$v['id']] = $v['name'];
  44. }
  45. $result[__($n['name'])] = $temp;
  46. }
  47. $groupdata = $result;
  48. }
  49. $this->view->assign('groupdata', $groupdata);
  50. $this->assignconfig("admin", ['id' => $this->auth->id]);
  51. }
  52. /**
  53. * 查看
  54. */
  55. public function index()
  56. {
  57. if ($this->request->isAjax()) {
  58. //如果发送的来源是Selectpage,则转发到Selectpage
  59. if ($this->request->request('keyField')) {
  60. return $this->selectpage();
  61. }
  62. $childrenGroupIds = $this->childrenGroupIds;
  63. $groupName = AuthGroup::where('id', 'in', $childrenGroupIds)
  64. ->column('id,name');
  65. $authGroupList = AuthGroupAccess::where('group_id', 'in', $childrenGroupIds)
  66. ->field('uid,group_id')
  67. ->select();
  68. $adminGroupName = [];
  69. foreach ($authGroupList as $k => $v) {
  70. if (isset($groupName[$v['group_id']])) {
  71. $adminGroupName[$v['uid']][$v['group_id']] = $groupName[$v['group_id']];
  72. }
  73. }
  74. $groups = $this->auth->getGroups();
  75. foreach ($groups as $m => $n) {
  76. $adminGroupName[$this->auth->id][$n['id']] = $n['name'];
  77. }
  78. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  79. $total = $this->model
  80. ->where($where)
  81. ->where('id', 'in', $this->childrenAdminIds)
  82. ->order($sort, $order)
  83. ->count();
  84. $list = $this->model
  85. ->where($where)
  86. ->where('id', 'in', $this->childrenAdminIds)
  87. ->field(['password', 'salt', 'token'], true)
  88. ->order($sort, $order)
  89. ->limit($offset, $limit)
  90. ->select();
  91. foreach ($list as $k => &$v) {
  92. $groups = isset($adminGroupName[$v['id']]) ? $adminGroupName[$v['id']] : [];
  93. $v['groups'] = implode(',', array_keys($groups));
  94. $v['groups_text'] = implode(',', array_values($groups));
  95. }
  96. unset($v);
  97. $result = array("total" => $total, "rows" => $list);
  98. return json($result);
  99. }
  100. return $this->view->fetch();
  101. }
  102. /**
  103. * 添加
  104. */
  105. public function add()
  106. {
  107. if ($this->request->isPost()) {
  108. $this->token();
  109. $params = $this->request->post("row/a");
  110. if ($params) {
  111. $params['salt'] = Random::alnum();
  112. $params['password'] = md5(md5($params['password']) . $params['salt']);
  113. $params['avatar'] = '/assets/img/avatar.png'; //设置新管理员默认头像。
  114. $result = $this->model->validate('Admin.add')->save($params);
  115. if ($result === false) {
  116. $this->error($this->model->getError());
  117. }
  118. $group = $this->request->post("group/a");
  119. //过滤不允许的组别,避免越权
  120. $group = array_intersect($this->childrenGroupIds, $group);
  121. $dataset = [];
  122. foreach ($group as $value) {
  123. $dataset[] = ['uid' => $this->model->id, 'group_id' => $value];
  124. }
  125. model('AuthGroupAccess')->saveAll($dataset);
  126. $this->success();
  127. }
  128. $this->error();
  129. }
  130. return $this->view->fetch();
  131. }
  132. /**
  133. * 编辑
  134. */
  135. public function edit($ids = null)
  136. {
  137. $row = $this->model->get(['id' => $ids]);
  138. if (!$row) {
  139. $this->error(__('No Results were found'));
  140. }
  141. if ($this->request->isPost()) {
  142. $this->token();
  143. $params = $this->request->post("row/a");
  144. if ($params) {
  145. if ($params['password']) {
  146. $params['salt'] = Random::alnum();
  147. $params['password'] = md5(md5($params['password']) . $params['salt']);
  148. } else {
  149. unset($params['password'], $params['salt']);
  150. }
  151. //这里需要针对username和email做唯一验证
  152. $adminValidate = \think\Loader::validate('Admin');
  153. $adminValidate->rule([
  154. 'username' => 'require|max:50|unique:admin,username,' . $row->id,
  155. 'email' => 'require|email|unique:admin,email,' . $row->id
  156. ]);
  157. $result = $row->validate('Admin.edit')->save($params);
  158. if ($result === false) {
  159. $this->error($row->getError());
  160. }
  161. // 先移除所有权限
  162. model('AuthGroupAccess')->where('uid', $row->id)->delete();
  163. $group = $this->request->post("group/a");
  164. // 过滤不允许的组别,避免越权
  165. $group = array_intersect($this->childrenGroupIds, $group);
  166. $dataset = [];
  167. foreach ($group as $value) {
  168. $dataset[] = ['uid' => $row->id, 'group_id' => $value];
  169. }
  170. model('AuthGroupAccess')->saveAll($dataset);
  171. $this->success();
  172. }
  173. $this->error();
  174. }
  175. $grouplist = $this->auth->getGroups($row['id']);
  176. $groupids = [];
  177. foreach ($grouplist as $k => $v) {
  178. $groupids[] = $v['id'];
  179. }
  180. $this->view->assign("row", $row);
  181. $this->view->assign("groupids", $groupids);
  182. return $this->view->fetch();
  183. }
  184. /**
  185. * 删除
  186. */
  187. public function del($ids = "")
  188. {
  189. if ($ids) {
  190. // 避免越权删除管理员
  191. $childrenGroupIds = $this->childrenGroupIds;
  192. $adminList = $this->model->where('id', 'in', $ids)->where('id', 'in', function ($query) use ($childrenGroupIds) {
  193. $query->name('auth_group_access')->where('group_id', 'in', $childrenGroupIds)->field('uid');
  194. })->select();
  195. if ($adminList) {
  196. $deleteIds = [];
  197. foreach ($adminList as $k => $v) {
  198. $deleteIds[] = $v->id;
  199. }
  200. $deleteIds = array_diff($deleteIds, [$this->auth->id]);
  201. if ($deleteIds) {
  202. $this->model->destroy($deleteIds);
  203. model('AuthGroupAccess')->where('uid', 'in', $deleteIds)->delete();
  204. $this->success();
  205. }
  206. }
  207. }
  208. $this->error();
  209. }
  210. /**
  211. * 批量更新
  212. * @internal
  213. */
  214. public function multi($ids = "")
  215. {
  216. // 管理员禁止批量操作
  217. $this->error();
  218. }
  219. /**
  220. * 下拉搜索
  221. */
  222. public function selectpage()
  223. {
  224. $this->dataLimit = 'auth';
  225. $this->dataLimitField = 'id';
  226. return parent::selectpage();
  227. }
  228. }