Admin.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <?php
  2. namespace app\admin\controller\auth;
  3. use app\admin\model\AuthGroup;
  4. use app\admin\model\AuthGroupAccess;
  5. use app\admin\model\Protector;
  6. use app\common\controller\Backend;
  7. use fast\Random;
  8. use fast\Tree;
  9. use think\Session;
  10. /**
  11. * 管理员管理
  12. *
  13. * @icon fa fa-users
  14. * @remark 一个管理员可以有多个角色组,左侧的菜单根据管理员所拥有的权限进行生成
  15. */
  16. class Admin extends Backend
  17. {
  18. protected $model = null;
  19. protected $childrenGroupIds = [];
  20. protected $childrenAdminIds = [];
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. $this->model = model('Admin');
  25. $this->childrenAdminIds = $this->auth->getChildrenAdminIds(true);
  26. $this->childrenGroupIds = $this->auth->getChildrenGroupIds(true);
  27. $groupList = collection(AuthGroup::where('id', 'in', $this->childrenGroupIds)->select())->toArray();
  28. Tree::instance()->init($groupList);
  29. $groupdata = [];
  30. if ($this->auth->isSuperAdmin())
  31. {
  32. $result = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0));
  33. foreach ($result as $k => $v)
  34. {
  35. $groupdata[$v['id']] = $v['name'];
  36. }
  37. }
  38. else
  39. {
  40. $result = [];
  41. $groups = $this->auth->getGroups();
  42. foreach ($groups as $m => $n)
  43. {
  44. $childlist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray($n['id']));
  45. $temp = [];
  46. foreach ($childlist as $k => $v)
  47. {
  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. if ($this->request->isAjax())
  63. {
  64. //如果发送的来源是Selectpage,则转发到Selectpage
  65. if ($this->request->request('keyField'))
  66. {
  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. {
  78. if (isset($groupName[$v['group_id']]))
  79. $adminGroupName[$v['uid']][$v['group_id']] = $groupName[$v['group_id']];
  80. }
  81. $groups = $this->auth->getGroups();
  82. foreach ($groups as $m => $n)
  83. {
  84. $adminGroupName[$this->auth->id][$n['id']] = $n['name'];
  85. }
  86. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  87. $total = $this->model
  88. ->where($where)
  89. ->where('id', 'in', $this->childrenAdminIds)
  90. ->order($sort, $order)
  91. ->count();
  92. $list = $this->model
  93. ->where($where)
  94. ->where('id', 'in', $this->childrenAdminIds)
  95. ->field(['password', 'salt', 'token'], true)
  96. ->order($sort, $order)
  97. ->limit($offset, $limit)
  98. ->select();
  99. foreach ($list as $k => &$v)
  100. {
  101. $groups = isset($adminGroupName[$v['id']]) ? $adminGroupName[$v['id']] : [];
  102. $v['groups'] = implode(',', array_keys($groups));
  103. $v['surplus_count'] = ($v['protect_count'] - Protector::getProtectCount($v['id']));
  104. $v['groups_text'] = implode(',', array_values($groups));
  105. }
  106. unset($v);
  107. $result = array("total" => $total, "rows" => $list);
  108. return json($result);
  109. }
  110. return $this->view->fetch();
  111. }
  112. /**
  113. * 添加
  114. */
  115. public function add()
  116. {
  117. if ($this->request->isPost())
  118. {
  119. $params = $this->request->post("row/a");
  120. if ($params)
  121. {
  122. $params['salt'] = Random::alnum();
  123. $params['password'] = md5(md5($params['password']) . $params['salt']);
  124. $params['avatar'] = '/assets/img/avatar.png'; //设置新管理员默认头像。
  125. // 获取所属组别的保护天数
  126. $protect_count = AuthGroup::getProtectCountById($this->request->post("group/a")[0]);
  127. $this->makeProtect($params,['protect_count'=>$protect_count]);
  128. $result = $this->model->validate('Admin.add')->save($params);
  129. if ($result === false)
  130. {
  131. $this->error($this->model->getError());
  132. }
  133. $group = $this->request->post("group/a");
  134. //过滤不允许的组别,避免越权
  135. $group = array_intersect($this->childrenGroupIds, $group);
  136. $dataset = [];
  137. foreach ($group as $value)
  138. {
  139. $dataset[] = ['uid' => $this->model->id, 'group_id' => $value];
  140. }
  141. model('AuthGroupAccess')->saveAll($dataset);
  142. $this->success();
  143. }
  144. $this->error();
  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. if ($this->request->isPost())
  157. {
  158. $params = $this->request->post("row/a");
  159. if ($params)
  160. {
  161. if ($params['password'])
  162. {
  163. $params['salt'] = Random::alnum();
  164. $params['password'] = md5(md5($params['password']) . $params['salt']);
  165. }
  166. else
  167. {
  168. unset($params['password'], $params['salt']);
  169. }
  170. //这里需要针对username和email做唯一验证
  171. $adminValidate = \think\Loader::validate('Admin');
  172. $adminValidate->rule([
  173. 'username' => 'require|max:50|unique:admin,username,' . $row->id,
  174. 'email' => 'require|email|unique:admin,email,' . $row->id
  175. ]);
  176. $result = $row->validate('Admin.edit')->save($params);
  177. if ($result === false)
  178. {
  179. $this->error($row->getError());
  180. }
  181. // 先移除所有权限
  182. model('AuthGroupAccess')->where('uid', $row->id)->delete();
  183. $group = $this->request->post("group/a");
  184. // 过滤不允许的组别,避免越权
  185. $group = array_intersect($this->childrenGroupIds, $group);
  186. $dataset = [];
  187. foreach ($group as $value)
  188. {
  189. $dataset[] = ['uid' => $row->id, 'group_id' => $value];
  190. }
  191. model('AuthGroupAccess')->saveAll($dataset);
  192. $this->success();
  193. }
  194. $this->error();
  195. }
  196. $grouplist = $this->auth->getGroups($row['id']);
  197. $groupids = [];
  198. foreach ($grouplist as $k => $v)
  199. {
  200. $groupids[] = $v['id'];
  201. }
  202. $this->view->assign("row", $row);
  203. $this->view->assign("groupids", $groupids);
  204. return $this->view->fetch();
  205. }
  206. /**
  207. * 删除
  208. */
  209. public function del($ids = "")
  210. {
  211. if ($ids)
  212. {
  213. // 避免越权删除管理员
  214. $childrenGroupIds = $this->childrenGroupIds;
  215. $adminList = $this->model->where('id', 'in', $ids)->where('id', 'in', function($query) use($childrenGroupIds) {
  216. $query->name('auth_group_access')->where('group_id', 'in', $childrenGroupIds)->field('uid');
  217. })->select();
  218. if ($adminList)
  219. {
  220. $deleteIds = [];
  221. foreach ($adminList as $k => $v)
  222. {
  223. $deleteIds[] = $v->id;
  224. }
  225. $deleteIds = array_diff($deleteIds, [$this->auth->id]);
  226. if ($deleteIds)
  227. {
  228. $this->model->destroy($deleteIds);
  229. model('AuthGroupAccess')->where('uid', 'in', $deleteIds)->delete();
  230. $this->success();
  231. }
  232. }
  233. }
  234. $this->error();
  235. }
  236. /**
  237. * 批量更新
  238. * @internal
  239. */
  240. public function multi($ids = "")
  241. {
  242. // 管理员禁止批量操作
  243. $this->error();
  244. }
  245. /**
  246. * 下拉搜索
  247. */
  248. protected function selectpage()
  249. {
  250. $this->dataLimit = 'auth';
  251. $this->dataLimitField = 'id';
  252. return parent::selectpage();
  253. }
  254. private function makeProtect(&$params,$arr)
  255. {
  256. $params['protect_count'] = isset($arr['protect_count']) ? $arr['protect_count'] : 30; // 保护个数
  257. $params['protect_day'] = isset($arr['protect_day']) ? $arr['protect_day'] : 90; // 保护天数
  258. $params['free_day'] = isset($arr['free_day']) ? $arr['free_day'] : 30; // 冻结保护天数
  259. }
  260. }