Admin.php 11 KB

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