Admin.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. /**
  9. * 管理员管理
  10. *
  11. * @icon fa fa-users
  12. * @remark 一个管理员可以有多个角色组,左侧的菜单根据管理员所拥有的权限进行生成
  13. */
  14. class Admin extends Backend
  15. {
  16. protected $model = null;
  17. protected $childrenGroupIds = [];
  18. protected $childrenAdminIds = [];
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. $this->model = model('Admin');
  23. $this->childrenAdminIds = $this->auth->getChildrenAdminIds(true);
  24. $this->childrenGroupIds = $this->auth->getChildrenGroupIds($this->auth->isSuperAdmin() ? true : false);
  25. $groupList = collection(AuthGroup::where('id', 'in', $this->childrenGroupIds)->select())->toArray();
  26. $groupIds = $this->auth->getGroupIds();
  27. Tree::instance()->init($groupList);
  28. $result = [];
  29. if ($this->auth->isSuperAdmin())
  30. {
  31. $result = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0));
  32. }
  33. else
  34. {
  35. foreach ($groupIds as $m => $n)
  36. {
  37. $result = array_merge($result, Tree::instance()->getTreeList(Tree::instance()->getTreeArray($n)));
  38. }
  39. }
  40. $groupName = [];
  41. foreach ($result as $k => $v)
  42. {
  43. $groupName[$v['id']] = $v['name'];
  44. }
  45. $this->view->assign('groupdata', $groupName);
  46. $this->assignconfig("admin", ['id' => $this->auth->id]);
  47. }
  48. /**
  49. * 查看
  50. */
  51. public function index()
  52. {
  53. if ($this->request->isAjax())
  54. {
  55. $childrenGroupIds = $this->childrenGroupIds;
  56. $groupName = AuthGroup::where('id', 'in', $childrenGroupIds)
  57. ->column('id,name');
  58. $authGroupList = AuthGroupAccess::where('group_id', 'in', $childrenGroupIds)
  59. ->field('uid,group_id')
  60. ->select();
  61. $adminGroupName = [];
  62. foreach ($authGroupList as $k => $v)
  63. {
  64. if (isset($groupName[$v['group_id']]))
  65. $adminGroupName[$v['uid']][$v['group_id']] = $groupName[$v['group_id']];
  66. }
  67. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  68. $total = $this->model
  69. ->where($where)
  70. ->where('id', 'in', $this->childrenAdminIds)
  71. ->order($sort, $order)
  72. ->count();
  73. $list = $this->model
  74. ->where($where)
  75. ->where('id', 'in', $this->childrenAdminIds)
  76. ->field(['password', 'salt', 'token'], true)
  77. ->order($sort, $order)
  78. ->limit($offset, $limit)
  79. ->select();
  80. foreach ($list as $k => &$v)
  81. {
  82. $groups = isset($adminGroupName[$v['id']]) ? $adminGroupName[$v['id']] : [];
  83. $v['groups'] = implode(',', array_keys($groups));
  84. $v['groups_text'] = implode(',', array_values($groups));
  85. }
  86. unset($v);
  87. $result = array("total" => $total, "rows" => $list);
  88. return json($result);
  89. }
  90. return $this->view->fetch();
  91. }
  92. /**
  93. * 添加
  94. */
  95. public function add()
  96. {
  97. if ($this->request->isPost())
  98. {
  99. $params = $this->request->post("row/a");
  100. if ($params)
  101. {
  102. $params['salt'] = Random::alnum();
  103. $params['password'] = md5(md5($params['password']) . $params['salt']);
  104. $params['avatar'] = '/assets/img/avatar.png'; //设置新管理员默认头像。
  105. $result = $this->model->validate('Admin.add')->save($params);
  106. if ($result === false)
  107. {
  108. $this->error($this->model->getError());
  109. }
  110. $group = $this->request->post("group/a");
  111. //过滤不允许的组别,避免越权
  112. $group = array_intersect($this->childrenGroupIds, $group);
  113. $dataset = [];
  114. foreach ($group as $value)
  115. {
  116. $dataset[] = ['uid' => $this->model->id, 'group_id' => $value];
  117. }
  118. model('AuthGroupAccess')->saveAll($dataset);
  119. $this->success();
  120. }
  121. $this->error();
  122. }
  123. return $this->view->fetch();
  124. }
  125. /**
  126. * 编辑
  127. */
  128. public function edit($ids = NULL)
  129. {
  130. $row = $this->model->get(['id' => $ids]);
  131. if (!$row)
  132. $this->error(__('No Results were found'));
  133. if ($this->request->isPost())
  134. {
  135. $params = $this->request->post("row/a");
  136. if ($params)
  137. {
  138. if ($params['password'])
  139. {
  140. $params['salt'] = Random::alnum();
  141. $params['password'] = md5(md5($params['password']) . $params['salt']);
  142. }
  143. else
  144. {
  145. unset($params['password'], $params['salt']);
  146. }
  147. //这里需要针对username和email做唯一验证
  148. $adminValidate = \think\Loader::validate('Admin');
  149. $adminValidate->rule([
  150. 'username' => 'require|max:50|unique:admin,username,' . $row->id,
  151. 'email' => 'require|email|unique:admin,email,' . $row->id
  152. ]);
  153. $result = $row->validate('Admin.edit')->save($params);
  154. if ($result === false)
  155. {
  156. $this->error($row->getError());
  157. }
  158. // 先移除所有权限
  159. model('AuthGroupAccess')->where('uid', $row->id)->delete();
  160. $group = $this->request->post("group/a");
  161. // 过滤不允许的组别,避免越权
  162. $group = array_intersect($this->childrenGroupIds, $group);
  163. $dataset = [];
  164. foreach ($group as $value)
  165. {
  166. $dataset[] = ['uid' => $row->id, 'group_id' => $value];
  167. }
  168. model('AuthGroupAccess')->saveAll($dataset);
  169. $this->success();
  170. }
  171. $this->error();
  172. }
  173. $grouplist = $this->auth->getGroups($row['id']);
  174. $groupids = [];
  175. foreach ($grouplist as $k => $v)
  176. {
  177. $groupids[] = $v['id'];
  178. }
  179. $this->view->assign("row", $row);
  180. $this->view->assign("groupids", $groupids);
  181. return $this->view->fetch();
  182. }
  183. /**
  184. * 删除
  185. */
  186. public function del($ids = "")
  187. {
  188. if ($ids)
  189. {
  190. // 避免越权删除管理员
  191. $childrenGroupIds = $this->childrenGroupIds;
  192. $adminList = $this->model->where('id', 'in', $ids)->where('id', 'in', function($query) use($childrenGroupIds) {
  193. $query->name('auth_group_access')->where('group_id', 'in', $childrenGroupIds)->field('uid');
  194. })->select();
  195. if ($adminList)
  196. {
  197. $deleteIds = [];
  198. foreach ($adminList as $k => $v)
  199. {
  200. $deleteIds[] = $v->id;
  201. }
  202. $deleteIds = array_diff($deleteIds, [$this->auth->id]);
  203. if ($deleteIds)
  204. {
  205. $this->model->destroy($deleteIds);
  206. model('AuthGroupAccess')->where('uid', 'in', $deleteIds)->delete();
  207. $this->success();
  208. }
  209. }
  210. }
  211. $this->error();
  212. }
  213. /**
  214. * 批量更新
  215. * @internal
  216. */
  217. public function multi($ids = "")
  218. {
  219. // 管理员禁止批量操作
  220. $this->error();
  221. }
  222. }