Admin.php 11 KB

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