酒店预订平台
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

316 linhas
11 KiB

  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. protected $noNeedRight = ['getList'];
  19. /**
  20. * @var \app\admin\model\Admin
  21. */
  22. protected $model = null;
  23. protected $selectpageFields = 'id,username,nickname,avatar';
  24. protected $searchFields = 'id,username,nickname';
  25. protected $childrenGroupIds = [];
  26. protected $childrenAdminIds = [];
  27. public function _initialize()
  28. {
  29. parent::_initialize();
  30. $this->model = model('Admin');
  31. $this->childrenAdminIds = $this->auth->getChildrenAdminIds($this->auth->isSuperAdmin());
  32. $this->childrenGroupIds = $this->auth->getChildrenGroupIds($this->auth->isSuperAdmin());
  33. $groupList = collection(AuthGroup::where('id', 'in', $this->childrenGroupIds)->select())->toArray();
  34. Tree::instance()->init($groupList);
  35. $groupdata = [];
  36. if ($this->auth->isSuperAdmin()) {
  37. $result = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0));
  38. foreach ($result as $k => $v) {
  39. $groupdata[$v['id']] = $v['name'];
  40. }
  41. } else {
  42. $result = [];
  43. $groups = $this->auth->getGroups();
  44. foreach ($groups as $m => $n) {
  45. $childlist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray($n['id']));
  46. $temp = [];
  47. foreach ($childlist as $k => $v) {
  48. $temp[$v['id']] = $v['name'];
  49. }
  50. $result[__($n['name'])] = $temp;
  51. }
  52. $groupdata = $result;
  53. }
  54. $this->view->assign('groupdata', $groupdata);
  55. $this->assignconfig("admin", ['id' => $this->auth->id]);
  56. }
  57. /**
  58. * 查看
  59. */
  60. public function index()
  61. {
  62. //设置过滤方法
  63. $this->request->filter(['strip_tags', 'trim']);
  64. if ($this->request->isAjax()) {
  65. //如果发送的来源是Selectpage,则转发到Selectpage
  66. if ($this->request->request('keyField')) {
  67. return $this->selectpage();
  68. }
  69. $childrenGroupIds = $this->childrenGroupIds;
  70. $groupName = AuthGroup::where('id', 'in', $childrenGroupIds)
  71. ->column('id,name');
  72. $authGroupList = AuthGroupAccess::where('group_id', 'in', $childrenGroupIds)
  73. ->field('uid,group_id')
  74. ->select();
  75. $adminGroupName = [];
  76. foreach ($authGroupList as $k => $v) {
  77. if (isset($groupName[$v['group_id']])) {
  78. $adminGroupName[$v['uid']][$v['group_id']] = $groupName[$v['group_id']];
  79. }
  80. }
  81. $groups = $this->auth->getGroups();
  82. foreach ($groups as $m => $n) {
  83. $adminGroupName[$this->auth->id][$n['id']] = $n['name'];
  84. }
  85. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  86. $list = $this->model
  87. ->where($where)
  88. ->where('id', 'in', $this->childrenAdminIds)
  89. ->field(['password', 'salt', 'token'], true)
  90. ->order($sort, $order)
  91. ->paginate($limit);
  92. foreach ($list as $k => &$v) {
  93. $groups = isset($adminGroupName[$v['id']]) ? $adminGroupName[$v['id']] : [];
  94. $v['groups'] = implode(',', array_keys($groups));
  95. $v['groups_text'] = implode(',', array_values($groups));
  96. }
  97. unset($v);
  98. $result = array("total" => $list->total(), "rows" => $list->items());
  99. return json($result);
  100. }
  101. return $this->view->fetch();
  102. }
  103. /**
  104. * 添加
  105. */
  106. public function add()
  107. {
  108. if ($this->request->isPost()) {
  109. $this->token();
  110. $params = $this->request->post("row/a");
  111. if ($params) {
  112. Db::startTrans();
  113. try {
  114. if (!Validate::is($params['password'], '\S{6,16}')) {
  115. exception(__("Please input correct password"));
  116. }
  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. return $this->view->fetch();
  145. }
  146. /**
  147. * 编辑
  148. */
  149. public function edit($ids = null)
  150. {
  151. $row = $this->model->get(['id' => $ids]);
  152. if (!$row) {
  153. $this->error(__('No Results were found'));
  154. }
  155. if (!in_array($row->id, $this->childrenAdminIds)) {
  156. $this->error(__('You have no permission'));
  157. }
  158. if ($this->request->isPost()) {
  159. $this->token();
  160. $params = $this->request->post("row/a");
  161. if ($params) {
  162. Db::startTrans();
  163. try {
  164. if ($params['password']) {
  165. if (!Validate::is($params['password'], '\S{6,16}')) {
  166. exception(__("Please input correct password"));
  167. }
  168. $params['salt'] = Random::alnum();
  169. $params['password'] = md5(md5($params['password']) . $params['salt']);
  170. } else {
  171. unset($params['password'], $params['salt']);
  172. }
  173. //这里需要针对username和email做唯一验证
  174. $adminValidate = \think\Loader::validate('Admin');
  175. $adminValidate->rule([
  176. 'username' => 'require|regex:\w{3,12}|unique:admin,username,' . $row->id,
  177. 'email' => 'require|email|unique:admin,email,' . $row->id,
  178. 'password' => 'regex:\S{32}',
  179. ]);
  180. $result = $row->validate('Admin.edit')->save($params);
  181. if ($result === false) {
  182. exception($row->getError());
  183. }
  184. // 先移除所有权限
  185. model('AuthGroupAccess')->where('uid', $row->id)->delete();
  186. $group = $this->request->post("group/a");
  187. // 过滤不允许的组别,避免越权
  188. $group = array_intersect($this->childrenGroupIds, $group);
  189. if (!$group) {
  190. exception(__('The parent group exceeds permission limit'));
  191. }
  192. $dataset = [];
  193. foreach ($group as $value) {
  194. $dataset[] = ['uid' => $row->id, 'group_id' => $value];
  195. }
  196. model('AuthGroupAccess')->saveAll($dataset);
  197. Db::commit();
  198. } catch (\Exception $e) {
  199. Db::rollback();
  200. $this->error($e->getMessage());
  201. }
  202. $this->success();
  203. }
  204. $this->error(__('Parameter %s can not be empty', ''));
  205. }
  206. $grouplist = $this->auth->getGroups($row['id']);
  207. $groupids = [];
  208. foreach ($grouplist as $k => $v) {
  209. $groupids[] = $v['id'];
  210. }
  211. $this->view->assign("row", $row);
  212. $this->view->assign("groupids", $groupids);
  213. return $this->view->fetch();
  214. }
  215. /**
  216. * 删除
  217. */
  218. public function del($ids = "")
  219. {
  220. if (!$this->request->isPost()) {
  221. $this->error(__("Invalid parameters"));
  222. }
  223. $ids = $ids ? $ids : $this->request->post("ids");
  224. if ($ids) {
  225. $ids = array_intersect($this->childrenAdminIds, array_filter(explode(',', $ids)));
  226. // 避免越权删除管理员
  227. $childrenGroupIds = $this->childrenGroupIds;
  228. $adminList = $this->model->where('id', 'in', $ids)->where('id', 'in', function ($query) use ($childrenGroupIds) {
  229. $query->name('auth_group_access')->where('group_id', 'in', $childrenGroupIds)->field('uid');
  230. })->select();
  231. if ($adminList) {
  232. $deleteIds = [];
  233. foreach ($adminList as $k => $v) {
  234. $deleteIds[] = $v->id;
  235. }
  236. $deleteIds = array_values(array_diff($deleteIds, [$this->auth->id]));
  237. if ($deleteIds) {
  238. Db::startTrans();
  239. try {
  240. $this->model->destroy($deleteIds);
  241. model('AuthGroupAccess')->where('uid', 'in', $deleteIds)->delete();
  242. Db::commit();
  243. } catch (\Exception $e) {
  244. Db::rollback();
  245. $this->error($e->getMessage());
  246. }
  247. $this->success();
  248. }
  249. $this->error(__('No rows were deleted'));
  250. }
  251. }
  252. $this->error(__('You have no permission'));
  253. }
  254. /**
  255. * 批量更新
  256. * @internal
  257. */
  258. public function multi($ids = "")
  259. {
  260. // 管理员禁止批量操作
  261. $this->error();
  262. }
  263. /**
  264. * 下拉搜索
  265. */
  266. public function selectpage()
  267. {
  268. $this->dataLimit = 'auth';
  269. $this->dataLimitField = 'id';
  270. return parent::selectpage();
  271. }
  272. public function getList(){
  273. $name=$this->request->post('name');
  274. $keyValue=$this->request->post('keyValue');
  275. $this->model->field('id,nickname as name');
  276. if($keyValue){
  277. $this->model->where(['id'=>$keyValue]);
  278. }elseif($name){
  279. $this->model->where(['nickname'=>['like','%'.$name.'%']]);
  280. }
  281. $result= $this->model->select();
  282. // print_r($result);
  283. if($keyValue){
  284. return json(['list' => $result]);
  285. }
  286. return json(['list' => $result]);
  287. }
  288. }