Admin.php 14 KB

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