Admin.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. $params['avatar'] = '/assets/img/avatar.png'; //设置新管理员默认头像。
  83. $admin = $this->model->create($params);
  84. $group = $this->request->post("group/a");
  85. //过滤不允许的组别,避免越权
  86. $group = array_intersect($this->childrenIds, $group);
  87. $dataset = [];
  88. foreach ($group as $value)
  89. {
  90. $dataset[] = ['uid' => $admin->id, 'group_id' => $value];
  91. }
  92. model('AuthGroupAccess')->saveAll($dataset);
  93. $this->code = 1;
  94. }
  95. return;
  96. }
  97. return $this->view->fetch();
  98. }
  99. /**
  100. * 编辑
  101. */
  102. public function edit($ids = NULL)
  103. {
  104. $row = $this->model->get(['id' => $ids]);
  105. if (!$row)
  106. $this->error(__('No Results were found'));
  107. if ($this->request->isPost())
  108. {
  109. $this->code = -1;
  110. $params = $this->request->post("row/a");
  111. if ($params)
  112. {
  113. if ($params['password'])
  114. {
  115. $params['salt'] = Random::alnum();
  116. $params['password'] = md5(md5($params['password']) . $params['salt']);
  117. }
  118. else
  119. {
  120. unset($params['password'], $params['salt']);
  121. }
  122. $row->save($params);
  123. // 先移除所有权限
  124. model('AuthGroupAccess')->where('uid', $row->id)->delete();
  125. $group = $this->request->post("group/a");
  126. // 过滤不允许的组别,避免越权
  127. $group = array_intersect($this->childrenIds, $group);
  128. $dataset = [];
  129. foreach ($group as $value)
  130. {
  131. $dataset[] = ['uid' => $row->id, 'group_id' => $value];
  132. }
  133. model('AuthGroupAccess')->saveAll($dataset);
  134. $this->code = 1;
  135. }
  136. return;
  137. }
  138. $grouplist = $this->auth->getGroups($row['id']);
  139. $groupids = [];
  140. foreach ($grouplist as $k => $v)
  141. {
  142. $groupids[] = $v['id'];
  143. }
  144. $this->view->assign("row", $row);
  145. $this->view->assign("groupids", $groupids);
  146. return $this->view->fetch();
  147. }
  148. /**
  149. * 删除
  150. */
  151. public function del($ids = "")
  152. {
  153. $this->code = -1;
  154. if ($ids)
  155. {
  156. // 避免越权删除管理员
  157. $childrenGroupIds = $this->childrenIds;
  158. $adminList = $this->model->where('id', 'in', $ids)->where('id', 'in', function($query) use($childrenGroupIds) {
  159. $query->name('auth_group_access')->where('group_id', 'in', $childrenGroupIds)->field('uid');
  160. })->select();
  161. if ($adminList)
  162. {
  163. $deleteIds = [];
  164. foreach ($adminList as $k => $v)
  165. {
  166. $deleteIds[] = $v->id;
  167. }
  168. $deleteIds = array_diff($deleteIds, [$this->auth->id]);
  169. if ($deleteIds)
  170. {
  171. $this->model->destroy($deleteIds);
  172. model('AuthGroupAccess')->where('uid', 'in', $deleteIds)->delete();
  173. $this->code = 1;
  174. }
  175. }
  176. }
  177. return;
  178. }
  179. /**
  180. * 批量更新
  181. * @internal
  182. */
  183. public function multi($ids = "")
  184. {
  185. // 管理员禁止批量操作
  186. $this->code = -1;
  187. }
  188. }