Admin.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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($this->auth->isSuperAdmin());
  30. $this->childrenGroupIds = $this->auth->getChildrenGroupIds($this->auth->isSuperAdmin());
  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. //设置过滤方法
  61. $this->request->filter(['strip_tags', 'trim']);
  62. if ($this->request->isAjax()) {
  63. //如果发送的来源是Selectpage,则转发到Selectpage
  64. if ($this->request->request('keyField')) {
  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. if (isset($groupName[$v['group_id']])) {
  76. $adminGroupName[$v['uid']][$v['group_id']] = $groupName[$v['group_id']];
  77. }
  78. }
  79. $groups = $this->auth->getGroups();
  80. foreach ($groups as $m => $n) {
  81. $adminGroupName[$this->auth->id][$n['id']] = $n['name'];
  82. }
  83. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  84. $list = $this->model
  85. ->where($where)
  86. ->where('id', 'in', $this->childrenAdminIds)
  87. ->field(['password', 'salt', 'token'], true)
  88. ->order($sort, $order)
  89. ->paginate($limit);
  90. foreach ($list as $k => &$v) {
  91. $groups = isset($adminGroupName[$v['id']]) ? $adminGroupName[$v['id']] : [];
  92. $v['groups'] = implode(',', array_keys($groups));
  93. $v['groups_text'] = implode(',', array_values($groups));
  94. }
  95. unset($v);
  96. $result = array("total" => $list->total(), "rows" => $list->items());
  97. return json($result);
  98. }
  99. return $this->view->fetch();
  100. }
  101. /**
  102. * 添加
  103. */
  104. public function add()
  105. {
  106. if ($this->request->isPost()) {
  107. $this->token();
  108. $params = $this->request->post("row/a");
  109. if ($params) {
  110. if (!Validate::is($params['password'], '\S{6,16}')) {
  111. $this->error(__("Please input correct password"));
  112. }
  113. $params['salt'] = Random::alnum();
  114. $params['password'] = md5(md5($params['password']) . $params['salt']);
  115. $params['avatar'] = '/assets/img/avatar.png'; //设置新管理员默认头像。
  116. $result = $this->model->validate('Admin.add')->save($params);
  117. if ($result === false) {
  118. $this->error($this->model->getError());
  119. }
  120. $group = $this->request->post("group/a");
  121. //过滤不允许的组别,避免越权
  122. $group = array_intersect($this->childrenGroupIds, $group);
  123. if (!$group) {
  124. $this->error(__('The parent group exceeds permission limit'));
  125. }
  126. $dataset = [];
  127. foreach ($group as $value) {
  128. $dataset[] = ['uid' => $this->model->id, 'group_id' => $value];
  129. }
  130. model('AuthGroupAccess')->saveAll($dataset);
  131. $this->success();
  132. }
  133. $this->error();
  134. }
  135. return $this->view->fetch();
  136. }
  137. /**
  138. * 编辑
  139. */
  140. public function edit($ids = null)
  141. {
  142. $row = $this->model->get(['id' => $ids]);
  143. if (!$row) {
  144. $this->error(__('No Results were found'));
  145. }
  146. if (!in_array($row->id, $this->childrenAdminIds)) {
  147. $this->error(__('You have no permission'));
  148. }
  149. if ($this->request->isPost()) {
  150. $this->token();
  151. $params = $this->request->post("row/a");
  152. if ($params) {
  153. if ($params['password']) {
  154. if (!Validate::is($params['password'], '\S{6,16}')) {
  155. $this->error(__("Please input correct password"));
  156. }
  157. $params['salt'] = Random::alnum();
  158. $params['password'] = md5(md5($params['password']) . $params['salt']);
  159. } else {
  160. unset($params['password'], $params['salt']);
  161. }
  162. //这里需要针对username和email做唯一验证
  163. $adminValidate = \think\Loader::validate('Admin');
  164. $adminValidate->rule([
  165. 'username' => 'require|regex:\w{3,12}|unique:admin,username,' . $row->id,
  166. 'email' => 'require|email|unique:admin,email,' . $row->id,
  167. 'password' => 'regex:\S{32}',
  168. ]);
  169. $result = $row->validate('Admin.edit')->save($params);
  170. if ($result === false) {
  171. $this->error($row->getError());
  172. }
  173. // 先移除所有权限
  174. model('AuthGroupAccess')->where('uid', $row->id)->delete();
  175. $group = $this->request->post("group/a");
  176. // 过滤不允许的组别,避免越权
  177. $group = array_intersect($this->childrenGroupIds, $group);
  178. if (!$group) {
  179. $this->error(__('The parent group exceeds permission limit'));
  180. }
  181. $dataset = [];
  182. foreach ($group as $value) {
  183. $dataset[] = ['uid' => $row->id, 'group_id' => $value];
  184. }
  185. model('AuthGroupAccess')->saveAll($dataset);
  186. $this->success();
  187. }
  188. $this->error();
  189. }
  190. $grouplist = $this->auth->getGroups($row['id']);
  191. $groupids = [];
  192. foreach ($grouplist as $k => $v) {
  193. $groupids[] = $v['id'];
  194. }
  195. $this->view->assign("row", $row);
  196. $this->view->assign("groupids", $groupids);
  197. return $this->view->fetch();
  198. }
  199. /**
  200. * 删除
  201. */
  202. public function del($ids = "")
  203. {
  204. if (!$this->request->isPost()) {
  205. $this->error(__("Invalid parameters"));
  206. }
  207. $ids = $ids ? $ids : $this->request->post("ids");
  208. if ($ids) {
  209. $ids = array_intersect($this->childrenAdminIds, array_filter(explode(',', $ids)));
  210. // 避免越权删除管理员
  211. $childrenGroupIds = $this->childrenGroupIds;
  212. $adminList = $this->model->where('id', 'in', $ids)->where('id', 'in', function ($query) use ($childrenGroupIds) {
  213. $query->name('auth_group_access')->where('group_id', 'in', $childrenGroupIds)->field('uid');
  214. })->select();
  215. if ($adminList) {
  216. $deleteIds = [];
  217. foreach ($adminList as $k => $v) {
  218. $deleteIds[] = $v->id;
  219. }
  220. $deleteIds = array_values(array_diff($deleteIds, [$this->auth->id]));
  221. if ($deleteIds) {
  222. $this->model->destroy($deleteIds);
  223. model('AuthGroupAccess')->where('uid', 'in', $deleteIds)->delete();
  224. $this->success();
  225. }
  226. }
  227. }
  228. $this->error(__('You have no permission'));
  229. }
  230. /**
  231. * 批量更新
  232. * @internal
  233. */
  234. public function multi($ids = "")
  235. {
  236. // 管理员禁止批量操作
  237. $this->error();
  238. }
  239. /**
  240. * 下拉搜索
  241. */
  242. public function selectpage()
  243. {
  244. $this->dataLimit = 'auth';
  245. $this->dataLimitField = 'id';
  246. return parent::selectpage();
  247. }
  248. }