Admin.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. //设置过滤方法
  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. $dataset = [];
  124. foreach ($group as $value) {
  125. $dataset[] = ['uid' => $this->model->id, 'group_id' => $value];
  126. }
  127. model('AuthGroupAccess')->saveAll($dataset);
  128. $this->success();
  129. }
  130. $this->error();
  131. }
  132. return $this->view->fetch();
  133. }
  134. /**
  135. * 编辑
  136. */
  137. public function edit($ids = null)
  138. {
  139. $row = $this->model->get(['id' => $ids]);
  140. if (!$row) {
  141. $this->error(__('No Results were found'));
  142. }
  143. if (!in_array($row->id, $this->childrenAdminIds)) {
  144. $this->error(__('You have no permission'));
  145. }
  146. if ($this->request->isPost()) {
  147. $this->token();
  148. $params = $this->request->post("row/a");
  149. if ($params) {
  150. if ($params['password']) {
  151. if (!Validate::is($params['password'], '\S{6,16}')) {
  152. $this->error(__("Please input correct password"));
  153. }
  154. $params['salt'] = Random::alnum();
  155. $params['password'] = md5(md5($params['password']) . $params['salt']);
  156. } else {
  157. unset($params['password'], $params['salt']);
  158. }
  159. //这里需要针对username和email做唯一验证
  160. $adminValidate = \think\Loader::validate('Admin');
  161. $adminValidate->rule([
  162. 'username' => 'require|regex:\w{3,12}|unique:admin,username,' . $row->id,
  163. 'email' => 'require|email|unique:admin,email,' . $row->id,
  164. 'password' => 'regex:\S{32}',
  165. ]);
  166. $result = $row->validate('Admin.edit')->save($params);
  167. if ($result === false) {
  168. $this->error($row->getError());
  169. }
  170. // 先移除所有权限
  171. model('AuthGroupAccess')->where('uid', $row->id)->delete();
  172. $group = $this->request->post("group/a");
  173. // 过滤不允许的组别,避免越权
  174. $group = array_intersect($this->childrenGroupIds, $group);
  175. $dataset = [];
  176. foreach ($group as $value) {
  177. $dataset[] = ['uid' => $row->id, 'group_id' => $value];
  178. }
  179. model('AuthGroupAccess')->saveAll($dataset);
  180. $this->success();
  181. }
  182. $this->error();
  183. }
  184. $grouplist = $this->auth->getGroups($row['id']);
  185. $groupids = [];
  186. foreach ($grouplist as $k => $v) {
  187. $groupids[] = $v['id'];
  188. }
  189. $this->view->assign("row", $row);
  190. $this->view->assign("groupids", $groupids);
  191. return $this->view->fetch();
  192. }
  193. /**
  194. * 删除
  195. */
  196. public function del($ids = "")
  197. {
  198. if (!$this->request->isPost()) {
  199. $this->error(__("Invalid parameters"));
  200. }
  201. $ids = $ids ? $ids : $this->request->post("ids");
  202. if ($ids) {
  203. $ids = array_intersect($this->childrenAdminIds, array_filter(explode(',', $ids)));
  204. // 避免越权删除管理员
  205. $childrenGroupIds = $this->childrenGroupIds;
  206. $adminList = $this->model->where('id', 'in', $ids)->where('id', 'in', function ($query) use ($childrenGroupIds) {
  207. $query->name('auth_group_access')->where('group_id', 'in', $childrenGroupIds)->field('uid');
  208. })->select();
  209. if ($adminList) {
  210. $deleteIds = [];
  211. foreach ($adminList as $k => $v) {
  212. $deleteIds[] = $v->id;
  213. }
  214. $deleteIds = array_values(array_diff($deleteIds, [$this->auth->id]));
  215. if ($deleteIds) {
  216. $this->model->destroy($deleteIds);
  217. model('AuthGroupAccess')->where('uid', 'in', $deleteIds)->delete();
  218. $this->success();
  219. }
  220. }
  221. }
  222. $this->error(__('You have no permission'));
  223. }
  224. /**
  225. * 批量更新
  226. * @internal
  227. */
  228. public function multi($ids = "")
  229. {
  230. // 管理员禁止批量操作
  231. $this->error();
  232. }
  233. /**
  234. * 下拉搜索
  235. */
  236. public function selectpage()
  237. {
  238. $this->dataLimit = 'auth';
  239. $this->dataLimitField = 'id';
  240. return parent::selectpage();
  241. }
  242. }