Admin.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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\Config;
  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 $childrenGroupIds = [];
  23. protected $childrenAdminIds = [];
  24. public function _initialize()
  25. {
  26. parent::_initialize();
  27. $this->model = model('Admin');
  28. $this->childrenAdminIds = $this->auth->getChildrenAdminIds(true);
  29. $this->childrenGroupIds = $this->auth->getChildrenGroupIds(true);
  30. $groupList = collection(AuthGroup::where('id', 'in', $this->childrenGroupIds)->select())->toArray();
  31. Tree::instance()->init($groupList);
  32. $groupdata = [];
  33. if ($this->auth->isSuperAdmin()) {
  34. $result = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0));
  35. foreach ($result as $k => $v) {
  36. $groupdata[$v['id']] = $v['name'];
  37. }
  38. } else {
  39. $result = [];
  40. $groups = $this->auth->getGroups();
  41. foreach ($groups as $m => $n) {
  42. $childlist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray($n['id']));
  43. $temp = [];
  44. foreach ($childlist as $k => $v) {
  45. $temp[$v['id']] = $v['name'];
  46. }
  47. $result[__($n['name'])] = $temp;
  48. }
  49. $groupdata = $result;
  50. }
  51. $this->view->assign('groupdata', $groupdata);
  52. $this->assignconfig("admin", ['id' => $this->auth->id]);
  53. }
  54. /**
  55. * 查看
  56. */
  57. public function index()
  58. {
  59. if ($this->request->isAjax()) {
  60. //如果发送的来源是Selectpage,则转发到Selectpage
  61. if ($this->request->request('keyField')) {
  62. return $this->selectpage();
  63. }
  64. $childrenGroupIds = $this->childrenGroupIds;
  65. $groupName = AuthGroup::where('id', 'in', $childrenGroupIds)
  66. ->column('id,name');
  67. $authGroupList = AuthGroupAccess::where('group_id', 'in', $childrenGroupIds)
  68. ->field('uid,group_id')
  69. ->select();
  70. $adminGroupName = [];
  71. foreach ($authGroupList as $k => $v) {
  72. if (isset($groupName[$v['group_id']])) {
  73. $adminGroupName[$v['uid']][$v['group_id']] = $groupName[$v['group_id']];
  74. }
  75. }
  76. $groups = $this->auth->getGroups();
  77. foreach ($groups as $m => $n) {
  78. $adminGroupName[$this->auth->id][$n['id']] = $n['name'];
  79. }
  80. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  81. $total = $this->model
  82. ->where($where)
  83. ->where('id', 'in', $this->childrenAdminIds)
  84. ->order($sort, $order)
  85. ->count();
  86. $list = $this->model
  87. ->where($where)
  88. ->where('id', 'in', $this->childrenAdminIds)
  89. ->field(['password', 'salt', 'token'], true)
  90. ->order($sort, $order)
  91. ->limit($offset, $limit)
  92. ->select();
  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" => $total, "rows" => $list);
  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. if(!Validate::is($params['password'], '\S{6,16}')){
  114. $this->error(__("Please input correct password"));
  115. }
  116. $params['salt'] = Random::alnum();
  117. $params['password'] = md5($params['password'] . $params['salt'].Config::get('fastadmin.salt'));
  118. $params['avatar'] = '/assets/img/avatar.png'; //设置新管理员默认头像。
  119. $result = $this->model->validate('Admin.add')->save($params);
  120. if ($result === false) {
  121. $this->error($this->model->getError());
  122. }
  123. $group = $this->request->post("group/a");
  124. //过滤不允许的组别,避免越权
  125. $group = array_intersect($this->childrenGroupIds, $group);
  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 ($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($params['password'] . $params['salt'].Config::get('fastadmin.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 ($ids) {
  199. // 避免越权删除管理员
  200. $childrenGroupIds = $this->childrenGroupIds;
  201. $adminList = $this->model->where('id', 'in', $ids)->where('id', 'in', function ($query) use ($childrenGroupIds) {
  202. $query->name('auth_group_access')->where('group_id', 'in', $childrenGroupIds)->field('uid');
  203. })->select();
  204. if ($adminList) {
  205. $deleteIds = [];
  206. foreach ($adminList as $k => $v) {
  207. $deleteIds[] = $v->id;
  208. }
  209. $deleteIds = array_diff($deleteIds, [$this->auth->id]);
  210. if ($deleteIds) {
  211. $this->model->destroy($deleteIds);
  212. model('AuthGroupAccess')->where('uid', 'in', $deleteIds)->delete();
  213. $this->success();
  214. }
  215. }
  216. }
  217. $this->error();
  218. }
  219. /**
  220. * 批量更新
  221. * @internal
  222. */
  223. public function multi($ids = "")
  224. {
  225. // 管理员禁止批量操作
  226. $this->error();
  227. }
  228. /**
  229. * 下拉搜索
  230. */
  231. public function selectpage()
  232. {
  233. $this->dataLimit = 'auth';
  234. $this->dataLimitField = 'id';
  235. return parent::selectpage();
  236. }
  237. }