Admin.php 11 KB

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