Admin.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. namespace app\admin\controller\auth;
  3. use app\common\controller\Backend;
  4. use fast\Random;
  5. use fast\Tree;
  6. /**
  7. * 管理员管理
  8. *
  9. * @icon fa fa-users
  10. * @remark 一个管理员可以有多个角色组,左侧的菜单根据管理员所拥有的权限进行生成
  11. */
  12. class Admin extends Backend
  13. {
  14. protected $model = null;
  15. //当前登录管理员所有子节点组别
  16. protected $childrenIds = [];
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. $this->model = model('Admin');
  21. $groups = $this->auth->getGroups();
  22. // 取出所有分组
  23. $grouplist = model('AuthGroup')->all(['status' => 'normal']);
  24. $objlist = [];
  25. foreach ($groups as $K => $v)
  26. {
  27. // 取出包含自己的所有子节点
  28. $childrenlist = Tree::instance()->init($grouplist)->getChildren($v['id'], TRUE);
  29. $obj = Tree::instance()->init($childrenlist)->getTreeArray($v['pid']);
  30. $objlist = array_merge($objlist, Tree::instance()->getTreeList($obj));
  31. }
  32. $groupdata = [];
  33. foreach ($objlist as $k => $v)
  34. {
  35. $groupdata[$v['id']] = $v['name'];
  36. }
  37. $this->childrenIds = array_keys($groupdata);
  38. $this->view->assign('groupdata', $groupdata);
  39. }
  40. /**
  41. * 查看
  42. */
  43. public function index()
  44. {
  45. if ($this->request->isAjax())
  46. {
  47. $childrenAdminIds = model('AuthGroupAccess')
  48. ->field('uid')
  49. ->where('group_id', 'in', $this->childrenIds)
  50. ->column('uid');
  51. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  52. $total = $this->model
  53. ->where($where)
  54. ->where('id', 'in', $childrenAdminIds)
  55. ->order($sort, $order)
  56. ->count();
  57. $list = $this->model
  58. ->where($where)
  59. ->where('id', 'in', $childrenAdminIds)
  60. ->field(['password', 'salt', 'token'], true)
  61. ->order($sort, $order)
  62. ->limit($offset, $limit)
  63. ->select();
  64. $result = array("total" => $total, "rows" => $list);
  65. return json($result);
  66. }
  67. return $this->view->fetch();
  68. }
  69. /**
  70. * 添加
  71. */
  72. public function add()
  73. {
  74. if ($this->request->isPost())
  75. {
  76. $this->code = -1;
  77. $params = $this->request->post("row/a");
  78. if ($params)
  79. {
  80. $params['salt'] = Random::alnum();
  81. $params['password'] = md5(md5($params['password']) . $params['salt']);
  82. $admin = $this->model->create($params);
  83. $group = $this->request->post("group/a");
  84. //过滤不允许的组别,避免越权
  85. $group = array_intersect($this->childrenIds, $group);
  86. $dataset = [];
  87. foreach ($group as $value)
  88. {
  89. $dataset[] = ['uid' => $admin->id, 'group_id' => $value];
  90. }
  91. model('AuthGroupAccess')->saveAll($dataset);
  92. $this->code = 1;
  93. }
  94. return;
  95. }
  96. return $this->view->fetch();
  97. }
  98. /**
  99. * 编辑
  100. */
  101. public function edit($ids = NULL)
  102. {
  103. $row = $this->model->get(['id' => $ids]);
  104. if (!$row)
  105. $this->error(__('No Results were found'));
  106. if ($this->request->isPost())
  107. {
  108. $this->code = -1;
  109. $params = $this->request->post("row/a");
  110. if ($params)
  111. {
  112. if ($params['password'])
  113. {
  114. $params['salt'] = Random::alnum();
  115. $params['password'] = md5(md5($params['password']) . $params['salt']);
  116. }
  117. else
  118. {
  119. unset($params['password'], $params['salt']);
  120. }
  121. $row->save($params);
  122. // 先移除所有权限
  123. model('AuthGroupAccess')->where('uid', $row->id)->delete();
  124. $group = $this->request->post("group/a");
  125. // 过滤不允许的组别,避免越权
  126. $group = array_intersect($this->childrenIds, $group);
  127. $dataset = [];
  128. foreach ($group as $value)
  129. {
  130. $dataset[] = ['uid' => $row->id, 'group_id' => $value];
  131. }
  132. model('AuthGroupAccess')->saveAll($dataset);
  133. $this->code = 1;
  134. }
  135. return;
  136. }
  137. $grouplist = $this->auth->getGroups($row['id']);
  138. $groupids = [];
  139. foreach ($grouplist as $k => $v)
  140. {
  141. $groupids[] = $v['id'];
  142. }
  143. $this->view->assign("row", $row);
  144. $this->view->assign("groupids", $groupids);
  145. return $this->view->fetch();
  146. }
  147. /**
  148. * 删除
  149. */
  150. public function del($ids = "")
  151. {
  152. $this->code = -1;
  153. if ($ids)
  154. {
  155. // 避免越权删除管理员
  156. $childrenGroupIds = $this->childrenIds;
  157. $adminList = $this->model->where('id', 'in', $ids)->where('id', 'in', function($query) use($childrenGroupIds) {
  158. $query->name('auth_group_access')->where('group_id', 'in', $childrenGroupIds)->field('uid');
  159. })->select();
  160. if ($adminList)
  161. {
  162. $deleteIds = [];
  163. foreach ($adminList as $k => $v)
  164. {
  165. $deleteIds[] = $v->id;
  166. }
  167. $deleteIds = array_diff($deleteIds, [$this->auth->id]);
  168. if ($deleteIds)
  169. {
  170. $this->model->destroy($deleteIds);
  171. model('AuthGroupAccess')->where('uid', 'in', $deleteIds)->delete();
  172. $this->code = 1;
  173. }
  174. }
  175. }
  176. return;
  177. }
  178. /**
  179. * 批量更新
  180. * @internal
  181. */
  182. public function multi($ids = "")
  183. {
  184. // 管理员禁止批量操作
  185. $this->code = -1;
  186. }
  187. }