Admin.php 11 KB

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