Admin.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. //公司
  125. $result = $this->model->validate('Admin.add')->save($params);
  126. if ($result === false) {
  127. exception($this->model->getError());
  128. }
  129. $group = $this->request->post("group/a");
  130. //过滤不允许的组别,避免越权
  131. $group = array_intersect($this->childrenGroupIds, $group);
  132. if (!$group) {
  133. exception(__('The parent group exceeds permission limit'));
  134. }
  135. $dataset = [];
  136. foreach ($group as $value) {
  137. $dataset[] = ['uid' => $this->model->id, 'group_id' => $value];
  138. }
  139. model('AuthGroupAccess')->saveAll($dataset);
  140. Db::commit();
  141. } catch (\Exception $e) {
  142. Db::rollback();
  143. $this->error($e->getMessage());
  144. }
  145. $this->success();
  146. }
  147. $this->error(__('Parameter %s can not be empty', ''));
  148. }
  149. return $this->view->fetch();
  150. }
  151. /**
  152. * 编辑
  153. */
  154. public function edit($ids = null)
  155. {
  156. $row = $this->model->get(['id' => $ids]);
  157. if (!$row) {
  158. $this->error(__('No Results were found'));
  159. }
  160. if (!in_array($row->id, $this->childrenAdminIds)) {
  161. $this->error(__('You have no permission'));
  162. }
  163. if ($this->request->isPost()) {
  164. $this->token();
  165. $params = $this->request->post("row/a");
  166. if ($params) {
  167. Db::startTrans();
  168. try {
  169. if ($params['password']) {
  170. if (!Validate::is($params['password'], '\S{6,30}')) {
  171. exception(__("Please input correct password"));
  172. }
  173. $params['salt'] = Random::alnum();
  174. $params['password'] = md5(md5($params['password']) . $params['salt']);
  175. } else {
  176. unset($params['password'], $params['salt']);
  177. }
  178. //公司
  179. $params['company_id'] = $this->request->post('company_id');
  180. //公司
  181. //这里需要针对username和email做唯一验证
  182. $adminValidate = \think\Loader::validate('Admin');
  183. $adminValidate->rule([
  184. 'username' => 'require|regex:\w{3,30}|unique:admin,username,' . $row->id,
  185. 'email' => 'require|email|unique:admin,email,' . $row->id,
  186. 'mobile' => 'regex:1[3-9]\d{9}|unique:admin,mobile,' . $row->id,
  187. 'password' => 'regex:\S{32}',
  188. ]);
  189. $result = $row->validate('Admin.edit')->save($params);
  190. if ($result === false) {
  191. exception($row->getError());
  192. }
  193. // 先移除所有权限
  194. model('AuthGroupAccess')->where('uid', $row->id)->delete();
  195. $group = $this->request->post("group/a");
  196. // 过滤不允许的组别,避免越权
  197. $group = array_intersect($this->childrenGroupIds, $group);
  198. if (!$group) {
  199. exception(__('The parent group exceeds permission limit'));
  200. }
  201. $dataset = [];
  202. foreach ($group as $value) {
  203. $dataset[] = ['uid' => $row->id, 'group_id' => $value];
  204. }
  205. model('AuthGroupAccess')->saveAll($dataset);
  206. Db::commit();
  207. } catch (\Exception $e) {
  208. Db::rollback();
  209. $this->error($e->getMessage());
  210. }
  211. $this->success();
  212. }
  213. $this->error(__('Parameter %s can not be empty', ''));
  214. }
  215. $grouplist = $this->auth->getGroups($row['id']);
  216. $groupids = [];
  217. foreach ($grouplist as $k => $v) {
  218. $groupids[] = $v['id'];
  219. }
  220. $this->view->assign("row", $row);
  221. $this->view->assign("groupids", $groupids);
  222. return $this->view->fetch();
  223. }
  224. /**
  225. * 删除
  226. */
  227. public function del($ids = "")
  228. {
  229. if (!$this->request->isPost()) {
  230. $this->error(__("Invalid parameters"));
  231. }
  232. $ids = $ids ? $ids : $this->request->post("ids");
  233. if ($ids) {
  234. $ids = array_intersect($this->childrenAdminIds, array_filter(explode(',', $ids)));
  235. // 避免越权删除管理员
  236. $childrenGroupIds = $this->childrenGroupIds;
  237. $adminList = $this->model->where('id', 'in', $ids)->where('id', 'in', function ($query) use ($childrenGroupIds) {
  238. $query->name('auth_group_access')->where('group_id', 'in', $childrenGroupIds)->field('uid');
  239. })->select();
  240. if ($adminList) {
  241. $deleteIds = [];
  242. foreach ($adminList as $k => $v) {
  243. $deleteIds[] = $v->id;
  244. }
  245. $deleteIds = array_values(array_diff($deleteIds, [$this->auth->id]));
  246. if ($deleteIds) {
  247. Db::startTrans();
  248. try {
  249. $this->model->destroy($deleteIds);
  250. model('AuthGroupAccess')->where('uid', 'in', $deleteIds)->delete();
  251. Db::commit();
  252. } catch (\Exception $e) {
  253. Db::rollback();
  254. $this->error($e->getMessage());
  255. }
  256. $this->success();
  257. }
  258. $this->error(__('No rows were deleted'));
  259. }
  260. }
  261. $this->error(__('You have no permission'));
  262. }
  263. /**
  264. * 批量更新
  265. * @internal
  266. */
  267. public function multi($ids = "")
  268. {
  269. // 管理员禁止批量操作
  270. $this->error();
  271. }
  272. /**
  273. * 下拉搜索
  274. */
  275. public function selectpage()
  276. {
  277. $this->dataLimit = 'auth';
  278. $this->dataLimitField = 'id';
  279. return parent::selectpage();
  280. }
  281. }