Admin.php 8.9 KB

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