酒店预订平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

331 line
12 KiB

  1. <?php
  2. namespace app\admin\controller\auth;
  3. use app\admin\dao\GroupDao;
  4. use app\admin\model\AuthGroup;
  5. use app\admin\model\AuthGroupAccess;
  6. use app\common\controller\Backend;
  7. use fast\Random;
  8. use fast\Tree;
  9. use think\Db;
  10. use think\Validate;
  11. /**
  12. * 管理员管理
  13. *
  14. * @icon fa fa-users
  15. * @remark 一个管理员可以有多个角色组,左侧的菜单根据管理员所拥有的权限进行生成
  16. */
  17. class Admin extends Backend
  18. {
  19. protected $noNeedRight = ['getList'];
  20. /**
  21. * @var \app\admin\model\Admin
  22. */
  23. protected $model = null;
  24. protected $selectpageFields = 'id,username,nickname,avatar';
  25. protected $searchFields = 'id,username,nickname';
  26. protected $childrenGroupIds = [];
  27. protected $childrenAdminIds = [];
  28. public function _initialize()
  29. {
  30. parent::_initialize();
  31. $this->model = model('Admin');
  32. $this->childrenAdminIds = $this->auth->getChildrenAdminIds($this->auth->isSuperAdmin());
  33. $this->childrenGroupIds = $this->auth->getChildrenGroupIds($this->auth->isSuperAdmin());
  34. $groupList = collection(AuthGroup::where('id', 'in', $this->childrenGroupIds)->select())->toArray();
  35. Tree::instance()->init($groupList);
  36. $groupdata = [];
  37. if ($this->auth->isSuperAdmin()) {
  38. $result = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0));
  39. foreach ($result as $k => $v) {
  40. $groupdata[$v['id']] = $v['name'];
  41. }
  42. } else {
  43. $result = [];
  44. $groups = $this->auth->getGroups();
  45. foreach ($groups as $m => $n) {
  46. $childlist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray($n['id']));
  47. $temp = [];
  48. foreach ($childlist as $k => $v) {
  49. $temp[$v['id']] = $v['name'];
  50. }
  51. $result[__($n['name'])] = $temp;
  52. }
  53. $groupdata = $result;
  54. }
  55. $this->view->assign('groupdata', $groupdata);
  56. $this->assignconfig("admin", ['id' => $this->auth->id]);
  57. }
  58. /**
  59. * 查看
  60. */
  61. public function index()
  62. {
  63. //设置过滤方法
  64. $this->request->filter(['strip_tags', 'trim']);
  65. if ($this->request->isAjax()) {
  66. //如果发送的来源是Selectpage,则转发到Selectpage
  67. if ($this->request->request('keyField')) {
  68. return $this->selectpage();
  69. }
  70. $childrenGroupIds = $this->childrenGroupIds;
  71. $groupName = AuthGroup::where('id', 'in', $childrenGroupIds)
  72. ->column('id,name');
  73. $authGroupList = AuthGroupAccess::where('group_id', 'in', $childrenGroupIds)
  74. ->field('uid,group_id')
  75. ->select();
  76. $adminGroupName = [];
  77. foreach ($authGroupList as $k => $v) {
  78. if (isset($groupName[$v['group_id']])) {
  79. $adminGroupName[$v['uid']][$v['group_id']] = $groupName[$v['group_id']];
  80. }
  81. }
  82. $groups = $this->auth->getGroups();
  83. foreach ($groups as $m => $n) {
  84. $adminGroupName[$this->auth->id][$n['id']] = $n['name'];
  85. }
  86. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  87. $list = $this->model
  88. ->where($where)
  89. ->where('id', 'in', $this->childrenAdminIds)
  90. ->field(['password', 'salt', 'token'], true)
  91. ->order($sort, $order)
  92. ->paginate($limit);
  93. foreach ($list as $k => &$v) {
  94. $groups = isset($adminGroupName[$v['id']]) ? $adminGroupName[$v['id']] : [];
  95. $v['groups'] = implode(',', array_keys($groups));
  96. $v['groups_text'] = implode(',', array_values($groups));
  97. }
  98. unset($v);
  99. $result = array("total" => $list->total(), "rows" => $list->items());
  100. return json($result);
  101. }
  102. return $this->view->fetch();
  103. }
  104. /**
  105. * 添加
  106. */
  107. public function add()
  108. {
  109. if ($this->request->isPost()) {
  110. $this->token();
  111. $params = $this->request->post("row/a");
  112. if ($params) {
  113. Db::startTrans();
  114. try {
  115. if (!Validate::is($params['password'], '\S{6,16}')) {
  116. exception(__("Please input correct password"));
  117. }
  118. $params['salt'] = Random::alnum();
  119. $params['password'] = md5(md5($params['password']) . $params['salt']);
  120. $params['avatar'] = '/assets/img/avatar.png'; //设置新管理员默认头像。
  121. $result = $this->model->validate('Admin.add')->save($params);
  122. if ($result === false) {
  123. exception($this->model->getError());
  124. }
  125. $group = $this->request->post("group/a");
  126. //过滤不允许的组别,避免越权
  127. $group = array_intersect($this->childrenGroupIds, $group);
  128. if (!$group) {
  129. exception(__('The parent group exceeds permission limit'));
  130. }
  131. $dataset = [];
  132. foreach ($group as $value) {
  133. $dataset[] = ['uid' => $this->model->id, 'group_id' => $value];
  134. }
  135. model('AuthGroupAccess')->saveAll($dataset);
  136. Db::commit();
  137. } catch (\Exception $e) {
  138. Db::rollback();
  139. $this->error($e->getMessage());
  140. }
  141. $this->success();
  142. }
  143. $this->error(__('Parameter %s can not be empty', ''));
  144. }
  145. return $this->view->fetch();
  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,16}')) {
  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,12}|unique:admin,username,' . $row->id,
  178. 'email' => 'require|email|unique:admin,email,' . $row->id,
  179. 'password' => 'regex:\S{32}',
  180. ]);
  181. $result = $row->validate('Admin.edit')->save($params);
  182. if ($result === false) {
  183. exception($row->getError());
  184. }
  185. // 先移除所有权限
  186. model('AuthGroupAccess')->where('uid', $row->id)->delete();
  187. $group = $this->request->post("group/a");
  188. // 过滤不允许的组别,避免越权
  189. $group = array_intersect($this->childrenGroupIds, $group);
  190. if (!$group) {
  191. exception(__('The parent group exceeds permission limit'));
  192. }
  193. $dataset = [];
  194. foreach ($group as $value) {
  195. $dataset[] = ['uid' => $row->id, 'group_id' => $value];
  196. }
  197. model('AuthGroupAccess')->saveAll($dataset);
  198. Db::commit();
  199. } catch (\Exception $e) {
  200. Db::rollback();
  201. $this->error($e->getMessage());
  202. }
  203. $this->success();
  204. }
  205. $this->error(__('Parameter %s can not be empty', ''));
  206. }
  207. $grouplist = $this->auth->getGroups($row['id']);
  208. $groupids = [];
  209. foreach ($grouplist as $k => $v) {
  210. $groupids[] = $v['id'];
  211. }
  212. $this->view->assign("row", $row);
  213. $this->view->assign("groupids", $groupids);
  214. return $this->view->fetch();
  215. }
  216. /**
  217. * 删除
  218. */
  219. public function del($ids = "")
  220. {
  221. if (!$this->request->isPost()) {
  222. $this->error(__("Invalid parameters"));
  223. }
  224. $ids = $ids ? $ids : $this->request->post("ids");
  225. if ($ids) {
  226. $ids = array_intersect($this->childrenAdminIds, array_filter(explode(',', $ids)));
  227. // 避免越权删除管理员
  228. $childrenGroupIds = $this->childrenGroupIds;
  229. $adminList = $this->model->where('id', 'in', $ids)->where('id', 'in', function ($query) use ($childrenGroupIds) {
  230. $query->name('auth_group_access')->where('group_id', 'in', $childrenGroupIds)->field('uid');
  231. })->select();
  232. if ($adminList) {
  233. $deleteIds = [];
  234. foreach ($adminList as $k => $v) {
  235. $deleteIds[] = $v->id;
  236. }
  237. $deleteIds = array_values(array_diff($deleteIds, [$this->auth->id]));
  238. if ($deleteIds) {
  239. Db::startTrans();
  240. try {
  241. $this->model->destroy($deleteIds);
  242. model('AuthGroupAccess')->where('uid', 'in', $deleteIds)->delete();
  243. Db::commit();
  244. } catch (\Exception $e) {
  245. Db::rollback();
  246. $this->error($e->getMessage());
  247. }
  248. $this->success();
  249. }
  250. $this->error(__('No rows were deleted'));
  251. }
  252. }
  253. $this->error(__('You have no permission'));
  254. }
  255. /**
  256. * 批量更新
  257. * @internal
  258. */
  259. public function multi($ids = "")
  260. {
  261. // 管理员禁止批量操作
  262. $this->error();
  263. }
  264. /**
  265. * 下拉搜索
  266. */
  267. public function selectpage()
  268. {
  269. $this->dataLimit = 'auth';
  270. $this->dataLimitField = 'id';
  271. return parent::selectpage();
  272. }
  273. public function getList(){
  274. $name=$this->request->post('name');
  275. $keyValue=$this->request->post('keyValue');
  276. $this->model->field('id,nickname as name');
  277. $groupDao = new GroupDao();
  278. $groupIds = $groupDao->getSubGroup($groupDao->getTopGroup($this->auth->getGroupIds()[0]));
  279. //获取用户ID
  280. $accModel = new AuthGroupAccess();
  281. $accList = $accModel->where(["group_id" => ["in",$groupIds]])->select();
  282. if ($accList == null) {
  283. return json(['list' => []]);
  284. }
  285. $accList = $accList->toArray();
  286. $ids = [];
  287. foreach ($accList as $acc) {
  288. $ids[] = $acc['uid'];
  289. }
  290. $where = ["id"=>["in",$ids]];
  291. if($keyValue){
  292. $where = ['id'=>$keyValue];
  293. }elseif($name){
  294. $where = ['nickname'=>['like','%'.$name.'%'],"id"=>["in",$ids]];
  295. }
  296. $result= $this->model->where($where)->select();
  297. // print_r($result);
  298. if($keyValue){
  299. return json(['list' => $result]);
  300. }
  301. return json(['list' => $result]);
  302. }
  303. }