Admin.php 9.3 KB

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