Admin.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. $params['company'] = Db::name('company')->where('id',$params['company_id'])->value('company');
  175. //这里需要针对username和email做唯一验证
  176. $adminValidate = \think\Loader::validate('Admin');
  177. $adminValidate->rule([
  178. 'username' => 'require|regex:\w{3,30}|unique:admin,username,' . $row->id,
  179. 'email' => 'require|email|unique:admin,email,' . $row->id,
  180. 'mobile' => 'regex:1[3-9]\d{9}|unique:admin,mobile,' . $row->id,
  181. 'password' => 'regex:\S{32}',
  182. ]);
  183. $result = $row->validate('Admin.edit')->save($params);
  184. if ($result === false) {
  185. exception($row->getError());
  186. }
  187. // 先移除所有权限
  188. model('AuthGroupAccess')->where('uid', $row->id)->delete();
  189. $group = $this->request->post("group/a");
  190. // 过滤不允许的组别,避免越权
  191. $group = array_intersect($this->childrenGroupIds, $group);
  192. if (!$group) {
  193. exception(__('The parent group exceeds permission limit'));
  194. }
  195. $dataset = [];
  196. foreach ($group as $value) {
  197. $dataset[] = ['uid' => $row->id, 'group_id' => $value];
  198. }
  199. model('AuthGroupAccess')->saveAll($dataset);
  200. Db::commit();
  201. } catch (\Exception $e) {
  202. Db::rollback();
  203. $this->error($e->getMessage());
  204. }
  205. $this->success();
  206. }
  207. $this->error(__('Parameter %s can not be empty', ''));
  208. }
  209. $grouplist = $this->auth->getGroups($row['id']);
  210. $groupids = [];
  211. foreach ($grouplist as $k => $v) {
  212. $groupids[] = $v['id'];
  213. }
  214. $company = Db::name('company')->select();
  215. $this->view->assign("row", $row);
  216. $this->view->assign("company", $company);
  217. $this->view->assign("groupids", $groupids);
  218. return $this->view->fetch();
  219. }
  220. /**
  221. * 删除
  222. */
  223. public function del($ids = "")
  224. {
  225. if (!$this->request->isPost()) {
  226. $this->error(__("Invalid parameters"));
  227. }
  228. $ids = $ids ? $ids : $this->request->post("ids");
  229. if ($ids) {
  230. $ids = array_intersect($this->childrenAdminIds, array_filter(explode(',', $ids)));
  231. // 避免越权删除管理员
  232. $childrenGroupIds = $this->childrenGroupIds;
  233. $adminList = $this->model->where('id', 'in', $ids)->where('id', 'in', function ($query) use ($childrenGroupIds) {
  234. $query->name('auth_group_access')->where('group_id', 'in', $childrenGroupIds)->field('uid');
  235. })->select();
  236. if ($adminList) {
  237. $deleteIds = [];
  238. foreach ($adminList as $k => $v) {
  239. $deleteIds[] = $v->id;
  240. }
  241. $deleteIds = array_values(array_diff($deleteIds, [$this->auth->id]));
  242. if ($deleteIds) {
  243. Db::startTrans();
  244. try {
  245. $this->model->destroy($deleteIds);
  246. model('AuthGroupAccess')->where('uid', 'in', $deleteIds)->delete();
  247. Db::commit();
  248. } catch (\Exception $e) {
  249. Db::rollback();
  250. $this->error($e->getMessage());
  251. }
  252. $this->success();
  253. }
  254. $this->error(__('No rows were deleted'));
  255. }
  256. }
  257. $this->error(__('You have no permission'));
  258. }
  259. /**
  260. * 批量更新
  261. * @internal
  262. */
  263. public function multi($ids = "")
  264. {
  265. // 管理员禁止批量操作
  266. $this->error();
  267. }
  268. /**
  269. * 下拉搜索
  270. */
  271. public function selectpage()
  272. {
  273. $this->dataLimit = 'auth';
  274. $this->dataLimitField = 'id';
  275. return parent::selectpage();
  276. }
  277. }